Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Application/Folders/Commands/EnableFolder.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,4 +13,37 @@ public record Command : IRequest<FolderDto>
{
public Guid FolderId { get; init; }
}

public class CommandHandler : IRequestHandler<Command, FolderDto>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

public CommandHandler(IApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}

public async Task<FolderDto> 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<FolderDto>(result.Entity);
}
}
}
Original file line number Diff line number Diff line change
@@ -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>(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<KeyNotFoundException>()
.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<ConflictException>()
.WithMessage("Folder has already been enabled.");

// Cleanup
Remove(folder);
Remove(locker);
Remove(room);
Remove(await FindAsync<Department>(department.Id));
}
}