Adding ability to update and delete items
This commit is contained in:
@@ -36,4 +36,44 @@ public class ItemController : ControllerBase
|
||||
await _itemService.CreateAsync(item);
|
||||
return Ok(item);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[Authorize(Roles = "SUPER")]
|
||||
public async Task<IActionResult> 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<IActionResult> 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.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,4 +24,28 @@ public class ItemControllerTests
|
||||
|
||||
Assert.IsType<BadRequestObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Update_ItemNotFound()
|
||||
{
|
||||
var svc = new Mock<ItemService>(config);
|
||||
svc.Setup(i => i.GetByIdAsync("id")).ReturnsAsync((Item?)null);
|
||||
var controller = new ItemController(svc.Object, new Mock<LocationService>(config).Object);
|
||||
|
||||
var result = await controller.Update("id", new UpdateItemRequest { LocationId="loc", Name="x" });
|
||||
|
||||
Assert.IsType<NotFoundObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Delete_NotFound()
|
||||
{
|
||||
var svc = new Mock<ItemService>(config);
|
||||
svc.Setup(i => i.GetByIdAsync("id")).ReturnsAsync((Item?)null);
|
||||
var controller = new ItemController(svc.Object, new Mock<LocationService>(config).Object);
|
||||
|
||||
var result = await controller.Delete("id");
|
||||
|
||||
Assert.IsType<NotFoundObjectResult>(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace GameAuthApi.Models;
|
||||
|
||||
public class UpdateItemRequest
|
||||
{
|
||||
public string LocationId { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user