-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVacationRequestsEndpoints.cs
138 lines (117 loc) · 6.11 KB
/
VacationRequestsEndpoints.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
namespace HrAspire.Web.ApiGateway.Endpoints;
using System.Security.Claims;
using HrAspire.Vacations.Web;
using HrAspire.Web.ApiGateway.Mappers;
using HrAspire.Web.Common;
using HrAspire.Web.Common.Models.VacationRequests;
using Microsoft.AspNetCore.Mvc;
public static class VacationRequestsEndpoints
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"StyleCop.CSharp.ReadabilityRules",
"SA1116:Split parameters should start on line after declaration",
Justification = "Better readability.")]
public static IEndpointConventionBuilder MapVacationRequestsEndpoints(this IEndpointRouteBuilder endpoints)
{
var group = endpoints.MapGroup("/").RequireAuthorization();
group.MapGet(
"/Employees/{employeeId}/VacationRequests",
(VacationRequests.VacationRequestsClient vacationRequestsClient,
ClaimsPrincipal user,
[FromRoute] string employeeId,
[FromQuery] int pageNumber = 0,
[FromQuery] int pageSize = 10)
=> GrpcToHttpHelper.HandleGrpcCallAsync(async () =>
{
var vacationRequestsResponse = await vacationRequestsClient.ListEmployeeVacationRequestsAsync(
new ListEmployeeVacationRequestsRequest
{
EmployeeId = employeeId,
PageNumber = pageNumber,
PageSize = pageSize,
CurrentEmployeeId = user.GetId(),
});
var vacationRequests = vacationRequestsResponse.VacationRequests.Select(e => e.MapToResponseModel()).ToList();
return Results.Ok(new VacationRequestsResponseModel(vacationRequests, vacationRequestsResponse.Total));
}));
group.MapPost(
"/VacationRequests",
(VacationRequests.VacationRequestsClient vacationRequestsClient,
[FromBody] VacationRequestCreateRequestModel model,
ClaimsPrincipal user)
=> GrpcToHttpHelper.HandleGrpcCallAsync(async () =>
{
var createResponse = await vacationRequestsClient.CreateAsync(new CreateVacationRequestRequest
{
EmployeeId = user.GetId()!,
Type = (VacationRequestType)(int)model.Type.GetValueOrDefault(),
FromDate = model.FromDate.GetValueOrDefault().ToTimestamp(),
ToDate = model.ToDate.GetValueOrDefault().ToTimestamp(),
Notes = model.Notes,
});
return Results.Created(string.Empty, createResponse.Id);
}));
group.MapGet(
"/VacationRequests/{id:int}",
(VacationRequests.VacationRequestsClient vacationRequestsClient, [FromRoute] int id, ClaimsPrincipal user)
=> GrpcToHttpHelper.HandleGrpcCallAsync(async () =>
{
var vacationRequestResponse = await vacationRequestsClient.GetAsync(
new GetVacationRequestRequest { Id = id, CurrentEmployeeId = user.GetId() });
var vacationRequest = vacationRequestResponse.VacationRequest.MapToDetailsResponseModel();
return Results.Ok(vacationRequest);
}));
group.MapPut(
"/VacationRequests/{id:int}",
(VacationRequests.VacationRequestsClient vacationRequestsClient,
[FromRoute] int id,
[FromBody] VacationRequestUpdateRequestModel model,
ClaimsPrincipal user)
=> GrpcToHttpHelper.HandleGrpcCallAsync(async () =>
{
var updateResponse = await vacationRequestsClient.UpdateAsync(
new UpdateVacationRequestRequest
{
Id = id,
Type = (VacationRequestType)(int)model.Type.GetValueOrDefault(),
FromDate = model.FromDate.ToTimestamp(),
ToDate = model.ToDate.ToTimestamp(),
Notes = model.Notes,
CurrentEmployeeId = user.GetId(),
});
return Results.Ok();
}));
group.MapDelete(
"/VacationRequests/{id:int}",
(VacationRequests.VacationRequestsClient vacationRequestsClient, [FromRoute] int id, ClaimsPrincipal user)
=> GrpcToHttpHelper.HandleGrpcCallAsync(async () =>
{
await vacationRequestsClient.DeleteAsync(
new DeleteVacationRequestRequest { Id = id, CurrentEmployeeId = user.GetId() });
return Results.Ok();
}));
group
.MapPost(
"/VacationRequests/{id:int}/Approval",
(VacationRequests.VacationRequestsClient vacationRequestsClient, [FromRoute] int id, ClaimsPrincipal user)
=> GrpcToHttpHelper.HandleGrpcCallAsync(async () =>
{
await vacationRequestsClient.ApproveAsync(
new ChangeStatusOfVacationRequestRequest { Id = id, CurrentEmployeeId = user.GetId()! });
return Results.Ok();
}))
.RequireAuthorization(Constants.ManagerAuthPolicyName);
group
.MapPost(
"/VacationRequests/{id:int}/Rejection",
(VacationRequests.VacationRequestsClient vacationRequestsClient, [FromRoute] int id, ClaimsPrincipal user)
=> GrpcToHttpHelper.HandleGrpcCallAsync(async () =>
{
await vacationRequestsClient.RejectAsync(
new ChangeStatusOfVacationRequestRequest { Id = id, CurrentEmployeeId = user.GetId()! });
return Results.Ok();
}))
.RequireAuthorization(Constants.ManagerAuthPolicyName);
return group;
}
}