You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.6 KiB
71 lines
1.6 KiB
package api
|
|
|
|
import (
|
|
"git.aiterp.net/lucifer/new-server/app/config"
|
|
"git.aiterp.net/lucifer/new-server/models"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func EventHandlers(r gin.IRoutes) {
|
|
r.GET("", handler(func(c *gin.Context) (interface{}, error) {
|
|
return config.EventHandlerRepository().FetchAll(ctxOf(c))
|
|
}))
|
|
|
|
r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) {
|
|
return config.EventHandlerRepository().FindByID(ctxOf(c), intParam(c, "id"))
|
|
}))
|
|
|
|
r.POST("", handler(func(c *gin.Context) (interface{}, error) {
|
|
var body models.EventHandler
|
|
err := parseBody(c, &body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if body.Conditions == nil {
|
|
body.Conditions = make(map[string]models.EventCondition)
|
|
}
|
|
|
|
err = config.EventHandlerRepository().Save(ctxOf(c), &body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return body, nil
|
|
}))
|
|
|
|
r.PUT("/:id", handler(func(c *gin.Context) (interface{}, error) {
|
|
var body models.EventHandlerUpdate
|
|
err := parseBody(c, &body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
handler, err := config.EventHandlerRepository().FindByID(ctxOf(c), intParam(c, "id"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
handler.ApplyUpdate(body)
|
|
|
|
err = config.EventHandlerRepository().Save(ctxOf(c), handler)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return handler, nil
|
|
}))
|
|
|
|
r.DELETE("/:id", handler(func(c *gin.Context) (interface{}, error) {
|
|
handler, err := config.EventHandlerRepository().FindByID(ctxOf(c), intParam(c, "id"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = config.EventHandlerRepository().Delete(ctxOf(c), handler)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return handler, nil
|
|
}))
|
|
}
|