55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using EonaCat.DoxaApi.Attributes;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SampleApi.Models;
|
|
|
|
namespace SampleApi.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/orders")]
|
|
[DoxaApiGroup("Orders")]
|
|
public class OrdersController : ControllerBase
|
|
{
|
|
private static readonly List<Order> _orders = new();
|
|
|
|
[HttpGet]
|
|
public ActionResult<List<Order>> GetOrders()
|
|
=> Ok(_orders.OrderByDescending(o => o.PlacedAt).ToList());
|
|
|
|
[HttpGet("{id}")]
|
|
public ActionResult<Order> GetById(Guid id)
|
|
{
|
|
var order = _orders.FirstOrDefault(o => o.Id == id);
|
|
return order is null ? NotFound() : Ok(order);
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult<Order> Create([FromBody] CreateOrderRequest request)
|
|
{
|
|
var order = new Order
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
UserId = request.UserId,
|
|
Lines = request.Lines,
|
|
Total = request.Lines.Sum(l => l.UnitPrice * l.Quantity),
|
|
Status = OrderStatus.Pending,
|
|
PlacedAt = DateTime.UtcNow
|
|
};
|
|
_orders.Add(order);
|
|
return CreatedAtAction(nameof(GetById), new { id = order.Id }, order);
|
|
}
|
|
|
|
[HttpPost("{id}/cancel")]
|
|
public ActionResult<Order> Cancel(Guid id)
|
|
{
|
|
var order = _orders.FirstOrDefault(o => o.Id == id);
|
|
if (order is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
order.Status = OrderStatus.Cancelled;
|
|
return Ok(order);
|
|
}
|
|
}
|
|
}
|