package calendar import ( "time" "github.com/rs/xid" "gorm.io/gorm" "kumoly.io/kumoly/app/errors" ) type Event struct { ID string `gorm:"primaryKey"` Start time.Time End time.Time Name string `gorm:"not null"` Color string `gorm:"-"` IsExt bool CalendarID string EventGroup EventGroup `json:"Detial"` EventGroupID string CreatedAt time.Time UpdatedAt time.Time } func (e *Event) BeforeSave(tx *gorm.DB) (err error) { if e.CalendarID == "" { return errors.ErrorBadRequest } return } func (e *Event) BeforeCreate(tx *gorm.DB) (err error) { if e.ID == "" { e.ID = xid.New().String() } e.IsExt = false return } func (e *Event) AfterFind(tx *gorm.DB) (err error) { eg := EventGroup{} if err := tx.First(&eg, "id = ?", e.EventGroupID).Error; err != nil { return err } e.EventGroup = eg e.Color = color[e.EventGroup.Color%len(color)] return }