2024-04-24 21:57:13 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-04-29 21:45:18 +07:00
|
|
|
"time"
|
2024-04-24 21:57:13 +07:00
|
|
|
|
|
|
|
"github.com/aarondl/opt/omit"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/tigorlazuardi/redmage/models"
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type deviceUpdate struct {
|
|
|
|
models.Device
|
|
|
|
|
|
|
|
MinX *int32 `json:"min_x"`
|
|
|
|
MinY *int32 `json:"min_y"`
|
|
|
|
MaxX *int32 `json:"max_x"`
|
|
|
|
MaxY *int32 `json:"max_y"`
|
|
|
|
NSFW *int32 `json:"nsfw"`
|
|
|
|
WindowsWallpaperMode *int32 `json:"windows_wallpaper_mode"`
|
|
|
|
AspectRatioTolerance *float64 `json:"aspect_ratio_tolerance"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (routes *Routes) APIDeviceUpdate(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
ctx, span := tracer.Start(r.Context(), "*Routes.APIDeviceUpdate")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-04-30 11:34:30 +07:00
|
|
|
var (
|
|
|
|
enc = json.NewEncoder(rw)
|
|
|
|
dec = json.NewDecoder(r.Body)
|
|
|
|
)
|
|
|
|
|
2024-04-30 14:12:33 +07:00
|
|
|
slug := chi.URLParam(r, "slug")
|
|
|
|
if slug == "" {
|
2024-04-24 21:57:13 +07:00
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
2024-04-30 14:12:33 +07:00
|
|
|
_ = json.NewEncoder(rw).Encode(map[string]string{"error": "missing name"})
|
2024-04-24 21:57:13 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var body deviceUpdate
|
|
|
|
|
2024-04-30 11:34:30 +07:00
|
|
|
if err = dec.Decode(&body); err != nil {
|
2024-04-24 21:57:13 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to decode json body")
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
_ = json.NewEncoder(rw).Encode(map[string]string{"error": fmt.Sprintf("cannot decode json body: %s", err)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-30 14:12:33 +07:00
|
|
|
device, err := routes.API.DevicesUpdate(ctx, slug, &models.DeviceSetter{
|
2024-04-24 21:57:13 +07:00
|
|
|
Name: omit.FromCond(body.Name, body.Name != ""),
|
|
|
|
ResolutionX: omit.FromCond(body.ResolutionX, body.ResolutionX != 0),
|
|
|
|
ResolutionY: omit.FromCond(body.ResolutionY, body.ResolutionY != 0),
|
|
|
|
AspectRatioTolerance: omit.FromPtr(body.AspectRatioTolerance),
|
|
|
|
MinX: omit.FromPtr(body.MinX),
|
|
|
|
MinY: omit.FromPtr(body.MinY),
|
|
|
|
MaxX: omit.FromPtr(body.MaxX),
|
|
|
|
MaxY: omit.FromPtr(body.MaxY),
|
|
|
|
NSFW: omit.FromPtr(body.NSFW),
|
|
|
|
WindowsWallpaperMode: omit.FromPtr(body.WindowsWallpaperMode),
|
2024-04-29 21:45:18 +07:00
|
|
|
UpdatedAt: omit.From(time.Now().Unix()),
|
2024-04-24 21:57:13 +07:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-04-30 11:34:30 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to update device")
|
2024-04-24 22:26:04 +07:00
|
|
|
code, message := errs.HTTPMessage(err)
|
|
|
|
rw.WriteHeader(code)
|
2024-04-30 11:34:30 +07:00
|
|
|
_ = enc.Encode(map[string]string{"error": message})
|
2024-04-24 21:57:13 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-30 11:34:30 +07:00
|
|
|
_ = enc.Encode(device)
|
2024-04-24 21:57:13 +07:00
|
|
|
}
|