update
parent
df63ab8d5e
commit
5c8b5c20d6
|
@ -27,17 +27,22 @@ func ApiEventQuery(c *gin.Context) {
|
|||
}
|
||||
var result *gorm.DB
|
||||
|
||||
if grp != "" && auth.ACHas(c, auth.ADMIN, auth.SYSTEM, grp) {
|
||||
var grp_id uint
|
||||
db.Raw("select id from groups where name = ?", grp).Scan(&grp_id)
|
||||
if grp_id == 0 {
|
||||
panic(errors.ErrorNotFound)
|
||||
if grp != "" {
|
||||
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, grp) {
|
||||
panic(errors.ErrorForbidden)
|
||||
}
|
||||
result = db.Find(&events, "`group_id` = ? ", grp_id)
|
||||
result = db.Find(&events, `event_group_id in (
|
||||
select id from event_groups where group_id = (
|
||||
select id from groups where name = ?
|
||||
)
|
||||
)`, grp)
|
||||
} else if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM) {
|
||||
result = db.
|
||||
Find(&events, "`group_id` in (?) or group_id = 0",
|
||||
db.Table("groups").Select("id").Where("name in ?", cl.Groups))
|
||||
result = db.Find(&events, `event_group_id in (
|
||||
select eg.id from event_groups eg where eg.group_id in (
|
||||
select g.id from groups g where g.name in ?
|
||||
)
|
||||
)`, cl.Groups)
|
||||
|
||||
} else {
|
||||
result = db.Find(&events)
|
||||
}
|
||||
|
@ -60,15 +65,28 @@ func ApiEventNew(c *gin.Context) {
|
|||
if e.Start.IsZero() || e.End.IsZero() || e.Start.Before(e.End) {
|
||||
panic(ErrorInvalidTime)
|
||||
}
|
||||
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.GroupName) {
|
||||
cal := 0
|
||||
if db.Raw("select count(id) from calendars where id = ?", e.CalendarID).
|
||||
Scan(&cal); cal != 1 {
|
||||
panic(errors.ErrorNotFound)
|
||||
}
|
||||
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.EventGroup.GroupName) {
|
||||
panic(errors.ErrorForbidden)
|
||||
}
|
||||
if err := db.Create(e).Error; err != nil {
|
||||
if e.EventGroup.Name == "" {
|
||||
e.EventGroup.Name = e.Name
|
||||
}
|
||||
if err := db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(e).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
history.Send(history.Info().
|
||||
Nm("Create").
|
||||
Grp(e.GroupName).Bd(e).
|
||||
Grp(e.EventGroup.GroupName).Bd(e).
|
||||
Iss(c.GetString(auth.GinUserKey)).
|
||||
Msg("Event created"))
|
||||
server.OK(c, e)
|
||||
|
@ -90,7 +108,7 @@ func ApiEventUpdate(c *gin.Context) {
|
|||
}
|
||||
history.Send(history.Info().
|
||||
Nm("Update").
|
||||
Grp(e.GroupName).Bd(e).
|
||||
Grp(e.EventGroup.GroupName).Bd(e).
|
||||
Iss(c.GetString(auth.GinUserKey)).
|
||||
Msg("Event Updated"))
|
||||
server.OK(c, e)
|
||||
|
@ -107,13 +125,16 @@ func ApiEventDelete(c *gin.Context) {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
err = db.Delete(&Calendar{}, "id = ?", id).Error
|
||||
err = db.Delete(e, "id = ?", id).Error
|
||||
if err != nil {
|
||||
panic(errors.NewError(404, err))
|
||||
}
|
||||
db.Exec(`delete from event_groups where id = ?
|
||||
and 0 = (select count(id) from events where event_group_id = ?)`,
|
||||
e.EventGroupID, e.EventGroupID)
|
||||
history.Send(history.Info().
|
||||
Nm("Delete").
|
||||
Grp(e.GroupName).Bd(e).
|
||||
Grp(e.EventGroup.GroupName).Bd(e).
|
||||
Iss(c.GetString(auth.GinUserKey)).
|
||||
Msg("Event Deleted"))
|
||||
server.OK(c, "ok")
|
||||
|
|
|
@ -14,6 +14,11 @@ var l zerolog.Logger
|
|||
|
||||
var db *gorm.DB
|
||||
|
||||
var color []string = []string{
|
||||
"red", "pink", "purple", "deep-purple", "indigo", "blue", "light-blue", "cyan", "teal", "green",
|
||||
"light-green", "lime", "yellow", "amber", "orange", "deep-orange", "blue-grey",
|
||||
}
|
||||
|
||||
type Calendar struct {
|
||||
ID string `gorm:"primaryKey"`
|
||||
|
||||
|
@ -63,5 +68,6 @@ func (c *Calendar) AfterFind(tx *gorm.DB) (err error) {
|
|||
db.Raw("select name from groups where id = ?", c.GroupID).Scan(&name)
|
||||
c.GroupName = name
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -5,46 +5,31 @@ import (
|
|||
|
||||
"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
|
||||
Start time.Time
|
||||
End time.Time
|
||||
Name string `gorm:"not null"`
|
||||
|
||||
// ntu
|
||||
Class string
|
||||
CourseID string
|
||||
Semester string
|
||||
Color string `gorm:"-"`
|
||||
|
||||
IsExt bool
|
||||
|
||||
CalendarID string
|
||||
EventGroup EventGroup `json:"Detial"`
|
||||
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
|
||||
if e.CalendarID == "" {
|
||||
return errors.ErrorBadRequest
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -53,14 +38,16 @@ func (e *Event) BeforeCreate(tx *gorm.DB) (err error) {
|
|||
if e.ID == "" {
|
||||
e.ID = xid.New().String()
|
||||
}
|
||||
e.IsExt = false
|
||||
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
|
||||
eg := EventGroup{}
|
||||
if err := tx.First(&eg, "id = ?", e.EventGroupID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
e.EventGroup = eg
|
||||
e.Color = color[e.EventGroup.Color%len(color)]
|
||||
return
|
||||
}
|
||||
|
|
|
@ -15,7 +15,19 @@ type EventGroup struct {
|
|||
Name string
|
||||
Rrule string
|
||||
|
||||
Events []Event
|
||||
// event header
|
||||
Hosts string
|
||||
Description string
|
||||
Location string
|
||||
|
||||
// ntu
|
||||
Class string
|
||||
CourseID string
|
||||
Semester string
|
||||
|
||||
// UI
|
||||
Color int
|
||||
// event end
|
||||
|
||||
Group auth.Group `json:"-"`
|
||||
GroupName string `gorm:"-" json:"Group"`
|
||||
|
@ -36,6 +48,8 @@ func (eg *EventGroup) BeforeSave(tx *gorm.DB) (err error) {
|
|||
} else {
|
||||
eg.GroupID = 0
|
||||
}
|
||||
eg.Color = GetNewColor(eg.GroupName)
|
||||
|
||||
return
|
||||
}
|
||||
func (eg *EventGroup) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package calendar
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"kumoly.io/kumoly/app/server"
|
||||
"kumoly.io/kumoly/app/store"
|
||||
"kumoly.io/kumoly/app/util"
|
||||
|
@ -16,6 +19,7 @@ func (srv Service) Del() {}
|
|||
func (srv Service) Health() error { return nil }
|
||||
|
||||
func (srv Service) Init() error {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
db = store.DB
|
||||
|
||||
l = util.Klog.With().Str("mod", "auth").Logger()
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package calendar
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"kumoly.io/kumoly/app/auth"
|
||||
|
@ -26,10 +28,10 @@ func HasEventAccess(c *gin.Context, e *Event, cid string) error {
|
|||
if err != nil {
|
||||
return errors.NewError(404, err)
|
||||
}
|
||||
if e.GroupName == "" {
|
||||
if e.EventGroup.GroupName == "" {
|
||||
return nil
|
||||
}
|
||||
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.GroupName) {
|
||||
if !auth.ACHas(c, auth.ADMIN, auth.SYSTEM, e.EventGroup.GroupName) {
|
||||
return errors.ErrorForbidden
|
||||
}
|
||||
return nil
|
||||
|
@ -47,3 +49,13 @@ func ChangeCalGroup(tx *gorm.DB, cal_id, to string) error {
|
|||
) where calendar_id = ?`, to, cal_id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetNewColor(GroupName string) int {
|
||||
eg := &EventGroup{}
|
||||
if err := db.Select("color").
|
||||
Where("group_id = (select id from groups where name = ? )", GroupName).
|
||||
Last(eg).Error; err != nil {
|
||||
return rand.Intn(len(color))
|
||||
}
|
||||
return eg.Color + 1
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue