diff --git a/api/devices_update.go b/api/devices_update.go index 0ea22e4..4fa04b3 100644 --- a/api/devices_update.go +++ b/api/devices_update.go @@ -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 } diff --git a/rest/devices/create.http b/rest/devices/create.http index 4c28999..a80e1b0 100644 --- a/rest/devices/create.http +++ b/rest/devices/create.http @@ -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", diff --git a/rest/devices/update.http b/rest/devices/update.http index 564025c..3ec6527 100644 --- a/rest/devices/update.http +++ b/rest/devices/update.http @@ -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 } diff --git a/rest/subreddits/start.http b/rest/subreddits/start.http index d4cad57..95f0c2b 100644 --- a/rest/subreddits/start.http +++ b/rest/subreddits/start.http @@ -3,5 +3,5 @@ Host: localhost:8080 Content-Type: application/json { - "subreddit": "animemidriff" + "subreddit": "wallpapers" } diff --git a/server/routes/device_update.go b/server/routes/device_update.go index 4980243..ad05fbb 100644 --- a/server/routes/device_update.go +++ b/server/routes/device_update.go @@ -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) } diff --git a/views/homeview/homeview.templ b/views/homeview/homeview.templ index fd7c29f..7166d03 100644 --- a/views/homeview/homeview.templ +++ b/views/homeview/homeview.templ @@ -41,6 +41,9 @@ templ HomeContent(c *views.Context, data Data) { @recentRangeInput(c) @nsfwToggle(c, data) + if data.TotalImages == 0 { +

There are no recently added images in the current time range.

+ } for _, recently := range data.RecentlyAddedImages {

{ recently.Device.Name }