Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Api/Controllers/DocumentsController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,6 +102,7 @@ public async Task<ActionResult<Result<DocumentLogDto>>> GetLogById([FromRoute] G
/// <returns>A paginated list of DocumentDto</returns>
[RequiresRole(IdentityData.Roles.Admin)]
[HttpGet]
[RequiresRole(IdentityData.Roles.Admin)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
Expand All@@ -125,6 +126,32 @@ public async Task<ActionResult<Result<PaginatedList<DocumentDto>>>> GetAllForAdm
}

/// <summary>
/// Get documents of the employee
/// </summary>
/// <param name="queryParameters"></param>
/// <returns></returns>
[HttpGet("get-self-documents")]
[RequiresRole(IdentityData.Roles.Employee)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<Result<PaginatedList<DocumentDto>>>> 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<PaginatedList<DocumentDto>>.Succeed(result));
}

/// Get all log of document
/// </summary>
/// <param name="queryParameters"></param>
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
namespace Api.Controllers.Payload.Requests.Documents;

/// <summary>
/// Query parameters for getting all documents that belong to an employee
/// </summary>
public class GetSelfDocumentsPaginatedQueryParameters : PaginatedQueryParameters
{
public string? SearchTerm { get; set; }
}
68 changes: 68 additions & 0 deletions src/Application/Documents/Queries/GetSelfDocumentsPaginated.cs
Original file line numberDiff line numberDiff line change
@@ -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<PaginatedList<DocumentDto>>
{
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<Query, PaginatedList<DocumentDto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

public QueryHandler(IApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}

public async Task<PaginatedList<DocumentDto>> 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<DocumentDto>())
{
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<DocumentDto>>(list);

return new PaginatedList<DocumentDto>(result, count, pageNumber.Value, sizeNumber.Value);
}
}
}
}