From 6a46fb3d672ab807c47284b872a923caebc58748 Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Mon, 29 May 2023 18:01:12 +0700 Subject: [PATCH 1/3] add: integration test --- .../Rooms/Queries/GetRoomByIdTests.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/Application.Tests.Integration/Rooms/Queries/GetRoomByIdTests.cs diff --git a/tests/Application.Tests.Integration/Rooms/Queries/GetRoomByIdTests.cs b/tests/Application.Tests.Integration/Rooms/Queries/GetRoomByIdTests.cs new file mode 100644 index 00000000..1f3ee018 --- /dev/null +++ b/tests/Application.Tests.Integration/Rooms/Queries/GetRoomByIdTests.cs @@ -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.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() + .WithMessage("Room does not exist."); + } +} \ No newline at end of file From 01e57c34619d3d8d6fef3941ecaef4a6fb7097a8 Mon Sep 17 00:00:00 2001 From: Vzart Date: Mon, 29 May 2023 20:31:45 +0700 Subject: [PATCH 2/3] feat: get room by id --- src/Application/Rooms/Queries/GetRoomById.cs | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/Application/Rooms/Queries/GetRoomById.cs b/src/Application/Rooms/Queries/GetRoomById.cs index 6a3657bd..ab29c8df 100644 --- a/src/Application/Rooms/Queries/GetRoomById.cs +++ b/src/Application/Rooms/Queries/GetRoomById.cs @@ -1,12 +1,51 @@ +using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; +using AutoMapper; +using FluentValidation; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Rooms.Queries; public class GetRoomById { + public class Validator : AbstractValidator + { + public Validator() + { + RuleLevelCascadeMode = CascadeMode.Stop; + + RuleFor(x => x.RoomId) + .NotEmpty().WithMessage("RoomId is required."); + } + } public record Query : IRequest { public Guid RoomId { get; init; } } + + public class GetRoomByIdHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public GetRoomByIdHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task 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(room); + } + } } \ No newline at end of file From 95f565c87eb183dc3ecfec3674faa6612b9b051e Mon Sep 17 00:00:00 2001 From: Vzart Date: Mon, 29 May 2023 20:57:00 +0700 Subject: [PATCH 3/3] refactor: remove unesscary validator and refactor name of handler --- src/Application/Rooms/Queries/GetRoomById.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/Application/Rooms/Queries/GetRoomById.cs b/src/Application/Rooms/Queries/GetRoomById.cs index ab29c8df..6e834b68 100644 --- a/src/Application/Rooms/Queries/GetRoomById.cs +++ b/src/Application/Rooms/Queries/GetRoomById.cs @@ -9,27 +9,17 @@ namespace Application.Rooms.Queries; public class GetRoomById { - public class Validator : AbstractValidator - { - public Validator() - { - RuleLevelCascadeMode = CascadeMode.Stop; - - RuleFor(x => x.RoomId) - .NotEmpty().WithMessage("RoomId is required."); - } - } public record Query : IRequest { public Guid RoomId { get; init; } } - public class GetRoomByIdHandler : IRequestHandler + public class QueryHandler : IRequestHandler { private readonly IApplicationDbContext _context; private readonly IMapper _mapper; - public GetRoomByIdHandler(IApplicationDbContext context, IMapper mapper) + public QueryHandler(IApplicationDbContext context, IMapper mapper) { _context = context; _mapper = mapper;