From 7aa192d7281fea590ec4ce1b126844017a9131b1 Mon Sep 17 00:00:00 2001 From: hz Date: Mon, 9 Jun 2025 09:49:36 -0500 Subject: [PATCH] Adding ability to update and delete items --- Controllers/ItemController.cs | 40 ++++++++++++++++++++++++ GameAuthApi.Tests/ItemControllerTests.cs | 24 ++++++++++++++ Models/DTOs/UpdateItemRequest.cs | 7 +++++ Services/ItemService.cs | 5 +++ 4 files changed, 76 insertions(+) create mode 100644 Models/DTOs/UpdateItemRequest.cs diff --git a/Controllers/ItemController.cs b/Controllers/ItemController.cs index 1740fa2..908e9a5 100644 --- a/Controllers/ItemController.cs +++ b/Controllers/ItemController.cs @@ -36,4 +36,44 @@ public class ItemController : ControllerBase await _itemService.CreateAsync(item); return Ok(item); } + + [HttpPut("{id}")] + [Authorize(Roles = "SUPER")] + public async Task Update(string id, [FromBody] UpdateItemRequest req) + { + var item = await _itemService.GetByIdAsync(id); + if (item == null) + return NotFound("Item not found."); + + var location = await _locationService.GetByIdAsync(req.LocationId); + if (location == null) + return BadRequest("Invalid location ID."); + + if (item.LocationId != req.LocationId) + { + if (!string.IsNullOrEmpty(item.LocationId)) + await _locationService.RemoveItemFromLocationAsync(item.LocationId, item.Id); + await _locationService.AddItemToLocationAsync(req.LocationId, item.Id); + } + + item.Name = req.Name; + item.LocationId = req.LocationId; + await _itemService.UpdateAsync(item); + return Ok(item); + } + + [HttpDelete("{id}")] + [Authorize(Roles = "SUPER")] + public async Task Delete(string id) + { + var item = await _itemService.GetByIdAsync(id); + if (item == null) + return NotFound("Item not found."); + + if (!string.IsNullOrEmpty(item.LocationId)) + await _locationService.RemoveItemFromLocationAsync(item.LocationId, item.Id); + + await _itemService.DeleteAsync(id); + return Ok($"Item '{item.Name}' deleted."); + } } diff --git a/GameAuthApi.Tests/ItemControllerTests.cs b/GameAuthApi.Tests/ItemControllerTests.cs index 70db10c..2fbcded 100644 --- a/GameAuthApi.Tests/ItemControllerTests.cs +++ b/GameAuthApi.Tests/ItemControllerTests.cs @@ -24,4 +24,28 @@ public class ItemControllerTests Assert.IsType(result); } + + [Fact] + public async Task Update_ItemNotFound() + { + var svc = new Mock(config); + svc.Setup(i => i.GetByIdAsync("id")).ReturnsAsync((Item?)null); + var controller = new ItemController(svc.Object, new Mock(config).Object); + + var result = await controller.Update("id", new UpdateItemRequest { LocationId="loc", Name="x" }); + + Assert.IsType(result); + } + + [Fact] + public async Task Delete_NotFound() + { + var svc = new Mock(config); + svc.Setup(i => i.GetByIdAsync("id")).ReturnsAsync((Item?)null); + var controller = new ItemController(svc.Object, new Mock(config).Object); + + var result = await controller.Delete("id"); + + Assert.IsType(result); + } } diff --git a/Models/DTOs/UpdateItemRequest.cs b/Models/DTOs/UpdateItemRequest.cs new file mode 100644 index 0000000..f5131a2 --- /dev/null +++ b/Models/DTOs/UpdateItemRequest.cs @@ -0,0 +1,7 @@ +namespace GameAuthApi.Models; + +public class UpdateItemRequest +{ + public string LocationId { get; set; } + public string Name { get; set; } +} diff --git a/Services/ItemService.cs b/Services/ItemService.cs index 021379e..5a4af39 100644 --- a/Services/ItemService.cs +++ b/Services/ItemService.cs @@ -39,4 +39,9 @@ public class ItemService { await _items.ReplaceOneAsync(i => i.Id == item.Id, item); } + + public virtual async Task DeleteAsync(string id) + { + await _items.DeleteOneAsync(i => i.Id == id); + } }