|
|
package api
import ( "git.aiterp.net/lucifer/new-server/app/config" "git.aiterp.net/lucifer/new-server/models" "github.com/gin-gonic/gin" )
func ColorPresets(r gin.IRoutes) { r.GET("", handler(func(c *gin.Context) (interface{}, error) { return config.ColorPresetRepository().FetchAll(ctxOf(c)) }))
r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) { return config.ColorPresetRepository().Find(ctxOf(c), intParam(c, "id")) }))
r.POST("", handler(func(c *gin.Context) (interface{}, error) { var body struct { Name string `json:"name"` ColorString string `json:"colorString"` } err := parseBody(c, &body) if err != nil { return nil, err }
newColor, err := models.ParseColorValue(body.ColorString) if err != nil { return nil, err }
preset := models.ColorPreset{ Name: body.Name, Value: newColor, }
preset.Validate() err = config.ColorPresetRepository().Save(ctxOf(c), &preset) if err != nil { return nil, err }
return preset, nil }))
r.PUT("/:id", handler(func(c *gin.Context) (interface{}, error) { var body struct { Name *string `json:"name"` ColorString *string `json:"colorString"` } err := parseBody(c, &body) if err != nil { return nil, err }
preset, err := config.ColorPresetRepository().Find(ctxOf(c), intParam(c, "id")) if err != nil { return nil, err }
if body.Name != nil { preset.Name = *body.Name }
if body.ColorString != nil { newColor, err := models.ParseColorValue(*body.ColorString) if err != nil { return nil, err }
preset.Value = newColor }
preset.Validate() err = config.ColorPresetRepository().Save(ctxOf(c), &preset) if err != nil { return nil, err }
return preset, nil }))
r.DELETE("/:id", handler(func(c *gin.Context) (interface{}, error) { preset, err := config.ColorPresetRepository().Find(ctxOf(c), intParam(c, "id")) if err != nil { return nil, err }
err = config.ColorPresetRepository().Delete(ctxOf(c), &preset) if err != nil { return nil, err }
return preset, nil })) }
|