-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryStockEndpoints.cs
More file actions
107 lines (92 loc) · 4.26 KB
/
InventoryStockEndpoints.cs
File metadata and controls
107 lines (92 loc) · 4.26 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
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
// MIT-License
// Copyright BridgingIT GmbH - All Rights Reserved
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file at https://github.com/bridgingit/bitdevkit/license
namespace BridgingIT.DevKit.Examples.BookFiesta.Modules.Inventory.Presentation.Web;
using BridgingIT.DevKit.Common;
using BridgingIT.DevKit.Examples.BookFiesta.Modules.Inventory.Application;
using BridgingIT.DevKit.Examples.BookFiesta.Modules.Inventory.Domain;
using BridgingIT.DevKit.Presentation.Web;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
public class InventoryStockEndpoints : EndpointsBase
{
public override void Map(IEndpointRouteBuilder app)
{
var group = app
.MapGroup("api/tenants/{tenantId}/inventory/stocks")
.WithTags("Inventory");
group.MapGet("/{id}", GetStock)
.WithName("GetInventoryStock")
.Produces<ProblemDetails>(400)
.Produces<ProblemDetails>(500);
group.MapGet("/", GetStocks)
.WithName("GetInventoryStocks")
.Produces<ProblemDetails>(400)
.Produces<ProblemDetails>(500);
group.MapPost("/", CreateStock)
.WithName("CreateInventoryStock")
.ProducesValidationProblem()
.Produces<ProblemDetails>(400)
.Produces<ProblemDetails>(500);
group.MapPost("/{id}/movements", CreateStockMovement)
.WithName("CreateInventoryStockMovement")
.ProducesValidationProblem()
.Produces<ProblemDetails>(400)
.Produces<ProblemDetails>(500);
}
private static async Task<Results<Ok<StockModel>, NotFound, ProblemHttpResult>> GetStock(
[FromServices] IMediator mediator,
[FromServices] IMapper mapper,
[FromRoute] string tenantId,
[FromRoute] string id)
{
var result = (await mediator.Send(new StockFindOneQuery(tenantId, id))).Result;
return result.IsFailure && result.HasError<NotFoundResultError>()
? TypedResults.NotFound()
: result.IsSuccess
? TypedResults.Ok(mapper.Map<Stock, StockModel>(result.Value))
: TypedResults.Problem(result.ToString(), statusCode: 400);
}
private static async Task<Results<Ok<IEnumerable<StockModel>>, ProblemHttpResult>> GetStocks(
[FromServices] IMediator mediator,
[FromServices] IMapper mapper,
[FromRoute] string tenantId)
{
var result = (await mediator.Send(new StockFindAllQuery(tenantId))).Result;
return result.IsSuccess
? TypedResults.Ok(mapper.Map<IEnumerable<Stock>, IEnumerable<StockModel>>(result.Value))
: TypedResults.Problem(result.ToString(), statusCode: 400);
}
private static async Task<Results<Created<StockModel>, ProblemHttpResult>> CreateStock(
[FromServices] IMediator mediator,
[FromServices] IMapper mapper,
[FromRoute] string tenantId,
[FromBody] StockModel model)
{
var result = (await mediator.Send(new StockCreateCommand(tenantId, model))).Result;
return result.IsSuccess
? TypedResults.Created(
$"api/tenants/{tenantId}/inventory/stocks/{result.Value.Id}",
mapper.Map<Stock, StockModel>(result.Value))
: TypedResults.Problem(result.ToString(), statusCode: 400);
}
private static async Task<Results<Created<StockModel>, NotFound, ProblemHttpResult>> CreateStockMovement(
[FromServices] IMediator mediator,
[FromServices] IMapper mapper,
[FromRoute] string tenantId,
[FromRoute] string id,
[FromBody] StockMovementModel model)
{
var result = (await mediator.Send(new StockMovementApplyCommand(tenantId, id, model))).Result;
return result.IsFailure && result.HasError<NotFoundResultError>() ? TypedResults.NotFound()
: result.IsSuccess ? TypedResults.Created(
$"api/tenants/{tenantId}/inventory/stocks/{result.Value.Id}",
mapper.Map<Stock, StockModel>(result.Value))
: TypedResults.Problem(result.ToString(), statusCode: 400);
}
}