2024-04-24 21:57:13 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
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-24 21:57:13 +07:00
|
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
|
|
if err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to parse id")
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
_ = json.NewEncoder(rw).Encode(map[string]string{"error": fmt.Sprintf("bad id: %s", err)})
|
|
|
|
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 11:34:30 +07:00
|
|
|
device, err := routes.API.DevicesUpdate(ctx, id, &models.DeviceSetter{
|
2024-04-24 21:57:13 +07:00
|
|
|
Slug: omit.FromCond(body.Slug, body.Slug != ""),
|
|
|
|
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
|
|
|
}
|