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.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user