Files
fm_be/internal/domain/file_manager/model_attachment.go
2026-07-16 22:16:45 +07:00

48 lines
1.8 KiB
Go

package filemanager
import (
"strings"
"time"
"gorm.io/gorm"
"wucher/internal/shared/pkg/uuidv7"
)
type Attachment struct {
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
FileID []byte `gorm:"type:binary(16);not null;column:file_id;index:idx_attachments_file_id;uniqueIndex:uq_attachments_ref_file,priority:3"`
RefType string `gorm:"type:varchar(64);not null;column:ref_type;index:idx_attachments_ref_sort,priority:1;index:idx_attachments_ref_category,priority:1;uniqueIndex:uq_attachments_ref_file,priority:1"`
RefID string `gorm:"type:varchar(191);not null;column:ref_id;index:idx_attachments_ref_sort,priority:2;index:idx_attachments_ref_category,priority:2;uniqueIndex:uq_attachments_ref_file,priority:2"`
Category *string `gorm:"type:varchar(64);column:category;index:idx_attachments_ref_category,priority:3"`
SortOrder int `gorm:"type:int;not null;default:0;column:sort_order;index:idx_attachments_ref_sort,priority:3"`
IsPrimary bool `gorm:"type:boolean;not null;default:false;column:is_primary"`
AttachedBy []byte `gorm:"type:binary(16);column:attached_by"`
CreatedAt time.Time `gorm:"column:created_at;index:idx_attachments_ref_sort,priority:4"`
UpdatedAt time.Time `gorm:"column:updated_at"`
File *File `gorm:"foreignKey:FileID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}
func (Attachment) TableName() string { return "attachments" }
func (a *Attachment) BeforeCreate(_ *gorm.DB) error {
if len(a.ID) == 0 {
a.ID = uuidv7.MustBytes()
}
if trimmed := strings.TrimSpace(a.RefType); trimmed != "" {
a.RefType = trimmed
}
if trimmed := strings.TrimSpace(a.RefID); trimmed != "" {
a.RefID = trimmed
}
if a.Category != nil {
value := strings.TrimSpace(*a.Category)
if value == "" {
a.Category = nil
} else {
a.Category = &value
}
}
return nil
}