device: updating values now return rows
This commit is contained in:
parent
e5b1d4aa9d
commit
0073306563
|
@ -9,20 +9,26 @@ import (
|
|||
"github.com/tigorlazuardi/redmage/pkg/errs"
|
||||
)
|
||||
|
||||
func (api *API) DevicesUpdate(ctx context.Context, id int, update *models.DeviceSetter) (err error) {
|
||||
func (api *API) DevicesUpdate(ctx context.Context, id int, update *models.DeviceSetter) (device *models.Device, err error) {
|
||||
ctx, span := tracer.Start(ctx, "*API.DevicesUpdate")
|
||||
defer span.End()
|
||||
|
||||
err = models.Devices.Update(ctx, api.db, update, &models.Device{ID: int32(id)})
|
||||
device = &models.Device{ID: int32(id)}
|
||||
|
||||
err = models.Devices.Update(ctx, api.db, update, device)
|
||||
if err != nil {
|
||||
var sqliteErr sqlite3.Error
|
||||
if errors.As(err, &sqliteErr) {
|
||||
if sqliteErr.Code == sqlite3.ErrConstraint {
|
||||
return errs.Wrapw(err, "a device with the same slug id already exists").Code(409)
|
||||
return device, errs.Wrapw(err, "a device with the same slug id already exists").Code(409)
|
||||
}
|
||||
}
|
||||
return errs.Wrapw(err, "failed to update device", "id", id, "values", update)
|
||||
return device, errs.Wrapw(err, "failed to update device", "id", id, "values", update)
|
||||
}
|
||||
|
||||
return
|
||||
if err := device.Reload(ctx, api.db); err != nil {
|
||||
return device, errs.Wrapw(err, "failed to reload device", "id", id)
|
||||
}
|
||||
|
||||
return device, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
POST http://localhost:8080/api/v1/devices HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Content-Type: application/json
|
||||
Content-Length: 211
|
||||
|
||||
{
|
||||
"name": "Laptop",
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
PATCH http://localhost:8080/api/v1/devices/1 HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Content-Type: application/json
|
||||
Content-Length: 78
|
||||
|
||||
{
|
||||
"slug": "laptop",
|
||||
"aspect_ratio_tolerance": 0.2,
|
||||
"nsfw": 1
|
||||
"nsfw": 0
|
||||
}
|
||||
|
|
|
@ -3,5 +3,5 @@ Host: localhost:8080
|
|||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"subreddit": "animemidriff"
|
||||
"subreddit": "wallpapers"
|
||||
}
|
||||
|
|
|
@ -31,6 +31,11 @@ func (routes *Routes) APIDeviceUpdate(rw http.ResponseWriter, r *http.Request) {
|
|||
ctx, span := tracer.Start(r.Context(), "*Routes.APIDeviceUpdate")
|
||||
defer span.End()
|
||||
|
||||
var (
|
||||
enc = json.NewEncoder(rw)
|
||||
dec = json.NewDecoder(r.Body)
|
||||
)
|
||||
|
||||
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
log.New(ctx).Err(err).Error("failed to parse id")
|
||||
|
@ -41,14 +46,14 @@ func (routes *Routes) APIDeviceUpdate(rw http.ResponseWriter, r *http.Request) {
|
|||
|
||||
var body deviceUpdate
|
||||
|
||||
if err = json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
if err = dec.Decode(&body); err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
err = routes.API.DevicesUpdate(ctx, id, &models.DeviceSetter{
|
||||
device, err := routes.API.DevicesUpdate(ctx, id, &models.DeviceSetter{
|
||||
Slug: omit.FromCond(body.Slug, body.Slug != ""),
|
||||
Name: omit.FromCond(body.Name, body.Name != ""),
|
||||
ResolutionX: omit.FromCond(body.ResolutionX, body.ResolutionX != 0),
|
||||
|
@ -63,11 +68,12 @@ func (routes *Routes) APIDeviceUpdate(rw http.ResponseWriter, r *http.Request) {
|
|||
UpdatedAt: omit.From(time.Now().Unix()),
|
||||
})
|
||||
if err != nil {
|
||||
log.New(ctx).Err(err).Error("failed to update device")
|
||||
code, message := errs.HTTPMessage(err)
|
||||
rw.WriteHeader(code)
|
||||
_ = json.NewEncoder(rw).Encode(map[string]string{"error": message})
|
||||
_ = enc.Encode(map[string]string{"error": message})
|
||||
return
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(rw).Encode(map[string]string{"message": "ok"})
|
||||
_ = enc.Encode(device)
|
||||
}
|
||||
|
|
|
@ -41,6 +41,9 @@ templ HomeContent(c *views.Context, data Data) {
|
|||
@recentRangeInput(c)
|
||||
@nsfwToggle(c, data)
|
||||
</div>
|
||||
if data.TotalImages == 0 {
|
||||
<h2 class="mt-4">There are no recently added images in the current time range.</h2>
|
||||
}
|
||||
for _, recently := range data.RecentlyAddedImages {
|
||||
<div class="divider"></div>
|
||||
<h2 class="mt-4">{ recently.Device.Name }</h2>
|
||||
|
|
Loading…
Reference in a new issue