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
29 changes: 29 additions & 0 deletions src/Application/Rooms/Queries/GetRoomById.cs
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
using Application.Common.Interfaces;
using Application.Common.Models.Dtos.Physical;
using AutoMapper;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Rooms.Queries;

Expand All@@ -9,4 +13,29 @@ public record Query : IRequest<RoomDto>
{
public Guid RoomId { get; init; }
}

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

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

public async Task<RoomDto> Handle(Query request, CancellationToken cancellationToken)
{
var room = await _context.Rooms
.FirstOrDefaultAsync(x => x.Id.Equals(request.RoomId), cancellationToken: cancellationToken);

if (room is null)
{
throw new KeyNotFoundException("Room does not exist.");
}

return _mapper.Map<RoomDto>(room);
}
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
using Application.Rooms.Queries;
using Domain.Entities;
using FluentAssertions;
using Xunit;

namespace Application.Tests.Integration.Rooms.Queries;

public class GetRoomByIdTests : BaseClassFixture
{
public GetRoomByIdTests(CustomApiFactory apiFactory) : base(apiFactory)
{
}

[Fact]
public async Task ShouldReturnRoom_WhenThatRoomExists()
{
// Arrange
var department = CreateDepartment();
var room = CreateRoom(department);
await AddAsync(room);

var query = new GetRoomById.Query()
{
RoomId = room.Id,
};

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

// Assert
result.Id.Should().Be(room.Id);
result.Name.Should().Be(room.Name);

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

[Fact]
public async Task ShouldThrowKeyNotFoundException_WhenThatRoomDoesNotExist()
{
// Arrange
var query = new GetRoomById.Query()
{
RoomId = Guid.NewGuid(),
};

// Act
var action = async () => await SendAsync(query);

// Assert
await action.Should().ThrowAsync<KeyNotFoundException>()
.WithMessage("Room does not exist.");
}
}