-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetEmployeesQuery.cs
More file actions
71 lines (63 loc) · 2.92 KB
/
Copy pathGetEmployeesQuery.cs
File metadata and controls
71 lines (63 loc) · 2.92 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
namespace TalentManagementAPI.Application.Features.Employees.Queries.GetEmployees
{
/// <summary>
/// GetAllEmployeesQuery - handles media IRequest
/// BaseRequestParameter - contains paging parameters
/// To add filter/search parameters, add search properties to the body of this class
/// </summary>
public class GetEmployeesQuery : QueryParameter, IRequest<PagedResult<IEnumerable<Entity>>>
{
//examples:
public string LastName { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
public string EmployeeNumber { get; set; }
public string PositionTitle { get; set; }
public ListParameter ShapeParameter { get; set; }
}
public class GetAllEmployeesQueryHandler : IRequestHandler<GetEmployeesQuery, PagedResult<IEnumerable<Entity>>>
{
private readonly IEmployeeRepositoryAsync _repository;
private readonly IModelHelper _modelHelper;
/// <summary>
/// Constructor for GetAllEmployeesQueryHandler class.
/// </summary>
/// <param name="employeeRepository">IEmployeeRepositoryAsync object.</param>
/// <param name="modelHelper">IModelHelper object.</param>
/// <returns>
/// GetAllEmployeesQueryHandler object.
/// </returns>
public GetAllEmployeesQueryHandler(IEmployeeRepositoryAsync employeeRepository, IModelHelper modelHelper)
{
_repository = employeeRepository;
_modelHelper = modelHelper;
}
/// <summary>
/// Handles the GetEmployeesQuery request and returns a PagedResult containing the requested data.
/// </summary>
/// <param name="request">The GetEmployeesQuery request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A PagedResult containing the requested data.</returns>
public async Task<PagedResult<IEnumerable<Entity>>> Handle(GetEmployeesQuery request, CancellationToken cancellationToken)
{
var objRequest = request;
//filtered fields security
if (!string.IsNullOrEmpty(objRequest.Fields))
{
//limit to fields in view model
objRequest.Fields = _modelHelper.ValidateModelFields<GetEmployeesViewModel>(objRequest.Fields);
}
else
{
//default fields from view model
objRequest.Fields = _modelHelper.GetModelFields<GetEmployeesViewModel>();
}
// query based on filter
var qryResult = await _repository.GetEmployeeResponseAsync(objRequest);
var data = qryResult.data;
RecordsCount recordCount = qryResult.recordsCount;
// response wrapper
return PagedResult<IEnumerable<Entity>>.Success(data, objRequest.PageNumber, objRequest.PageSize, recordCount);
}
}
}