app/calendar/event_group.go

70 lines
1.2 KiB
Go
Raw Normal View History

2021-12-21 11:44:27 +00:00
package calendar
import (
"time"
"github.com/rs/xid"
"gorm.io/gorm"
2021-12-24 08:56:16 +00:00
"kumoly.io/kumoly/app/auth"
"kumoly.io/kumoly/app/errors"
2021-12-21 11:44:27 +00:00
)
type EventGroup struct {
ID string `gorm:"primaryKey"`
2021-12-24 08:56:16 +00:00
Name string
Rrule string
2021-12-25 09:48:33 +00:00
// event header
Hosts string
Description string
// ntu
2021-12-26 08:19:18 +00:00
Class string
CourseID string
Semester string
IsRegular bool
2021-12-25 09:48:33 +00:00
// UI
Color int
// event end
2021-12-21 11:44:27 +00:00
2021-12-24 08:56:16 +00:00
Group auth.Group `json:"-"`
GroupName string `gorm:"-" json:"Group"`
GroupID uint `json:"-"`
2021-12-21 11:44:27 +00:00
CreatedAt time.Time
UpdatedAt time.Time
}
2021-12-24 08:56:16 +00:00
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
}
2021-12-25 09:48:33 +00:00
2021-12-24 08:56:16 +00:00
return
}
2021-12-21 11:44:27 +00:00
func (eg *EventGroup) BeforeCreate(tx *gorm.DB) (err error) {
if eg.ID == "" {
eg.ID = xid.New().String()
}
2021-12-26 08:19:18 +00:00
eg.Color = GetNewColor(eg.GroupName)
2021-12-21 11:44:27 +00:00
return
}
2021-12-24 08:56:16 +00:00
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
}