diff --git a/src/Application/Staffs/Queries/GetStaffByRoom.cs b/src/Application/Staffs/Queries/GetStaffByRoom.cs index 9f1ac07b..60db1c83 100644 --- a/src/Application/Staffs/Queries/GetStaffByRoom.cs +++ b/src/Application/Staffs/Queries/GetStaffByRoom.cs @@ -1,5 +1,8 @@ +using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Staffs.Queries; @@ -9,4 +12,37 @@ public record Query : IRequest { public Guid RoomId { get; init; } } + + public class QueryHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public QueryHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + var room = await _context.Rooms + .Include(x => x.Department) + .Include(x => x.Staff) + .ThenInclude(x => x.User) + .FirstOrDefaultAsync(x => x.Id.Equals(request.RoomId), cancellationToken: cancellationToken); + + if (room is null) + { + throw new KeyNotFoundException("Room does not exist."); + } + + if (room.Staff is null) + { + throw new KeyNotFoundException("Staff does not exist."); + } + + return _mapper.Map(room.Staff); + } + } } \ No newline at end of file diff --git a/tests/Application.Tests.Integration/Staffs/Queries/GetStaffByRoomTests.cs b/tests/Application.Tests.Integration/Staffs/Queries/GetStaffByRoomTests.cs new file mode 100644 index 00000000..123e2542 --- /dev/null +++ b/tests/Application.Tests.Integration/Staffs/Queries/GetStaffByRoomTests.cs @@ -0,0 +1,97 @@ +using Application.Common.Mappings; +using Application.Common.Models.Dtos.Physical; +using Application.Identity; +using Application.Staffs.Queries; +using Application.Users.Queries; +using AutoMapper; +using Domain.Entities; +using Domain.Entities.Physical; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Staffs.Queries; + +public class GetStaffByRoomTests : BaseClassFixture +{ + private readonly IMapper _mapper; + + public GetStaffByRoomTests(CustomApiFactory apiFactory) : base(apiFactory) + { + var configuration = new MapperConfiguration(config => config.AddProfile()); + _mapper = configuration.CreateMapper(); + } + + [Fact] + public async Task ShouldReturnStaff_WhenRoomHaveStaff() + { + // Arrange + var department = CreateDepartment(); + var room = CreateRoom(department); + var user = CreateUser(IdentityData.Roles.Staff, "123123"); + var staff = CreateStaff(user, room); + await AddAsync(staff); + + var query = new GetStaffByRoom.Query() + { + RoomId = room.Id + }; + + // Act + var result = await SendAsync(query); + + // Assert + result.Should().BeEquivalentTo(_mapper.Map(staff), + config => config.Excluding(x => x.User.Created)); + result.User.Should().BeEquivalentTo(_mapper.Map(user), + config => config.Excluding(x => x.Created)); + result.Room.Should().BeEquivalentTo(_mapper.Map(room)); + + // Cleanup + Remove(staff); + Remove(await FindAsync(user.Id)); + Remove(await FindAsync(room.Id)); + Remove(await FindAsync(department.Id)); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenRoomDoesNotExist() + { + // Arrange + var query = new GetStaffByRoom.Query() + { + RoomId = Guid.NewGuid() + }; + + // Act + var action = async () => await SendAsync(query); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Room does not exist."); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenRoomDoesNotHaveStaff() + { + // Arrange + var department = CreateDepartment(); + var room = CreateRoom(department); + await AddAsync(room); + + var query = new GetStaffByRoom.Query() + { + RoomId = room.Id + }; + + // Act + var action = async () => await SendAsync(query); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Staff does not exist."); + + // Cleanup + Remove(room); + Remove(await FindAsync(department.Id)); + } +} \ No newline at end of file