color api
This commit is contained in:
@@ -12,6 +12,10 @@ func Bridges(r gin.IRoutes) {
|
||||
return config.BridgeRepository().FetchAll(ctxOf(c))
|
||||
}))
|
||||
|
||||
r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) {
|
||||
return config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
|
||||
}))
|
||||
|
||||
r.POST("", handler(func(c *gin.Context) (interface{}, error) {
|
||||
var body struct {
|
||||
Driver models.DriverKind `json:"driver"`
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
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
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user