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
70 changes: 70 additions & 0 deletions src/Application/Folders/Commands/UpdateFolder.cs
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,85 @@
using Application.Common.Exceptions;
using Application.Common.Interfaces;
using Application.Common.Models.Dtos.Physical;
using AutoMapper;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Folders.Commands;

public class UpdateFolder
{
public class Validator : AbstractValidator<Command>
{
public Validator()
{
RuleLevelCascadeMode = CascadeMode.Stop;

RuleFor(f => f.Name)
.NotEmpty().WithMessage("Name is required.")
.MaximumLength(64).WithMessage("Name cannot exceed 64 characters.");

RuleFor(f => f.Description)
.MaximumLength(256).WithMessage("Description cannot exceed 256 characters.");

RuleFor(f => f.Capacity)
.NotEmpty().WithMessage("Folder capacity is required.")
.GreaterThanOrEqualTo(1).WithMessage("Folder's capacity cannot be less than 1.");
}
}

public record Command : IRequest<FolderDto>
{
public Guid FolderId { get; init; }
public string Name { get; init; } = null!;
public string? Description { get; init; }
public int Capacity { 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.");
}

var nameExisted = await _context.Folders.AnyAsync( x =>
x.Name.Trim().ToLower().Equals(request.Name.Trim().ToLower())
&& x.Id != folder.Id
, cancellationToken);

if (nameExisted)
{
throw new ConflictException("Folder name already exists.");
}

if (request.Capacity < folder.NumberOfDocuments)
{
throw new ConflictException("New capacity cannot be less than current number of documents.");
}

folder.Name = request.Name;
folder.Description = request.Description;
folder.Capacity = request.Capacity;

var result = _context.Folders.Update(folder);
await _context.SaveChangesAsync(cancellationToken);
return _mapper.Map<FolderDto>(result.Entity);
}
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
using Application.Common.Exceptions;
using Application.Folders.Commands;
using Domain.Entities;
using FluentAssertions;
using Xunit;

namespace Application.Tests.Integration.Folders.Commands;

public class UpdateFolderTests : BaseClassFixture
{
public UpdateFolderTests(CustomApiFactory apiFactory) : base(apiFactory)
{
}

[Fact]
public async Task ShouldUpdateFolder_WhenUpdateDetailsAreValid()
{
// Arrange
var department = CreateDepartment();
var folder = CreateFolder();
var locker = CreateLocker(folder);
var room = CreateRoom(department, locker);
await AddAsync(room);

var command = new UpdateFolder.Command()
{
FolderId = folder.Id,
Name = "Something else",
Capacity = 6,
Description = "ehehe",
};

// Act
var result = await SendAsync(command);

// Assert
result.Id.Should().Be(folder.Id);
result.Name.Should().Be(command.Name);
result.Capacity.Should().Be(command.Capacity);
result.Description.Should().Be(command.Description);

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

[Fact]
public async Task ShouldThrowKeyNotFoundException_WhenThatFolderDoesNotExist()
{
// Arrange
var command = new UpdateFolder.Command()
{
FolderId = Guid.NewGuid(),
Name = "Something else",
Capacity = 6,
Description = "ehehe",
};

// Act
var result = async () => await SendAsync(command);

// Assert
await result.Should().ThrowAsync<KeyNotFoundException>()
.WithMessage("Folder does not exist.");
}

[Fact]
public async Task ShouldThrowConflictException_WhenNewFolderNameHasAlreadyExistedInThatLocker()
{
// Arrange
var department = CreateDepartment();
var duplicateNameFolder = CreateFolder();
var folder = CreateFolder();
var locker = CreateLocker(duplicateNameFolder, folder);
var room = CreateRoom(department, locker);
await AddAsync(room);

var command = new UpdateFolder.Command()
{
FolderId = folder.Id,
Name = duplicateNameFolder.Name,
Capacity = 6,
Description = "ehehe",
};

// Act
var result = async () => await SendAsync(command);

// Assert
await result.Should().ThrowAsync<ConflictException>()
.WithMessage("Folder name already exists.");

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

[Fact]
public async Task ShouldThrowConflictException_WhenNewCapacityIsLessThanCurrentNumberOfDocuments()
{
// Arrange
var department = CreateDepartment();
var documents = CreateNDocuments(2);
var folder = CreateFolder(documents);
folder.Capacity = 3;
var locker = CreateLocker(folder);
var room = CreateRoom(department, locker);
await AddAsync(room);

var command = new UpdateFolder.Command()
{
FolderId = folder.Id,
Name = "Something else",
Capacity = 1,
Description = "ehehe",
};

// Act
var result = async () => await SendAsync(command);

// Assert
await result.Should().ThrowAsync<ConflictException>()
.WithMessage("New capacity cannot be less than current number of documents.");

// Cleanup
Remove(documents[0]);
Remove(documents[1]);
Remove(folder);
Remove(locker);
Remove(room);
Remove(await FindAsync<Department>(department.Id));
}
}