Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Feat/update locker#136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Feat/update locker #136
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5537e6e
add: integration test
ChienNQuang a7b48f3
feat: implement update locker
StarryFolf da86dd3
Update GetAllDepartments.cs (temporary workaround until chien provide…
StarryFolf 19c6f6d
fuck chiến
StarryFolf 4ddd4c0
Update UpdateLocker.cs
StarryFolf 1a2c4df
test
ChienNQuang 0a57fed
remove: exclude admin department
ChienNQuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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) | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
132 changes: 132 additions & 0 deletions
132 tests/Application.Tests.Integration/Lockers/Commands/UpdateLockerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.