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
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,10 @@ namespace Api.Controllers.Payload.Requests.Rooms;
/// </summary>
public class GetAllRoomsPaginatedQueryParameters
{
/// <summary>
/// Search term
/// </summary>
public string? SearchTerm { get; set; }
/// <summary>
/// Page number
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Api/Controllers/RoomsController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,14 +36,16 @@ public async Task<ActionResult<Result<RoomDto>>> GetById([FromRoute] Guid roomId
/// </summary>
/// <param name="queryParameters">Get all rooms paginated details</param>
/// <returns>A paginated list of rooms</returns>
[RequiresRole(IdentityData.Roles.Admin)]
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult<Result<PaginatedList<RoomDto>>>> GetAllPaginated(
[FromQuery] GetAllLockersPaginatedQueryParameters queryParameters)
[FromQuery] GetAllRoomsPaginatedQueryParameters queryParameters)
{
var query = new GetAllRoomsPaginated.Query()
{
SearchTerm = queryParameters.SearchTerm,
Page = queryParameters.Page,
Size = queryParameters.Size,
SortBy = queryParameters.SortBy,
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));
}
}
45 changes: 45 additions & 0 deletions src/Application/Rooms/Queries/GetAllRoomsPaginated.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.Rooms.Queries;
Expand All@@ -8,9 +13,49 @@ public class GetAllRoomsPaginated
{
public record Query : IRequest<PaginatedList<RoomDto>>
{
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<RoomDto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

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

public async Task<PaginatedList<RoomDto>> Handle(Query request, CancellationToken cancellationToken)
{
var rooms = _context.Rooms.AsQueryable();

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

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

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

namespace Application.Tests.Integration.Rooms.Queries;

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

_mapper = configuration.CreateMapper();
}

[Fact]
public async Task ShouldReturnAllRooms()
{
// Arrange
var department1 = CreateDepartment();
var department2 = CreateDepartment();
var room1 = CreateRoom(department1);
var room2 = CreateRoom(department2);
await AddAsync(room1);
await AddAsync(room2);

var query = new GetAllRoomsPaginated.Query();

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

// Assert
result.TotalCount.Should().Be(2);
result.Items.Should()
.ContainEquivalentOf(_mapper.Map<RoomDto>(room1),
config => config.IgnoringCyclicReferences());
result.Items.Should()
.ContainEquivalentOf(_mapper.Map<RoomDto>(room2),
config => config.IgnoringCyclicReferences());

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

[Fact]
public async Task ShouldReturnOrderById_WhenWrongSortByIsProvided()
{
// Arrange
var department1 = CreateDepartment();
var department2 = CreateDepartment();
var room1 = CreateRoom(department1);
var room2 = CreateRoom(department2);
await AddAsync(room1);
await AddAsync(room2);

var query = new GetAllRoomsPaginated.Query()
{
SortBy = "e",
};

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

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

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