-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryStockSnapshotEndpoints.cs
More file actions
88 lines (76 loc) · 3.53 KB
/
InventoryStockSnapshotEndpoints.cs
File metadata and controls
88 lines (76 loc) · 3.53 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
// 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 InventoryStockSnapshotEndpoints : EndpointsBase
{
public override void Map(IEndpointRouteBuilder app)
{
var group = app
.MapGroup("api/tenants/{tenantId}/inventory/stocks/{stockId}/stocksnapshots")
.WithTags("Inventory");
group.MapGet("/{id}", GetStockSnapshot)
.WithName("GetInventoryStockSnapshot")
.Produces<ProblemDetails>(400)
.Produces<ProblemDetails>(500);
group.MapGet("/", GetStockSnapshots)
.WithName("GetInventoryStockSnapshots")
.Produces<ProblemDetails>(400)
.Produces<ProblemDetails>(500);
group.MapPost("/", CreateStockSnapshot)
.WithName("CreateInventoryStockSnapshot")
.ProducesValidationProblem()
.Produces<ProblemDetails>(400)
.Produces<ProblemDetails>(500);
}
private static async Task<Results<Ok<StockSnapshotModel>, NotFound, ProblemHttpResult>> GetStockSnapshot(
[FromServices] IMediator mediator,
[FromServices] IMapper mapper,
[FromRoute] string tenantId,
[FromRoute] string stockId,
[FromRoute] string id)
{
var result = (await mediator.Send(
new StockSnapshotFindOneQuery(tenantId, stockId, id))).Result;
return result.Value == null ? TypedResults.NotFound() :
result.IsSuccess ? TypedResults.Ok(mapper.Map<StockSnapshot, StockSnapshotModel>(result.Value)) :
TypedResults.Problem(result.ToString(), statusCode: 400);
}
private static async Task<Results<Ok<IEnumerable<StockSnapshotModel>>, ProblemHttpResult>> GetStockSnapshots(
[FromServices] IMediator mediator,
[FromServices] IMapper mapper,
[FromRoute] string tenantId,
[FromRoute] string stockId)
{
var result = (await mediator.Send(
new StockSnapshotFindAllQuery(tenantId, stockId))).Result;
return result.IsSuccess
? TypedResults.Ok(mapper.Map<IEnumerable<StockSnapshot>, IEnumerable<StockSnapshotModel>>(result.Value))
: TypedResults.Problem(result.ToString(), statusCode: 400);
}
private static async Task<Results<Created<StockSnapshotModel>, ProblemHttpResult>> CreateStockSnapshot(
[FromServices] IMediator mediator,
[FromServices] IMapper mapper,
[FromRoute] string tenantId,
[FromRoute] string stockId)
{
var result = (await mediator.Send(
new StockSnapshotCreateCommand(tenantId, stockId))).Result;
return result.IsSuccess
? TypedResults.Created(
$"api/tenants/{tenantId}/inventory/stocks/{stockId}/stocksnapshots/{result.Value.Id}",
mapper.Map<StockSnapshot, StockSnapshotModel>(result.Value))
: TypedResults.Problem(result.ToString(), statusCode: 400);
}
}