diff --git a/src/Application/Folders/Queries/GetFolderById.cs b/src/Application/Folders/Queries/GetFolderById.cs index fd0d0b57..9b1d07df 100644 --- a/src/Application/Folders/Queries/GetFolderById.cs +++ b/src/Application/Folders/Queries/GetFolderById.cs @@ -1,5 +1,8 @@ +using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Folders.Queries; @@ -9,4 +12,28 @@ public record Query : IRequest { public Guid FolderId { 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 folder = await _context.Folders.FirstOrDefaultAsync(x => x.Id.Equals(request.FolderId), cancellationToken); + + if (folder is null) + { + throw new KeyNotFoundException("Folder does not exist."); + } + + return _mapper.Map(folder); + } + } } \ No newline at end of file diff --git a/tests/Application.Tests.Integration/Folders/Queries/GetFolderByIdTests.cs b/tests/Application.Tests.Integration/Folders/Queries/GetFolderByIdTests.cs new file mode 100644 index 00000000..d4d078b6 --- /dev/null +++ b/tests/Application.Tests.Integration/Folders/Queries/GetFolderByIdTests.cs @@ -0,0 +1,59 @@ +using Application.Folders.Queries; +using Domain.Entities; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Folders.Queries; + +public class GetFolderByIdTests : BaseClassFixture +{ + public GetFolderByIdTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldReturnFolder_WhenThatFolderExists() + { + // Arrange + var department = CreateDepartment(); + var folder = CreateFolder(); + var locker = CreateLocker(folder); + var room = CreateRoom(department, locker); + await AddAsync(room); + + var query = new GetFolderById.Query() + { + FolderId = folder.Id, + }; + + // Act + var result = await SendAsync(query); + + // Assert + result.Id.Should().Be(folder.Id); + result.Name.Should().Be(folder.Name); + + // Cleanup + Remove(folder); + Remove(locker); + Remove(room); + Remove(await FindAsync(department.Id)); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenThatFolderDoesNotExist() + { + // Arrange + var query = new GetFolderById.Query() + { + FolderId = Guid.NewGuid(), + }; + + // Act + var action = async () => await SendAsync(query); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Folder does not exist."); + } +} \ No newline at end of file