139 lines
3.9 KiB
C#
139 lines
3.9 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using OneForMe.Data;
|
|
using OneForMe.Models;
|
|
|
|
namespace OneForMe.Controllers;
|
|
|
|
[Authorize]
|
|
public class OrderController : Controller
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public OrderController(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: Order/Create
|
|
public IActionResult Create()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// POST: Order/Create
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(Order order, string[] itemNames, string[] itemPrices)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return View();
|
|
|
|
order.OrderCode = GenerateOrderCode();
|
|
order.CreatorName = User.Identity?.Name ?? "Unknown";
|
|
|
|
_context.Orders.Add(order);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// Add menu items
|
|
for (int i = 0; i < itemNames.Length; i++)
|
|
{
|
|
itemPrices[i] = itemPrices[i].Replace(".", ",");
|
|
if (!string.IsNullOrEmpty(itemNames[i]) && decimal.TryParse(itemPrices[i], out var price) && price > 0)
|
|
{
|
|
_context.MenuItems.Add(new MenuItem
|
|
{
|
|
OrderId = order.Id,
|
|
Name = itemNames[i],
|
|
Price = price
|
|
});
|
|
}
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
|
|
return RedirectToAction("Details", new { code = order.OrderCode });
|
|
}
|
|
|
|
// GET: Order/Join
|
|
public async Task<IActionResult> Join(string code)
|
|
{
|
|
var order = await _context.Orders
|
|
.Include(o => o.MenuItems)
|
|
.Include(o => o.OrderItems)
|
|
.FirstOrDefaultAsync(o => o.OrderCode == code);
|
|
|
|
if (order == null)
|
|
return NotFound("Order not found");
|
|
|
|
if (order.IsClosed)
|
|
return BadRequest("This order is closed");
|
|
|
|
return View(order);
|
|
}
|
|
|
|
// POST: Order/AddItem
|
|
[HttpPost]
|
|
public async Task<IActionResult> AddItem(int orderId, int menuItemId, int quantity, string participantName, string? participantEmail)
|
|
{
|
|
var order = await _context.Orders.FindAsync(orderId);
|
|
if (order == null || order.IsClosed)
|
|
return BadRequest("Order not found or is closed");
|
|
|
|
var menuItem = await _context.MenuItems.FindAsync(menuItemId);
|
|
if (menuItem == null)
|
|
return BadRequest("Menu item not found");
|
|
|
|
var orderItem = new OrderItem
|
|
{
|
|
OrderId = orderId,
|
|
MenuItemId = menuItemId,
|
|
Quantity = quantity,
|
|
ParticipantName = participantName,
|
|
ParticipantEmail = participantEmail
|
|
};
|
|
|
|
_context.OrderItems.Add(orderItem);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return RedirectToAction("Join", new { code = order.OrderCode });
|
|
}
|
|
|
|
// GET: Order/Details/{code}
|
|
public async Task<IActionResult> Details(string code)
|
|
{
|
|
var order = await _context.Orders
|
|
.Include(o => o.MenuItems)
|
|
.Include(o => o.OrderItems)
|
|
.FirstOrDefaultAsync(o => o.OrderCode == code);
|
|
|
|
if (order == null)
|
|
return NotFound();
|
|
|
|
return View(order);
|
|
}
|
|
|
|
// GET: Order/Close/{code}
|
|
public async Task<IActionResult> Close(string code)
|
|
{
|
|
var order = await _context.Orders.FirstOrDefaultAsync(o => o.OrderCode == code);
|
|
|
|
if (order == null)
|
|
return NotFound("Order not found");
|
|
|
|
if (order.IsClosed)
|
|
return BadRequest("Order is already closed");
|
|
|
|
order.IsClosed = true;
|
|
order.ClosedAt = DateTime.UtcNow;
|
|
|
|
_context.Orders.Update(order);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return RedirectToAction("Details", new { code = order.OrderCode });
|
|
}
|
|
|
|
private string GenerateOrderCode()
|
|
{
|
|
return Guid.NewGuid().ToString().Substring(0, 8).ToUpper();
|
|
}
|
|
} |