2021-12-21 11:44:27 +00:00
|
|
|
package calendar
|
|
|
|
|
2021-12-21 16:00:23 +00:00
|
|
|
import (
|
2021-12-25 09:48:33 +00:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2021-12-26 18:44:47 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-12-24 08:56:16 +00:00
|
|
|
"kumoly.io/kumoly/app/server"
|
2021-12-21 16:00:23 +00:00
|
|
|
"kumoly.io/kumoly/app/store"
|
|
|
|
"kumoly.io/kumoly/app/util"
|
|
|
|
)
|
|
|
|
|
2021-12-21 11:44:27 +00:00
|
|
|
type Service struct{}
|
|
|
|
|
|
|
|
func (srv Service) GetName() string { return "calendar.Service" }
|
2021-12-21 16:00:23 +00:00
|
|
|
func (srv Service) GetDependencies() []string { return []string{"server.Service"} }
|
2021-12-21 11:44:27 +00:00
|
|
|
func (srv Service) IsService() bool { return false }
|
|
|
|
func (srv Service) Main() error { return nil }
|
|
|
|
func (srv Service) Del() {}
|
|
|
|
func (srv Service) Health() error { return nil }
|
2021-12-21 16:00:23 +00:00
|
|
|
|
|
|
|
func (srv Service) Init() error {
|
2021-12-25 09:48:33 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2021-12-24 08:56:16 +00:00
|
|
|
db = store.DB
|
2021-12-21 16:00:23 +00:00
|
|
|
|
2022-01-02 16:50:21 +00:00
|
|
|
l = util.Klog.With().Str("mod", "calendar").Logger()
|
2021-12-21 16:00:23 +00:00
|
|
|
l.Debug().Msg("Migrating database for calendar.Service ...")
|
|
|
|
if err := store.Migrate(&Calendar{}, &EventGroup{}, &Event{}); err != nil {
|
|
|
|
l.Error().Err(err).Msg("Migrating database")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-24 08:56:16 +00:00
|
|
|
func (srv Service) Load() error {
|
|
|
|
calApi := server.API.Group("cal")
|
|
|
|
calApi.GET("", ApiCalQuery)
|
|
|
|
calApi.POST("", ApiCalNew)
|
|
|
|
calApi.PUT("", ApiCalUpdate)
|
|
|
|
calApi.DELETE("", ApiCalDelete)
|
|
|
|
|
2021-12-26 18:44:47 +00:00
|
|
|
//+-30day
|
2022-01-08 06:57:11 +00:00
|
|
|
server.Server.GET("ics/:cid/basic.ics", func(c *gin.Context) {
|
2021-12-26 18:44:47 +00:00
|
|
|
cid := c.Param("cid")
|
|
|
|
ics, err := ICS(cid,
|
|
|
|
time.Now().Add(time.Hour*-720),
|
|
|
|
time.Now().Add(time.Hour*720))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-01-08 07:05:36 +00:00
|
|
|
c.Header("Content-Type", "text/calendar; charset=utf-8")
|
2021-12-26 18:44:47 +00:00
|
|
|
c.String(200, ics)
|
|
|
|
})
|
|
|
|
|
2021-12-24 08:56:16 +00:00
|
|
|
eventApi := server.API.Group("event")
|
|
|
|
eventApi.GET("", ApiEventQuery)
|
|
|
|
eventApi.POST("", ApiEventNew)
|
|
|
|
eventApi.PUT("", ApiEventUpdate)
|
|
|
|
eventApi.DELETE("", ApiEventDelete)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|