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) { handler, err := config.EventHandlerRepository().FindByID(ctxOf(c), intParam(c, "id")) if err != nil { return nil, err } if c.Query("hard") == "true" { var body models.EventHandler err := parseBody(c, &body) if err != nil || body.ID != intParam(c, "id") { return nil, err } handler = &body } else { var body models.EventHandlerUpdate err := parseBody(c, &body) 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 })) }