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/LockersController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,6 +44,7 @@ public async Task<ActionResult<Result<PaginatedList<LockerDto>>>> GetAllPaginate
var query = new GetAllLockersPaginated.Query()
{
RoomId = queryParameters.RoomId,
SearchTerm = queryParameters.SearchTerm,
Page = queryParameters.Page,
Size = queryParameters.Size,
SortBy = queryParameters.SortBy,
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,10 @@ public class GetAllLockersPaginatedQueryParameters
/// </summary>
public Guid? RoomId { get; set; }
/// <summary>
/// Search term
/// </summary>
public string? SearchTerm { get; set; }
/// <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 numberDiff line numberDiff 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));
}
}
50 changes: 50 additions & 0 deletions src/Application/Lockers/Queries/GetAllLockersPaginated.cs
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
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 MediatR;

namespace Application.Lockers.Queries;
Expand All@@ -9,9 +14,54 @@ public class GetAllLockersPaginated
public record Query : IRequest<PaginatedList<LockerDto>>
{
public Guid? RoomId { 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<LockerDto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

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

public async Task<PaginatedList<LockerDto>> Handle(Query request, CancellationToken cancellationToken)
{
var lockers = _context.Lockers.AsQueryable();

if (request.RoomId is not null)
{
lockers = lockers.Where(x => x.Room.Id == request.RoomId);
}

if (!(request.SearchTerm is null || request.SearchTerm.Trim().Equals(string.Empty)))
{
lockers = lockers.Where(x =>
x.Name.ToLower().Contains(request.SearchTerm.ToLower()));
}

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 lockers
.ProjectTo<LockerDto>(_mapper.ConfigurationProvider)
.OrderByCustom(sortBy, sortOrder)
.PaginatedListAsync(pageNumber.Value, sizeNumber.Value);

return result;
}
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
using Application.Common.Mappings;
using Application.Common.Models.Dtos.Physical;
using Application.Lockers.Queries;
using AutoMapper;
using FluentAssertions;
using Xunit;

namespace Application.Tests.Integration.Lockers.Queries;

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

_mapper = configuration.CreateMapper();
}

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

var query = new GetAllLockersPaginated.Query();

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

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

// Cleanup
Remove(locker1);
Remove(locker2);
Remove(room);
Remove(department);
}

[Fact]
public async Task ShouldReturnLockersOfOneRoom_WhenSpecifyIdOfThatRoom()
{
// Arrange
var department1 = CreateDepartment();
var department2 = CreateDepartment();
var locker1 = CreateLocker();
var locker2 = CreateLocker();
var room1 = CreateRoom(department1, locker1, locker2);
var locker3 = CreateLocker();
var locker4 = CreateLocker();
var room2 = CreateRoom(department2, locker3, locker4);
await AddAsync(room1);
await AddAsync(room2);

var query = new GetAllLockersPaginated.Query()
{
RoomId = room1.Id,
};

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

// Assert
result.TotalCount.Should().Be(2);
result.Items.Should().ContainEquivalentOf(_mapper.Map<LockerDto>(locker1),
config => config.IgnoringCyclicReferences());
result.Items.Should().ContainEquivalentOf(_mapper.Map<LockerDto>(locker2),
config => config.IgnoringCyclicReferences());
result.Items.Should().NotContainEquivalentOf(_mapper.Map<LockerDto>(locker3),
config => config.IgnoringCyclicReferences());
result.Items.Should().NotContainEquivalentOf(_mapper.Map<LockerDto>(locker4),
config => config.IgnoringCyclicReferences());

// Cleanup
Remove(locker1);
Remove(locker2);
Remove(room1);
Remove(room2);
Remove(department1);
Remove(department2);
}
}