Added order item delete functionality

This commit is contained in:
2025-11-30 17:38:28 +01:00
parent bfda6dae0b
commit 8ad3085833
5 changed files with 49 additions and 1 deletions

View File

@@ -128,6 +128,24 @@ public class OrderController : Controller
return RedirectToAction("Join", new { code = order.OrderCode });
}
[HttpPost]
public async Task<IActionResult> DeleteItem(int orderItemId)
{
var orderItem = await _context.OrderItems
.Include(oi => oi.Order)
.FirstOrDefaultAsync(oi => oi.Id == orderItemId);
if (orderItem == null)
return NotFound("Order item not found");
if (orderItem.Order?.CreatorName != User.Identity?.Name && orderItem.ParticipantEmail != User.Identity?.Name)
return Forbid("Only the order creator or the participant who added the item can delete it");
_context.OrderItems.Remove(orderItem);
await _context.SaveChangesAsync();
return RedirectToAction("Details", new { code = orderItem.Order?.OrderCode });
}
// GET: Order/Details/{code}
public async Task<IActionResult> Details(string code)
{