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
36 changes: 36 additions & 0 deletions src/Application/Staffs/Queries/GetStaffByRoom.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,4 +12,37 @@ public record Query : IRequest<StaffDto>
{
public Guid RoomId { get; init; }
}

public class QueryHandler : IRequestHandler<Query, StaffDto>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

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

public async Task<StaffDto> 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<StaffDto>(room.Staff);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<MappingProfile>());
_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<StaffDto>(staff),
config => config.Excluding(x => x.User.Created));
result.User.Should().BeEquivalentTo(_mapper.Map<UserDto>(user),
config => config.Excluding(x => x.Created));
result.Room.Should().BeEquivalentTo(_mapper.Map<RoomDto>(room));

// Cleanup
Remove(staff);
Remove(await FindAsync<User>(user.Id));
Remove(await FindAsync<Room>(room.Id));
Remove(await FindAsync<Department>(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<KeyNotFoundException>()
.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<KeyNotFoundException>()
.WithMessage("Staff does not exist.");

// Cleanup
Remove(room);
Remove(await FindAsync<Department>(department.Id));
}
}