From da6ffa816783b8612e7e80646ebdaf21de371668 Mon Sep 17 00:00:00 2001 From: Blitblank Date: Sun, 22 Mar 2026 01:17:24 -0500 Subject: [PATCH] fix item update api --- api/src/Controllers/ItemsController.cs | 13 +------------ api/src/Services/ItemService.cs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/api/src/Controllers/ItemsController.cs b/api/src/Controllers/ItemsController.cs index 6d59798..79a3067 100644 --- a/api/src/Controllers/ItemsController.cs +++ b/api/src/Controllers/ItemsController.cs @@ -56,18 +56,7 @@ public class ItemsController : ControllerBase { [HttpPut("{id}")] public async Task> updateItem(int id, ItemDto item) { - Item? oldItem = await service_.Get(id); - if(oldItem == null) return NotFound(); - - Item updatedItem = new Item { - Id = id, - Name = item.Name, - Description = item.Description, - CreatedAt = oldItem.CreatedAt, - LastEditedAt = DateTime.UtcNow - }; - - var updated = await service_.Update(updatedItem); + var updated = await service_.Update(id, item); if (updated == null) return NotFound(); diff --git a/api/src/Services/ItemService.cs b/api/src/Services/ItemService.cs index eeacadb..8fab9ff 100644 --- a/api/src/Services/ItemService.cs +++ b/api/src/Services/ItemService.cs @@ -31,10 +31,17 @@ public class ItemService { return item; } - public async Task Update(Item item) { - db_.Items.Update(item); + public async Task Update(int id, ItemDto item) { + + Item? oldItem = await db_.Items.FindAsync(id); + if(oldItem == null) return oldItem; + + oldItem.Name = item.Name; + oldItem.Description = item.Description; + oldItem.LastEditedAt = DateTime.UtcNow; + await db_.SaveChangesAsync(); - return item; + return oldItem; } public async Task Delete(int id) {