Files
latosa-escrima/backend/core/models/shortcodes.go
2025-01-29 18:09:41 +01:00

37 lines
1.1 KiB
Go

package models
import (
"errors"
"github.com/google/uuid"
"github.com/uptrace/bun"
)
type ShortcodeType string
const (
ShortcodeMedia ShortcodeType = "media"
ShortcodeValue ShortcodeType = "value"
)
type Shortcode struct {
bun.BaseModel `bun:"table:shortcodes,alias:sc"`
ID int64 `bun:"id,pk,autoincrement" json:"id"` // Primary key
Code string `bun:"code,notnull,unique" json:"code"` // The shortcode value
Type ShortcodeType `bun:"shortcode_type,notnull" json:"type"`
Value *string `bun:"value" json:"value,omitempty"`
MediaID *uuid.UUID `bun:"media_id,type:uuid" json:"media_id,omitempty"` // Nullable reference to another table's ID
Media *Media `bun:"rel:belongs-to,join:media_id=id" json:"media,omitempty"` // Relation to Media
}
func (s *Shortcode) Validate() error {
if s.Value != nil && s.MediaID != nil {
return errors.New("both value and media_id cannot be set at the same time")
}
if s.Value == nil && s.MediaID == nil {
return errors.New("either value or media_id must be set")
}
return nil
}