fix item update api
All checks were successful
Build and Deploy API / build-and-deploy (push) Successful in 9s
All checks were successful
Build and Deploy API / build-and-deploy (push) Successful in 9s
This commit is contained in:
@@ -56,18 +56,7 @@ public class ItemsController : ControllerBase {
|
||||
[HttpPut("{id}")]
|
||||
public async Task<ActionResult<Item>> 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();
|
||||
|
||||
|
||||
@@ -31,10 +31,17 @@ public class ItemService {
|
||||
return item;
|
||||
}
|
||||
|
||||
public async Task<Item> Update(Item item) {
|
||||
db_.Items.Update(item);
|
||||
public async Task<Item?> 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<bool> Delete(int id) {
|
||||
|
||||
Reference in New Issue
Block a user