| Name | Message | Date |
|---|---|---|
| 📄 OrdersController.cs | 1 day ago |
📄
OrdersController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; using Walley.Checkout.Api.Models; using Walley.Checkout.Api.Services; namespace Walley.Checkout.Api.Controllers; [ApiController] [Route("api/[controller]")] public class OrdersController : ControllerBase { private readonly IOrderService _orderService; private readonly IRefundService _refundService; public OrdersController(IOrderService orderService, IRefundService refundService) { _orderService = orderService; _refundService = refundService; } /// <summary> /// Get all orders. /// </summary> [HttpGet] public async Task<ActionResult<IEnumerable<Order>>> GetAll() { var orders = await _orderService.GetAllOrdersAsync(); return Ok(orders); } /// <summary> /// Get a specific order by ID. /// </summary> [HttpGet("{id}")] public async Task<ActionResult<Order>> GetById(string id) { // Task 4: This error handling needs improvement. // Right now any issue results in a generic 500 from the exception middleware. var order = await _orderService.GetOrderByIdAsync(id); return Ok(order); } /// <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. /// </summary> [HttpPost("{orderId}/refunds")] public async Task<ActionResult<Refund>> RequestRefund( string orderId, [FromBody] RefundRequest request) { var refund = await _refundService.ProcessRefundAsync( orderId, request.Amount, request.Reason); if (refund.Status == RefundStatus.Rejected) { return UnprocessableEntity(refund); } return Ok(refund); } } 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; } public string Reason { get; set; } = string.Empty; }