diff --git a/src/Application/Folders/Commands/EnableFolder.cs b/src/Application/Folders/Commands/EnableFolder.cs index 33e3cb23..2cd66702 100644 --- a/src/Application/Folders/Commands/EnableFolder.cs +++ b/src/Application/Folders/Commands/EnableFolder.cs @@ -1,5 +1,9 @@ +using Application.Common.Exceptions; +using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Folders.Commands; @@ -9,4 +13,37 @@ 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 + .FirstOrDefaultAsync(x => x.Id.Equals(request.FolderId), cancellationToken); + + if (folder is null) + { + throw new KeyNotFoundException("Folder does not exist."); + } + + if (folder.IsAvailable) + { + throw new ConflictException("Folder has already been enabled."); + } + + folder.IsAvailable = true; + var result = _context.Folders.Update(folder); + await _context.SaveChangesAsync(cancellationToken); + return _mapper.Map(result.Entity); + } + } } \ No newline at end of file diff --git a/tests/Application.Tests.Integration/Folders/Commands/EnableFolderTests.cs b/tests/Application.Tests.Integration/Folders/Commands/EnableFolderTests.cs new file mode 100644 index 00000000..62f0d165 --- /dev/null +++ b/tests/Application.Tests.Integration/Folders/Commands/EnableFolderTests.cs @@ -0,0 +1,90 @@ +using Application.Common.Exceptions; +using Application.Folders.Commands; +using Domain.Entities; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Folders.Commands; + +public class EnableFolderTests : BaseClassFixture +{ + public EnableFolderTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldEnableFolder_WhenThatFolderExistsAndIsDisabled() + { + // Arrange + var department = CreateDepartment(); + var folder = CreateFolder(); + var locker = CreateLocker(folder); + var room = CreateRoom(department, locker); + folder.IsAvailable = false; + await AddAsync(room); + + var command = new EnableFolder.Command() + { + FolderId = folder.Id, + }; + + // Act + var result = await SendAsync(command); + + // Assert + result.IsAvailable.Should().BeTrue(); + + // Cleanup + Remove(folder); + Remove(locker); + Remove(room); + Remove(await FindAsync(department.Id)); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenThatFolderDoesNotExist() + { + // Arrange + var command = new EnableFolder.Command() + { + FolderId = Guid.NewGuid(), + }; + + // Act + var action = async () => await SendAsync(command); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Folder does not exist."); + } + + [Fact] + public async Task ShouldThrowConflictException_WhenFolderIsAlreadyAvailable() + { + // Arrange + var department = CreateDepartment(); + var folder = CreateFolder(); + folder.IsAvailable = true; + var locker = CreateLocker(folder); + var room = CreateRoom(department, locker); + await AddAsync(room); + + var command = new EnableFolder.Command() + { + FolderId = folder.Id, + }; + + // Act + var action = async () => await SendAsync(command); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Folder has already been enabled."); + + // Cleanup + Remove(folder); + Remove(locker); + Remove(room); + Remove(await FindAsync(department.Id)); + } +} \ No newline at end of file