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
|
||||
}))
|
||||
}
|
||||
@@ -15,9 +15,9 @@ func DriverProvider() models.DriverProvider {
|
||||
defer dpMutex.Unlock()
|
||||
|
||||
if dp == nil {
|
||||
dp = &drivers.MapBasedDriverProvider{Data: map[models.DriverKind]models.Driver{
|
||||
dp = drivers.DriverMap{
|
||||
models.DTNanoLeaf: &nanoleaf.Driver{},
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
return dp
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
var (
|
||||
bRepo models.BridgeRepository
|
||||
cpRepo models.ColorPresetRepository
|
||||
dRepo models.DeviceRepository
|
||||
ehRepo models.EventHandlerRepository
|
||||
)
|
||||
@@ -19,6 +20,14 @@ func BridgeRepository() models.BridgeRepository {
|
||||
return bRepo
|
||||
}
|
||||
|
||||
func ColorPresetRepository() models.ColorPresetRepository {
|
||||
if cpRepo == nil {
|
||||
cpRepo = &mysql.ColorPresetRepo{DBX: DBX()}
|
||||
}
|
||||
|
||||
return cpRepo
|
||||
}
|
||||
|
||||
func DeviceRepository() models.DeviceRepository {
|
||||
if dRepo == nil {
|
||||
panic("panik")
|
||||
|
||||
@@ -17,6 +17,7 @@ func StartServer() {
|
||||
|
||||
apiGin := ginny.Group("/api")
|
||||
api.Bridges(apiGin.Group("/bridges"))
|
||||
api.ColorPresets(apiGin.Group("/color-presets"))
|
||||
api.DriverKinds(apiGin.Group("/driver-kinds"))
|
||||
api.Events(apiGin.Group("/events"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user