app/calendar/calendar.go

67 lines
1.2 KiB
Go

package calendar
import (
"time"
"github.com/rs/xid"
"github.com/rs/zerolog"
"gorm.io/gorm"
"kumoly.io/kumoly/app/auth"
"kumoly.io/kumoly/app/errors"
)
var l zerolog.Logger
var db *gorm.DB
type Calendar struct {
ID string `gorm:"primaryKey"`
Name string `gorm:"index:idx_cal,unique"`
Description string
Timezone string
Events []Event
Group auth.Group `json:"-"`
GroupName string `gorm:"-" json:"Group"`
GroupID uint `gorm:"index:idx_cal,unique" json:"-"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (c *Calendar) BeforeSave(tx *gorm.DB) (err error) {
if c.GroupName != "" {
var grp_id uint
db.Raw("select id from groups where name = ?", c.GroupName).Scan(&grp_id)
if grp_id == 0 {
return errors.ErrorNotFound
}
c.GroupID = grp_id
} else {
c.GroupID = 0
}
return
}
func (c *Calendar) BeforeCreate(tx *gorm.DB) (err error) {
if c.ID == "" {
c.ID = xid.New().String()
}
if c.Timezone == "" {
c.Timezone = "Asia/Taipei"
}
return
}
func (c *Calendar) AfterFind(tx *gorm.DB) (err error) {
if c.GroupID != 0 {
var name string
db.Raw("select name from groups where id = ?", c.GroupID).Scan(&name)
c.GroupName = name
}
return
}