master
Evan Chen 2021-12-26 16:19:18 +08:00
parent 4cb2bad653
commit 1bb46c638d
4 changed files with 14 additions and 13 deletions

View File

@ -14,7 +14,7 @@ var l zerolog.Logger
var db *gorm.DB var db *gorm.DB
var color []string = []string{ var Color []string = []string{
"red", "pink", "purple", "deep-purple", "indigo", "blue", "light-blue", "cyan", "teal", "green", "red", "pink", "purple", "deep-purple", "indigo", "blue", "light-blue", "cyan", "teal", "green",
"light-green", "lime", "yellow", "amber", "orange", "deep-orange", "blue-grey", "light-green", "lime", "yellow", "amber", "orange", "deep-orange", "blue-grey",
} }

View File

@ -11,9 +11,10 @@ import (
type Event struct { type Event struct {
ID string `gorm:"primaryKey"` ID string `gorm:"primaryKey"`
Start time.Time Start time.Time
End time.Time End time.Time
Name string `gorm:"not null"` Name string `gorm:"not null"`
Location string
Color string `gorm:"-"` Color string `gorm:"-"`
@ -48,6 +49,6 @@ func (e *Event) AfterFind(tx *gorm.DB) (err error) {
return err return err
} }
e.EventGroup = eg e.EventGroup = eg
e.Color = color[e.EventGroup.Color%len(color)] e.Color = Color[e.EventGroup.Color%len(Color)]
return return
} }

View File

@ -18,12 +18,12 @@ type EventGroup struct {
// event header // event header
Hosts string Hosts string
Description string Description string
Location string
// ntu // ntu
Class string Class string
CourseID string CourseID string
Semester string Semester string
IsRegular bool
// UI // UI
Color int Color int
@ -48,7 +48,6 @@ func (eg *EventGroup) BeforeSave(tx *gorm.DB) (err error) {
} else { } else {
eg.GroupID = 0 eg.GroupID = 0
} }
eg.Color = GetNewColor(eg.GroupName)
return return
} }
@ -56,6 +55,7 @@ func (eg *EventGroup) BeforeCreate(tx *gorm.DB) (err error) {
if eg.ID == "" { if eg.ID == "" {
eg.ID = xid.New().String() eg.ID = xid.New().String()
} }
eg.Color = GetNewColor(eg.GroupName)
return return
} }

View File

@ -23,8 +23,8 @@ func HasCalAccess(c *gin.Context, cal *Calendar, cid string) error {
return nil return nil
} }
func HasEventAccess(c *gin.Context, e *Event, cid string) error { func HasEventAccess(c *gin.Context, e *Event, eid string) error {
err := db.First(e, "id = ?", cid).Error err := db.First(e, "id = ?", eid).Error
if err != nil { if err != nil {
return errors.NewError(404, err) return errors.NewError(404, err)
} }
@ -57,7 +57,7 @@ func GetNewColor(GroupName string) int {
if err := db.Select("color"). if err := db.Select("color").
Where("group_id = (select id from groups where name = ? )", GroupName). Where("group_id = (select id from groups where name = ? )", GroupName).
Last(eg).Error; err != nil { Last(eg).Error; err != nil {
return rand.Intn(len(color)) return rand.Intn(len(Color))
} }
return eg.Color + 1 return eg.Color + 1
} }