-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmployeesGrpcService.cs
113 lines (91 loc) · 3.48 KB
/
EmployeesGrpcService.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
namespace HrAspire.Employees.Web.Services;
using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using HrAspire.Employees.Business.Employees;
using HrAspire.Employees.Web.Mappers;
using HrAspire.Web.Common;
public class EmployeesGrpcService : Employees.EmployeesBase
{
private readonly IEmployeesService employeesService;
public EmployeesGrpcService(IEmployeesService employeesService)
{
this.employeesService = employeesService;
}
public override async Task<ListEmployeesResponse> List(ListEmployeesRequest request, ServerCallContext context)
{
var employees = await this.employeesService.ListAsync(request.CurrentEmployeeId, request.PageNumber, request.PageSize);
var total = await this.employeesService.GetCountAsync(request.CurrentEmployeeId);
var response = new ListEmployeesResponse { Total = total };
foreach (var employee in employees)
{
response.Employees.Add(employee.MapToEmployeeGrpcModel());
}
return response;
}
public override async Task<ListEmployeesResponse> ListManagers(Empty request, ServerCallContext context)
{
var managers = await this.employeesService.ListManagersAsync();
var response = new ListEmployeesResponse();
foreach (var manager in managers)
{
response.Employees.Add(manager.MapToEmployeeGrpcModel());
response.Total++;
}
return response;
}
public override async Task<GetEmployeeResponse> Get(GetEmployeeRequest request, ServerCallContext context)
{
var employee = await this.employeesService.GetAsync(request.Id, request.CurrentEmployeeId);
if (employee is null)
{
throw new RpcException(new Status(StatusCode.NotFound, detail: string.Empty));
}
var response = new GetEmployeeResponse { Employee = employee.MapToEmployeeDetailsGrpcModel() };
return response;
}
public override async Task<CreateEmployeeResponse> Create(CreateEmployeeRequest request, ServerCallContext context)
{
var createResult = await this.employeesService.CreateAsync(
request.Email,
request.Password,
request.FullName,
request.DateOfBirth.ToDateOnly(),
request.Position,
request.Department,
request.ManagerId,
request.Salary,
request.Role,
request.CreatedById);
if (createResult.IsError)
{
throw createResult.ToRpcException();
}
return new CreateEmployeeResponse { Id = createResult.Data };
}
public override async Task<Empty> Update(UpdateEmployeeRequest request, ServerCallContext context)
{
var updateResult = await this.employeesService.UpdateAsync(
request.Id,
request.FullName,
request.DateOfBirth.ToDateOnly(),
request.Position,
request.Department,
request.ManagerId,
request.Role);
if (updateResult.IsError)
{
throw updateResult.ToRpcException();
}
return new Empty();
}
public override async Task<Empty> Delete(DeleteEmployeeRequest request, ServerCallContext context)
{
var deleteResult = await this.employeesService.DeleteAsync(request.Id);
if (deleteResult.IsError)
{
throw deleteResult.ToRpcException();
}
return new Empty();
}
}