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 Events []Event 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 } else { eg.GroupID = 0 } return } func (eg *EventGroup) BeforeCreate(tx *gorm.DB) (err error) { if eg.ID == "" { eg.ID = xid.New().String() } 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 }