app/calendar/event.go

67 lines
1.2 KiB
Go

package calendar
import (
"time"
"github.com/rs/xid"
"gorm.io/gorm"
"kumoly.io/kumoly/app/auth"
"kumoly.io/kumoly/app/errors"
)
type Event struct {
ID string `gorm:"primaryKey"`
Start time.Time
End time.Time
Name string `gorm:"not null"`
Hosts string
Description string
Location string
// ntu
Class string
CourseID string
Semester string
CalendarID string
EventGroupID string
Group auth.Group `json:"-"`
GroupName string `gorm:"-" json:"Group"`
GroupID uint `json:"-"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (e *Event) BeforeSave(tx *gorm.DB) (err error) {
if e.GroupName != "" {
var grp_id uint
db.Raw("select id from groups where name = ?", e.GroupName).Scan(&grp_id)
if grp_id == 0 {
return errors.ErrorNotFound
}
e.GroupID = grp_id
} else {
e.GroupID = 0
}
return
}
func (e *Event) BeforeCreate(tx *gorm.DB) (err error) {
if e.ID == "" {
e.ID = xid.New().String()
}
return
}
func (e *Event) AfterFind(tx *gorm.DB) (err error) {
if e.GroupID != 0 {
var name string
db.Raw("select name from groups where id = ?", e.GroupID).Scan(&name)
e.GroupName = name
}
return
}