IMPROVEMENTS.md
+7
-0
diff --git a/IMPROVEMENTS.md b/IMPROVEMENTS.md
new file mode 100644
index 0000000..18a1a88
@@ -0,0 +1,7 @@
# Improvements
`UnitPrice` of `OrderLine` shouldn't be part of the API call, it should be looked up in DB based on `ProductName` (or even better an id).
Separate domain models from API. Right now we need to consider the shape of the API when updating the "domain" models.
Document all response types and status codes in the OpenAPI specification.
src/Walley.Checkout.Api/Controllers/OrdersController.cs
+47
-2
diff --git a/src/Walley.Checkout.Api/Controllers/OrdersController.cs b/src/Walley.Checkout.Api/Controllers/OrdersController.cs
index 658c320..6a94046 100644
@@ -1,3 +1,4 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Walley.Checkout.Api.Models;
using Walley.Checkout.Api.Services;
@@ -39,8 +40,15 @@ public class OrdersController : ControllerBase
return Ok(order);
}
// Task 1: Implement POST /api/orders
// See README for requirements.
/// <summary>
/// Creates a new order.
/// </summary>
[HttpPost]
public async Task<ActionResult<Order>> CreateOrder([FromBody] CreateOrderRequest request)
{
var order = await _orderService.CreateOrderAsync(request.ToDomainModel());
return CreatedAtAction(nameof(GetById), new { id = order.Id }, order);
}
/// <summary>
/// Request a refund for an order.
@@ -64,6 +72,43 @@ public class OrdersController : ControllerBase
}
}
public class CreateOrderRequest
{
[MinLength(1)]
public string CustomerName { get; set; } = string.Empty;
[EmailAddress]
public string CustomerEmail { get; set; } = string.Empty;
[MinLength(1)]
public List<CreateOrderLineRequest> Lines { get; set; } = new();
[RegularExpression(@"^[A-Z]{3}$")]
public string Currency { get; set; } = string.Empty;
public class CreateOrderLineRequest
{
[MinLength(1)]
public string ProductName { get; set; } = string.Empty;
[Range(1, int.MaxValue)]
public int Quantity { get; set; }
[Range(0d, double.MaxValue)]
public decimal UnitPrice { get; set; }
public OrderLine ToDomainModel() => new()
{
ProductName = ProductName,
Quantity = Quantity,
UnitPrice = UnitPrice,
};
}
public Order ToDomainModel() => new()
{
CustomerName = CustomerName,
CustomerEmail = CustomerEmail,
Lines = Lines.Select(l => l.ToDomainModel()).ToList(),
Currency = Currency,
};
}
public class RefundRequest
{
public decimal Amount { get; set; }