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
5 changes: 3 additions & 2 deletions src/Application/Departments/Queries/GetAllDepartments.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,9 +24,10 @@ public QueryHandler(IApplicationDbContext context, IMapper mapper)

public async Task<IEnumerable<DepartmentDto>> Handle(Query request, CancellationToken cancellationToken)
{
var departments = await _context.Departments.ToListAsync(cancellationToken);
var departments = await _context.Departments
.ToListAsync(cancellationToken);
var result = new ReadOnlyCollection<DepartmentDto>(_mapper.Map<List<DepartmentDto>>(departments));
return result;
}
}
}
}
68 changes: 68 additions & 0 deletions src/Application/Lockers/Commands/UpdateLocker.cs
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,83 @@
using Application.Common.Exceptions;
using Application.Common.Interfaces;
using Application.Common.Models.Dtos.Physical;
using AutoMapper;
using Domain.Entities.Physical;
using Domain.Exceptions;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Lockers.Commands;

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

RuleFor(x => x.Name)
.NotEmpty()
.MaximumLength(64).WithMessage("Locker's name cannot exceed 64 characters.");

RuleFor(x => x.Description)
.MaximumLength(256).WithMessage("Locker's description cannot exceed 256 characters.");

RuleFor(x => x.Capacity)
.GreaterThan(0).WithMessage("Locker's capacity cannot be less than 1");
}
}
public record Command : IRequest<LockerDto>
{
public Guid LockerId { get; init; }
public string Name { get; init; } = null!;
public string? Description { get; init; }
public int Capacity { get; init; }
}

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

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

public async Task<LockerDto> Handle(Command request, CancellationToken cancellationToken)
{
var locker = await _context.Lockers.FirstOrDefaultAsync(
x => x.Id.Equals(request.LockerId), cancellationToken);

if (locker is null)
{
Comment thread
StarryFolf marked this conversation as resolved.
throw new KeyNotFoundException("Locker does not exist.");
}

var duplicateLocker = await _context.Lockers.FirstOrDefaultAsync(
x => x.Name.Equals(request.Name), cancellationToken);

if (duplicateLocker is not null && !duplicateLocker.Equals(locker))
{
throw new ConflictException("New locker name already exists.");
}

if (locker.NumberOfFolders > request.Capacity)
{
throw new ConflictException("New capacity cannot be less than current number of folders.");
}

locker.Name = request.Name;
locker.Description = request.Description;
locker.Capacity = request.Capacity;

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

namespace Application.Tests.Integration.Lockers.Commands;

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

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

var command = new UpdateLocker.Command()
{
LockerId = locker.Id,
Name = "Something else",
Capacity = 6,
Description = "ehehe",
};

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

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

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

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

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

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

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

var command = new UpdateLocker.Command()
{
LockerId = locker.Id,
Name = duplicateNameLocker.Name,
Capacity = 6,
Description = "ehehe",
};

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

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

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

[Fact]
public async Task ShouldThrowConflictException_WhenNewCapacityIsLessThanCurrentNumberOfFolders()
{
// Arrange
var department = CreateDepartment();
var folder1 = CreateFolder();
var folder2 = CreateFolder();
var locker = CreateLocker(folder1, folder2);
locker.Capacity = 3;
var room = CreateRoom(department, locker);
await AddAsync(room);

var command = new UpdateLocker.Command()
{
LockerId = locker.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 folders.");

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