-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrdersController.cs
More file actions
62 lines (54 loc) · 2.44 KB
/
Copy pathOrdersController.cs
File metadata and controls
62 lines (54 loc) · 2.44 KB
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
namespace Comanda.Orders.WebApi.Controllers;
[ApiController]
[Authorize]
[Route("api/v1/orders")]
public sealed class OrdersController(IDispatcher dispatcher) : ControllerBase
{
[HttpGet]
[Stability(Stability.Stable)]
public async Task<IActionResult> GetOrdersAsync([FromQuery] OrdersFetchParameters request, CancellationToken cancellation)
{
var result = await dispatcher.DispatchAsync(request, cancellation);
/* applies pagination navigation links according to RFC 8288 (web linking) */
/* https://datatracker.ietf.org/doc/html/rfc8288 */
if (result.IsSuccess && result.Data is not null)
{
Response.WithPagination(result.Data);
Response.WithWebLinking(result.Data, Request);
}
// we know the switch here is not strictly necessary since we only handle the success case,
// but we keep it for consistency with the rest of the codebase and to follow established patterns.
return result switch
{
{ IsSuccess: true } when result.Data is not null =>
StatusCode(StatusCodes.Status200OK, result.Data.Items),
};
}
[HttpPost]
[Stability(Stability.Stable)]
public async Task<IActionResult> CreateOrderAsync([FromBody] OrderCreationScheme request, CancellationToken cancellation)
{
var result = await dispatcher.DispatchAsync(request, cancellation);
// we know the switch here is not strictly necessary since we only handle the success case,
// but we keep it for consistency with the rest of the codebase and to follow established patterns.
return result switch
{
{ IsSuccess: true } when result.Data is not null =>
StatusCode(StatusCodes.Status201Created, result.Data),
};
}
[HttpPut("{id}")]
[Stability(Stability.Stable)]
public async Task<IActionResult> UpdateOrderAsync(
[FromBody] OrderModificationScheme request, [FromRoute] string id, CancellationToken cancellation)
{
var result = await dispatcher.DispatchAsync(request with { Id = id }, cancellation);
return result switch
{
{ IsSuccess: true } when result.Data is not null =>
StatusCode(StatusCodes.Status200OK, result.Data),
{ IsFailure: true } when result.Error == OrderErrors.OrderDoesNotExist =>
StatusCode(StatusCodes.Status404NotFound, result.Error)
};
}
}