app/calendar/event.go

67 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 Event struct {
ID string `gorm:"primaryKey"`
2021-12-24 08:56:16 +00:00
Start time.Time
End time.Time
Name string `gorm:"not null"`
Hosts string
Description string
Location string
// ntu
Class string
CourseID string
Semester string
2021-12-21 11:44:27 +00:00
CalendarID string
EventGroupID string
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 (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
}
2021-12-21 11:44:27 +00:00
func (e *Event) BeforeCreate(tx *gorm.DB) (err error) {
if e.ID == "" {
e.ID = xid.New().String()
}
return
}
2021-12-24 08:56:16 +00:00
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
}