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
26 changes: 26 additions & 0 deletions src/Api/Controllers/DocumentsController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -269,6 +269,32 @@ public async Task<ActionResult<Result<DocumentDto>>> Delete([FromRoute] Guid doc
var result = await Mediator.Send(query);
return Ok(Result<DocumentDto>.Succeed(result));
}

/// <summary>
/// Get all documents of a user.
/// </summary>
/// <param name="userId">Id of the user</param>
/// <param name="queryParameters">Query parameters</param>
/// <returns>A list of DocumentDtos of the user.</returns>
[RequiresRole(IdentityData.Roles.Employee)]
[HttpGet("user/{userId:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<Result<DocumentDto>>> GetDocumentsOfUserPaginated([FromRoute] Guid userId,
[FromQuery] GetDocumentsOfUserPaginatedQueryParameters queryParameters)
{
var query = new GetDocumentsOfUserPaginated.Query()
{
UserId = userId,
Page = queryParameters.Page,
Size = queryParameters.Size,
SortBy = queryParameters.SortBy,
SortOrder = queryParameters.SortOrder,
};
var result = await Mediator.Send(query);
return Ok(Result<PaginatedList<DocumentDto>>.Succeed(result));
}

/// <summary>
/// Approve a document request
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
namespace Api.Controllers.Payload.Requests.Documents;

public class GetDocumentsOfUserPaginatedQueryParameters : PaginatedQueryParameters
{

}
70 changes: 70 additions & 0 deletions src/Application/Documents/Queries/GetDocumentsOfUserPaginated.cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 GetDocumentsOfUserPaginated
{
public record Query : IRequest<PaginatedList<DocumentDto>>
{
public Guid UserId { get; set; }
public int? Page { get; init; }
public int? Size { get; init; }
public string? SortBy { get; init; }
public string? SortOrder { get; init; }
}

public class Handler : IRequestHandler<Query, PaginatedList<DocumentDto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

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

public async Task<PaginatedList<DocumentDto>> Handle(Query request, CancellationToken cancellationToken)
{
var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == request.UserId
&& x.IsActive
&& x.IsActivated, cancellationToken);

if (user is null)
{
throw new KeyNotFoundException("User does not exist.");
}

var documents = _context.Documents
.Include(x => x.Department)
.Include(x => x.Folder)
.AsQueryable()
.Where(x => x.Importer!.Id.Equals(request.UserId) && !x.IsPrivate);

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
.Paginate(pageNumber.Value, sizeNumber.Value)
.OrderByCustom(sortBy, sortOrder)
.ToListAsync(cancellationToken);

var result = _mapper.Map<List<DocumentDto>>(list);

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