From c39a0625ee904843d3571740e72feb0d11463cf8 Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Mon, 29 May 2023 20:58:16 +0700 Subject: [PATCH 1/2] add: integration test --- .../Lockers/Queries/GetLockerByIdTests.cs | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/Application.Tests.Integration/Lockers/Queries/GetLockerByIdTests.cs diff --git a/tests/Application.Tests.Integration/Lockers/Queries/GetLockerByIdTests.cs b/tests/Application.Tests.Integration/Lockers/Queries/GetLockerByIdTests.cs new file mode 100644 index 00000000..be791603 --- /dev/null +++ b/tests/Application.Tests.Integration/Lockers/Queries/GetLockerByIdTests.cs @@ -0,0 +1,57 @@ +using Application.Lockers.Queries; +using Domain.Entities; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Lockers.Queries; + +public class GetLockerByIdTests : BaseClassFixture +{ + public GetLockerByIdTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldReturnLocker_WhenThatLockerExists() + { + // Arrange + var department = CreateDepartment(); + var locker = CreateLocker(); + var room = CreateRoom(department, locker); + await AddAsync(room); + + var query = new GetLockerById.Query() + { + LockerId = locker.Id, + }; + + // Act + var result = await SendAsync(query); + + // Assert + result.Id.Should().Be(locker.Id); + result.Name.Should().Be(locker.Name); + + // Cleanup + Remove(locker); + Remove(room); + Remove(await FindAsync(department.Id)); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenThatLockerDoesNotExist() + { + // Arrange + var query = new GetLockerById.Query() + { + LockerId = Guid.NewGuid(), + }; + + // Act + var action = async () => await SendAsync(query); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Locker does not exist."); + } +} \ No newline at end of file From 6be0d845ab4a60adc7c313e69044cfc0e1fdfd39 Mon Sep 17 00:00:00 2001 From: StarryFolf Date: Thu, 1 Jun 2023 21:24:13 +0700 Subject: [PATCH 2/2] feat: implement get locker my id --- .../Lockers/Queries/GetLockerById.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Application/Lockers/Queries/GetLockerById.cs b/src/Application/Lockers/Queries/GetLockerById.cs index 65c31416..8077335f 100644 --- a/src/Application/Lockers/Queries/GetLockerById.cs +++ b/src/Application/Lockers/Queries/GetLockerById.cs @@ -1,5 +1,8 @@ +using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Lockers.Queries; @@ -9,4 +12,28 @@ public record Query : IRequest { public Guid LockerId { 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 locker = await _context.Lockers.FirstOrDefaultAsync(x => x.Id.Equals(request.LockerId), cancellationToken); + + if (locker is null) + { + throw new KeyNotFoundException("Locker does not exist."); + } + + return _mapper.Map(locker); + } + } } \ No newline at end of file