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
3 changes: 2 additions & 1 deletion src/Api/Controllers/BorrowsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public async Task<ActionResult<Result<PaginatedList<BorrowDto>>>> GetAllRequests
/// <param name="documentId"></param>
/// <param name="queryParameters"></param>
/// <returns></returns>
[RequiresRole(IdentityData.Roles.Employee)]
[RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)]
[HttpGet("documents/{documentId:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
Expand All @@ -168,6 +168,7 @@ public async Task<ActionResult<Result<PaginatedList<BorrowDto>>>> GetAllRequests
Size = queryParameters.Size,
SortBy = queryParameters.SortBy,
SortOrder = queryParameters.SortOrder,
Status = queryParameters.Status,
};
var result = await Mediator.Send(command);
return Ok(Result<PaginatedList<BorrowDto>>.Succeed(result));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace Api.Controllers.Payload.Requests.Borrows;

public class GetAllBorrowRequestsPaginatedForDocumentQueryParameters : PaginatedQueryParameters
{

public string? Status { get; set; }
}
4 changes: 3 additions & 1 deletion src/Application/Borrows/Commands/BorrowDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public async Task<BorrowDto> Handle(Command request, CancellationToken cancellat
{
// Does not make sense if the same person go up and want to borrow the same document again
// even if the borrow day will be after the due day
if (existedBorrow.Borrower.Id == request.BorrowerId)
if (existedBorrow.Borrower.Id == request.BorrowerId
&& existedBorrow.Status is BorrowRequestStatus.Pending
or BorrowRequestStatus.Approved)
{
throw new ConflictException("This document is already requested borrow from the same user.");
}
Expand Down
17 changes: 17 additions & 0 deletions src/Application/Borrows/Queries/GetAllBorrowRequestsPaginated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Application.Common.Models;
using Application.Common.Models.Dtos.Physical;
using AutoMapper;
using Domain.Statuses;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;
Expand All @@ -29,6 +30,7 @@ public record Query : IRequest<PaginatedList<BorrowDto>>
public int? Size { get; init; }
public string? SortBy { get; init; }
public string? SortOrder { get; init; }
public string? Status { get; init; }
}

public class QueryHandler : IRequestHandler<Query, PaginatedList<BorrowDto>>
Expand Down Expand Up @@ -71,6 +73,21 @@ public async Task<PaginatedList<BorrowDto>> Handle(Query request,
{
borrows = borrows.Where(x => x.Document.Id == request.DocumentId);
}

if (request.Status is not null)
{
var statuses = request.Status.Split(",");
var enums = new List<BorrowRequestStatus>();
foreach (var status in statuses)
{
if (Enum.TryParse(status, true, out BorrowRequestStatus s))
{
enums.Add(s);
}
}

borrows = borrows.Where(x => enums.Contains(x.Status));
}

var sortBy = request.SortBy;
if (sortBy is null || !sortBy.MatchesPropertyName<BorrowDto>())
Expand Down