package calendar import ( "time" "github.com/rs/xid" "gorm.io/gorm" "kumoly.io/kumoly/app/auth" "kumoly.io/kumoly/app/errors" ) type EventGroup struct { ID string `gorm:"primaryKey"` Name string Rrule string // event header Hosts string Description string // ntu Class string CourseID string Semester string IsRegular bool // UI Color int // event end Group auth.Group `json:"-"` GroupName string `gorm:"-" json:"Group"` GroupID uint `json:"-"` CreatedAt time.Time UpdatedAt time.Time } func (eg *EventGroup) BeforeSave(tx *gorm.DB) (err error) { if eg.GroupName != "" { var grp_id uint db.Raw("select id from groups where name = ?", eg.GroupName).Scan(&grp_id) if grp_id == 0 { return errors.ErrorNotFound } eg.GroupID = grp_id } return } func (eg *EventGroup) BeforeCreate(tx *gorm.DB) (err error) { if eg.ID == "" { eg.ID = xid.New().String() } eg.Color = GetNewColor(eg.GroupName) return } func (eg *EventGroup) AfterFind(tx *gorm.DB) (err error) { if eg.GroupID != 0 { var name string db.Raw("select name from groups where id = ?", eg.GroupID).Scan(&name) eg.GroupName = name } return }