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
1 change: 1 addition & 0 deletions src/Api/Controllers/FoldersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public async Task<ActionResult<Result<PaginatedList<FolderDto>>>> GetAllPaginate
{
RoomId = queryParameters.RoomId,
LockerId = queryParameters.LockerId,
SearchTerm = queryParameters.SearchTerm,
Page = queryParameters.Page,
Size = queryParameters.Size,
SortBy = queryParameters.SortBy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class GetAllFoldersPaginatedQueryParameters
/// </summary>
public Guid? LockerId { get; set; }
/// <summary>
/// Search term
/// </summary>
public string? SearchTerm { get; init; }
/// <summary>
/// Page number
/// </summary>
public int? Page { get; set; }
Expand Down
13 changes: 13 additions & 0 deletions src/Application/Common/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Application.Common.Extensions;

public static class StringExtensions
{
public static bool MatchesPropertyName<T>(this string input)
where T : class
{
var type = typeof(T);
var properties = type.GetProperties();

return properties.Any(property => string.Equals(property.Name, input));
}
}
92 changes: 92 additions & 0 deletions src/Application/Folders/Queries/GetAllFoldersPaginated.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,110 @@
using Application.Common.Exceptions;
using Application.Common.Extensions;
using Application.Common.Interfaces;
using Application.Common.Mappings;
using Application.Common.Models;
using Application.Common.Models.Dtos.Physical;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Folders.Queries;

public class GetAllFoldersPaginated
{
public class Validator : AbstractValidator<Query>
{
public Validator()
{
RuleLevelCascadeMode = CascadeMode.Stop;

RuleFor(x => x.RoomId)
.Must((query, roomId) => roomId is not null || query.LockerId is null);
}
}

public record Query : IRequest<PaginatedList<FolderDto>>
{
public Guid? RoomId { get; init; }
public Guid? LockerId { get; init; }
public string? SearchTerm { get; init; }
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<FolderDto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

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

public async Task<PaginatedList<FolderDto>> Handle(Query request, CancellationToken cancellationToken)
{
var folders = _context.Folders.AsQueryable();
var roomExists = request.RoomId is not null;
var lockerExists = request.LockerId is not null;

if (lockerExists)
{
var locker = await _context.Lockers
.Include(x => x.Room)
.FirstOrDefaultAsync(x => x.Id == request.LockerId
&& x.IsAvailable, cancellationToken);
if (locker is null)
{
throw new KeyNotFoundException("Locker does not exist.");
}

if (locker.Room.Id != request.RoomId)
{
throw new ConflictException("Room does not match locker.");
}

folders = folders.Where(x => x.Locker.Id == request.LockerId);
}
else if (roomExists)
{
var room = await _context.Rooms
.FirstOrDefaultAsync(x => x.Id == request.RoomId
&& x.IsAvailable, cancellationToken);
if (room is null)
{
throw new KeyNotFoundException("Room does not exist.");
}

folders = folders.Where(x => x.Locker.Room.Id == request.RoomId);
}

if (!(request.SearchTerm is null || request.SearchTerm.Trim().Equals(string.Empty)))
{
folders = folders.Where(x =>
x.Name.Contains(request.SearchTerm, StringComparison.InvariantCultureIgnoreCase));
}

var sortBy = request.SortBy;
if (sortBy is null || !sortBy.MatchesPropertyName<LockerDto>())
{
sortBy = nameof(LockerDto.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 result = await folders
.ProjectTo<FolderDto>(_mapper.ConfigurationProvider)
.OrderByCustom(sortBy, sortOrder)
.PaginatedListAsync(pageNumber.Value, sizeNumber.Value);

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using Application.Common.Mappings;
using Application.Common.Models.Dtos.Physical;
using Application.Folders.Queries;
using AutoMapper;
using FluentAssertions;
using Xunit;

namespace Application.Tests.Integration.Folders.Queries;

public class GetAllFoldersPaginatedTests : BaseClassFixture
{
private readonly IMapper _mapper;
public GetAllFoldersPaginatedTests(CustomApiFactory apiFactory) : base(apiFactory)
{
var configuration = new MapperConfiguration(config => config.AddProfile<MappingProfile>());

_mapper = configuration.CreateMapper();
}

[Fact]
public async Task ShouldReturnFolders()
{
// Arrange
var department = CreateDepartment();
var folder1 = CreateFolder();
var folder2 = CreateFolder();
var folder3 = CreateFolder();
var locker1 = CreateLocker(folder1);
var locker2 = CreateLocker(folder2, folder3);
var room = CreateRoom(department, locker1, locker2);
await AddAsync(room);

var query = new GetAllFoldersPaginated.Query();

// Act
var result = await SendAsync(query);

// Assert
result.TotalCount.Should().Be(3);
result.Items.Should().BeEquivalentTo(_mapper.Map<FolderDto[]>(new[] { folder1, folder2, folder3 })
.OrderBy(x => x.Id), config => config.IgnoringCyclicReferences());
result.Items.Should().BeInAscendingOrder(x => x.Id);

// Cleanup
Remove(folder1);
Remove(folder2);
Remove(folder3);
Remove(locker1);
Remove(locker2);
Remove(room);
Remove(department);
}

[Fact]
public async Task ShouldReturnFoldersOfASpecificLocker()
{
// Arrange
var department = CreateDepartment();
var folder1 = CreateFolder();
var folder2 = CreateFolder();
var folder3 = CreateFolder();
var locker1 = CreateLocker(folder1);
var locker2 = CreateLocker(folder2, folder3);
var room = CreateRoom(department, locker1, locker2);
await AddAsync(room);

var query = new GetAllFoldersPaginated.Query()
{
RoomId = room.Id,
LockerId = locker2.Id,
};

// Act
var result = await SendAsync(query);

// Assert
result.TotalCount.Should().Be(2);
result.Items.Should().BeEquivalentTo(_mapper.Map<FolderDto[]>(new[] { folder2, folder3 })
.OrderBy(x => x.Id), config => config.IgnoringCyclicReferences());
result.Items.Should().BeInAscendingOrder(x => x.Id);

// Cleanup
Remove(folder1);
Remove(folder2);
Remove(folder3);
Remove(locker1);
Remove(locker2);
Remove(room);
Remove(department);
}

[Fact]
public async Task ShouldReturnNothing_WhenWrongPaginationDetailsAreProvided()
{
// Arrange
var department = CreateDepartment();
var folder1 = CreateFolder();
var folder2 = CreateFolder();
var folder3 = CreateFolder();
var locker1 = CreateLocker(folder1);
var locker2 = CreateLocker(folder2, folder3);
var room = CreateRoom(department, locker1, locker2);
await AddAsync(room);

var query = new GetAllFoldersPaginated.Query()
{
RoomId = room.Id,
LockerId = locker2.Id,
Page = -1,
Size = -4,
};

// Act
var result = await SendAsync(query);

// Assert
result.TotalCount.Should().Be(2);
result.Items.Should().BeEquivalentTo(_mapper.Map<FolderDto[]>(new[] { folder2, folder3 })
.OrderBy(x => x.Id), config => config.IgnoringCyclicReferences());
result.Items.Should().BeInAscendingOrder(x => x.Id);

// Cleanup
Remove(folder1);
Remove(folder2);
Remove(folder3);
Remove(locker1);
Remove(locker2);
Remove(room);
Remove(department);
}
}