diff --git a/src/Application/Folders/Commands/RemoveFolder.cs b/src/Application/Folders/Commands/RemoveFolder.cs index 528184ce..30fb5776 100644 --- a/src/Application/Folders/Commands/RemoveFolder.cs +++ b/src/Application/Folders/Commands/RemoveFolder.cs @@ -1,5 +1,11 @@ +using Application.Common.Exceptions; +using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; +using AutoMapper; +using Domain.Entities.Physical; +using FluentValidation; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Folders.Commands; @@ -9,4 +15,42 @@ public record Command : IRequest { public Guid FolderId { get; init; } } + + public class CommandHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public CommandHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task Handle(Command request, CancellationToken cancellationToken) + { + var folder = await _context.Folders + .Include(x => x.Locker) + .FirstOrDefaultAsync(x => x.Id.Equals(request.FolderId), cancellationToken); + + if (folder is null) + { + throw new KeyNotFoundException("Folder does not exist."); + } + + var containDocument = folder.NumberOfDocuments > 0; + + if (containDocument) + { + throw new ConflictException("Folder cannot be removed because it contains documents."); + } + + var locker = folder.Locker; + var result = _context.Folders.Remove(folder); + locker.NumberOfFolders -= 1; + + await _context.SaveChangesAsync(cancellationToken); + return _mapper.Map(result.Entity); + } + } } \ No newline at end of file diff --git a/tests/Application.Tests.Integration/Folders/Commands/RemoveFolderTests.cs b/tests/Application.Tests.Integration/Folders/Commands/RemoveFolderTests.cs new file mode 100644 index 00000000..a03af6b3 --- /dev/null +++ b/tests/Application.Tests.Integration/Folders/Commands/RemoveFolderTests.cs @@ -0,0 +1,94 @@ +using Application.Common.Exceptions; +using Application.Folders.Commands; +using Domain.Entities; +using Domain.Entities.Physical; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Folders.Commands; + +public class RemoveFolderTests : BaseClassFixture +{ + public RemoveFolderTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldRemoveFolder_WhenFolderHasNoDocuments() + { + // Arrange + var department = CreateDepartment(); + var folder = CreateFolder(); + var locker = CreateLocker(folder); + var room = CreateRoom(department, locker); + await Add(room); + + var command = new RemoveFolder.Command() + { + FolderId = folder.Id, + }; + + // Act + var result = await SendAsync(command); + + // Assert + result.Id.Should().Be(folder.Id); + var removedFolder = await FindAsync(folder.Id); + removedFolder.Should().BeNull(); + var lockerOfRemovedFolder = await FindAsync(locker.Id); + locker.NumberOfFolders.Should().Be(lockerOfRemovedFolder!.NumberOfFolders + 1); + + // Cleanup + Remove(lockerOfRemovedFolder); + Remove(await FindAsync(room.Id)); + Remove(await FindAsync(department.Id)); + } + + [Fact] + public async Task ShouldThrowConflictException_WhenFolderStillHasDocuments() + { + // Arrange + var department = CreateDepartment(); + var documents = CreateNDocuments(1); + var folder = CreateFolder(documents); + var locker = CreateLocker(folder); + var room = CreateRoom(department, locker); + await AddAsync(room); + + var command = new RemoveFolder.Command() + { + FolderId = folder.Id + }; + + // Act + var action = async () => await SendAsync(command); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Folder cannot be removed because it contains documents."); + + // Cleanup + Remove(documents.First()); + Remove(folder); + Remove(locker); + Remove(room); + Remove(await FindAsync(department.Id)); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenFolderDoesNotExist() + { + // Arrange + var command = new RemoveFolder.Command() + { + FolderId = Guid.NewGuid(), + }; + + // Act + var action = async () => await SendAsync(command); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Folder does not exist."); + } +} \ No newline at end of file