From 4557159729307fbec67c1f803a1cc967969eb7f1 Mon Sep 17 00:00:00 2001 From: Vzart Date: Tue, 13 Jun 2023 20:33:46 +0700 Subject: [PATCH 1/2] feat: get self documents --- src/Api/Controllers/DocumentsController.cs | 30 +++++++- ...etSelfDocumentsPaginatedQueryParameters.cs | 9 +++ .../Queries/GetSelfDocumentsPaginated.cs | 68 +++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/Api/Controllers/Payload/Requests/Documents/GetSelfDocumentsPaginatedQueryParameters.cs create mode 100644 src/Application/Documents/Queries/GetSelfDocumentsPaginated.cs diff --git a/src/Api/Controllers/DocumentsController.cs b/src/Api/Controllers/DocumentsController.cs index 9b7931ca..a13da3e8 100644 --- a/src/Api/Controllers/DocumentsController.cs +++ b/src/Api/Controllers/DocumentsController.cs @@ -44,6 +44,7 @@ public async Task>> GetById([FromRoute] Guid do /// Get all documents query parameters /// A paginated list of DocumentDto [HttpGet] + [RequiresRole(IdentityData.Roles.Admin)] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -65,7 +66,34 @@ public async Task>>> GetAllPagina var result = await Mediator.Send(query); return Ok(Result>.Succeed(result)); } - + + /// + /// Get documents of the employee + /// + /// + /// + [HttpGet("get-self-documents/{employeeId:guid}")] + [RequiresRole(IdentityData.Roles.Employee)] + [ProducesResponseType(StatusCodes.Status200OK)] + public async Task>>> GetSelfPaginated( + [FromQuery] GetSelfDocumentsPaginatedQueryParameters queryParameters) + { + var userId = _currentUserService.GetId(); + + var query = new GetSelfDocumentsPaginated.Query() + { + EmployeeId = userId, + Page = queryParameters.Page, + Size = queryParameters.Size, + SortBy = queryParameters.SortBy, + SearchTerm = queryParameters.SearchTerm, + SortOrder = queryParameters.SortOrder + }; + + var result = await Mediator.Send(query); + return Ok(Result>.Succeed(result)); + } + /// /// Get all document types /// diff --git a/src/Api/Controllers/Payload/Requests/Documents/GetSelfDocumentsPaginatedQueryParameters.cs b/src/Api/Controllers/Payload/Requests/Documents/GetSelfDocumentsPaginatedQueryParameters.cs new file mode 100644 index 00000000..3f7d6472 --- /dev/null +++ b/src/Api/Controllers/Payload/Requests/Documents/GetSelfDocumentsPaginatedQueryParameters.cs @@ -0,0 +1,9 @@ +namespace Api.Controllers.Payload.Requests.Documents; + +/// +/// Query parameters for getting all documents that belong to an employee +/// +public class GetSelfDocumentsPaginatedQueryParameters : PaginatedQueryParameters +{ + public string? SearchTerm { get; set; } +} \ No newline at end of file diff --git a/src/Application/Documents/Queries/GetSelfDocumentsPaginated.cs b/src/Application/Documents/Queries/GetSelfDocumentsPaginated.cs new file mode 100644 index 00000000..ceebbed1 --- /dev/null +++ b/src/Application/Documents/Queries/GetSelfDocumentsPaginated.cs @@ -0,0 +1,68 @@ +using Application.Common.Extensions; +using Application.Common.Interfaces; +using Application.Common.Models; +using Application.Common.Models.Dtos.Physical; +using AutoMapper; +using MediatR; +using Microsoft.EntityFrameworkCore; + +namespace Application.Documents.Queries; + +public class GetSelfDocumentsPaginated +{ + public record Query : IRequest> + { + public Guid EmployeeId { get; init; } + public string? SearchTerm { get; set; } + public int? Page { get; init; } + public int? Size { get; init; } + public string? SortBy { get; init; } + public string? SortOrder { get; init; } + + public class QueryHandler : IRequestHandler> + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public QueryHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task> Handle(Query request, CancellationToken cancellationToken) + { + var documents = _context.Documents.AsQueryable(); + + documents = documents + .Include(x => x.Department) + .Where(x => x.Importer!.Id.Equals(request.EmployeeId)); + + if (!(request.SearchTerm is null || request.SearchTerm.Trim().Equals(string.Empty))) + { + documents = documents.Where(x => + x.Title.ToLower().Contains(request.SearchTerm.ToLower())); + } + + var sortBy = request.SortBy; + if (sortBy is null || !sortBy.MatchesPropertyName()) + { + sortBy = nameof(DocumentDto.Id); + } + var sortOrder = request.SortOrder ?? "asc"; + var pageNumber = request.Page is null or <= 0 ? 1 : request.Page; + var sizeNumber = request.Size is null or <= 0 ? 5 : request.Size; + + var count = await documents.CountAsync(cancellationToken); + var list = await documents + .OrderByCustom(sortBy, sortOrder) + .Paginate(pageNumber.Value, sizeNumber.Value) + .ToListAsync(cancellationToken); + + var result = _mapper.Map>(list); + + return new PaginatedList(result, count, pageNumber.Value, sizeNumber.Value); + } + } + } +} \ No newline at end of file From a63e36fc0a63dc9d633c19c76036e05e2ed49254 Mon Sep 17 00:00:00 2001 From: Vzart Date: Tue, 13 Jun 2023 20:42:07 +0700 Subject: [PATCH 2/2] fix: minor stuff --- src/Api/Controllers/DocumentsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/Controllers/DocumentsController.cs b/src/Api/Controllers/DocumentsController.cs index a13da3e8..b4a99d60 100644 --- a/src/Api/Controllers/DocumentsController.cs +++ b/src/Api/Controllers/DocumentsController.cs @@ -72,7 +72,7 @@ public async Task>>> GetAllPagina /// /// /// - [HttpGet("get-self-documents/{employeeId:guid}")] + [HttpGet("get-self-documents")] [RequiresRole(IdentityData.Roles.Employee)] [ProducesResponseType(StatusCodes.Status200OK)] public async Task>>> GetSelfPaginated(