Files
nysoure/server/model/tag.go
2025-05-30 17:39:57 +08:00

48 lines
1.1 KiB
Go

package model
import "gorm.io/gorm"
type Tag struct {
gorm.Model
Name string `gorm:"unique"`
Description string
AliasOf *uint `gorm:"default:NULL"` // Foreign key for aliasing, can be NULL
Type string
Resources []Resource `gorm:"many2many:resource_tags;"`
Aliases []Tag `gorm:"foreignKey:AliasOf;references:ID"`
}
type TagView struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Aliases []string `json:"aliases"`
}
func (t *Tag) ToView() *TagView {
aliases := make([]string, 0, len(t.Aliases))
for _, alias := range t.Aliases {
aliases = append(aliases, alias.Name)
}
return &TagView{
ID: t.ID,
Name: t.Name,
Description: t.Description,
Type: t.Type,
Aliases: aliases,
}
}
type TagViewWithCount struct {
TagView
ResourceCount int `json:"resources_count"` // Count of resources associated with the tag
}
func (t *TagView) WithCount(count int) *TagViewWithCount {
return &TagViewWithCount{
TagView: *t,
ResourceCount: count,
}
}