From d8e2cd65208035b6847d115a94f2890dccfdcfc0 Mon Sep 17 00:00:00 2001 From: kaitoz11 <43519768+kaitoz11@users.noreply.github.com> Date: Fri, 26 May 2023 10:50:35 +0700 Subject: [PATCH 1/5] refactor(ExceptionMiddleware.cs): add static modifier to handle methods --- src/Api/Middlewares/ExceptionMiddleware.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Api/Middlewares/ExceptionMiddleware.cs b/src/Api/Middlewares/ExceptionMiddleware.cs index 194d73ab..5742bf16 100644 --- a/src/Api/Middlewares/ExceptionMiddleware.cs +++ b/src/Api/Middlewares/ExceptionMiddleware.cs @@ -54,25 +54,25 @@ private async Task HandleExceptionAsync(HttpContext context, Exception ex) } - private async void HandleKeyNotFoundException(HttpContext context, Exception ex) + private static async void HandleKeyNotFoundException(HttpContext context, Exception ex) { context.Response.StatusCode = StatusCodes.Status404NotFound; await WriteExceptionMessageAsync(context, ex); } - private async void HandleConflictException(HttpContext context, Exception ex) + private static async void HandleConflictException(HttpContext context, Exception ex) { context.Response.StatusCode = StatusCodes.Status409Conflict; await WriteExceptionMessageAsync(context, ex); } - private async void HandleNotAllowedException(HttpContext context, Exception ex) + private static async void HandleNotAllowedException(HttpContext context, Exception ex) { context.Response.StatusCode = StatusCodes.Status406NotAcceptable; await WriteExceptionMessageAsync(context, ex); } - private async void HandleRequestValidationException(HttpContext context, Exception ex) + private static async void HandleRequestValidationException(HttpContext context, Exception ex) { context.Response.StatusCode = StatusCodes.Status400BadRequest; @@ -86,13 +86,13 @@ private async void HandleRequestValidationException(HttpContext context, Excepti await context.Response.Body.WriteAsync(SerializeToUtf8BytesWeb(result)); } - private async void HandleLimitExceededException(HttpContext context, Exception ex) + private static async void HandleLimitExceededException(HttpContext context, Exception ex) { context.Response.StatusCode = StatusCodes.Status409Conflict; await WriteExceptionMessageAsync(context, ex); } - private async void HandleAuthenticationException(HttpContext context, Exception ex) + private static async void HandleAuthenticationException(HttpContext context, Exception ex) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await WriteExceptionMessageAsync(context, ex); @@ -104,7 +104,7 @@ private static async void HandleUnauthorizedAccessException(HttpContext context, await WriteExceptionMessageAsync(context, ex); } - private async void HandleInvalidOperationException(HttpContext context, Exception ex) + private static async void HandleInvalidOperationException(HttpContext context, Exception ex) { context.Response.StatusCode = StatusCodes.Status409Conflict; await WriteExceptionMessageAsync(context, ex); From d442319f3399ac6db1e989533b94ff17bba23626 Mon Sep 17 00:00:00 2001 From: kaitoz11 <43519768+kaitoz11@users.noreply.github.com> Date: Fri, 26 May 2023 12:45:31 +0700 Subject: [PATCH 2/5] refactor(AuthController.cs): add ProduceResponseType to endpoints --- src/Api/Controllers/AuthController.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Api/Controllers/AuthController.cs b/src/Api/Controllers/AuthController.cs index b0941458..841b836b 100644 --- a/src/Api/Controllers/AuthController.cs +++ b/src/Api/Controllers/AuthController.cs @@ -50,6 +50,8 @@ public async Task>> Login([FromBody] LoginModel [Authorize] [HttpPost] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task Logout() { var refreshToken = Request.Cookies[nameof(RefreshToken)]; @@ -67,6 +69,8 @@ public async Task Logout() [Authorize] [HttpPost] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task Refresh() { var refreshToken = Request.Cookies[nameof(RefreshToken)]; @@ -82,6 +86,8 @@ public async Task Refresh() [Authorize] [HttpPost] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task Validate() { var refreshToken = Request.Cookies[nameof(RefreshToken)]; From ce2af567369732e556a8688a548084b28e3a2cfd Mon Sep 17 00:00:00 2001 From: kaitoz11 <43519768+kaitoz11@users.noreply.github.com> Date: Fri, 26 May 2023 13:11:17 +0700 Subject: [PATCH 3/5] refactor(Departments): error message --- .../Commands/CreateDepartment/CreateDepartmentCommand.cs | 2 +- .../Departments/Commands/CreateDepartmentTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Application/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs b/src/Application/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs index cda6a16d..25eb8682 100644 --- a/src/Application/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs +++ b/src/Application/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs @@ -29,7 +29,7 @@ public async Task Handle(CreateDepartmentCommand request, Cancell if (department is not null) { - throw new ConflictException("Department name already exists"); + throw new ConflictException("Department name already exists."); } var entity = new Department { diff --git a/tests/Application.Tests.Integration/Departments/Commands/CreateDepartmentTests.cs b/tests/Application.Tests.Integration/Departments/Commands/CreateDepartmentTests.cs index 0e41af6c..e54ead7c 100644 --- a/tests/Application.Tests.Integration/Departments/Commands/CreateDepartmentTests.cs +++ b/tests/Application.Tests.Integration/Departments/Commands/CreateDepartmentTests.cs @@ -40,7 +40,7 @@ public async Task ShouldReturnConflict_WhenDepartmentNameHasExisted() var action = async () => await SendAsync(createDepartmentCommand); // Assert - await action.Should().ThrowAsync().WithMessage("Department name already exists"); + await action.Should().ThrowAsync().WithMessage("Department name already exists."); // Cleanup var departmentEntity = await FindAsync(department.Id); From 822b5733b153248c1015283f9d367e18e1691ca1 Mon Sep 17 00:00:00 2001 From: kaitoz11 <43519768+kaitoz11@users.noreply.github.com> Date: Fri, 26 May 2023 16:11:42 +0700 Subject: [PATCH 4/5] refactor(controller): Auth, Departments, Documents, Folders, Locker --- src/Api/Controllers/DepartmentsController.cs | 4 ++-- src/Api/Controllers/DocumentsController.cs | 8 ++++---- src/Api/Controllers/FoldersController.cs | 10 ++++++++-- src/Api/Controllers/LockersController.cs | 13 +++++++++++++ src/Api/Controllers/RoomsController.cs | 5 +++-- .../AddDepartmentCommand.cs} | 12 ++++++------ .../ImportDocument/ImportDocumentCommand.cs | 12 ++++++------ .../Folders/Commands/AddFolder/AddFolderCommand.cs | 5 +++-- .../Commands/DisableFolder/DisableFolderCommand.cs | 3 ++- .../Lockers/Commands/AddLocker/AddLockerCommand.cs | 10 +++++----- .../Rooms/Commands/CreateRoom/CreateRoomCommand.cs | 4 ++-- .../BaseClassFixture.cs | 4 ++-- .../CustomApiFactory.cs | 2 +- .../Folders/Commands/DisableFolderTests.cs | 3 ++- .../Lockers/Commands/AddLockerTests.cs | 2 +- 15 files changed, 60 insertions(+), 37 deletions(-) rename src/Application/Departments/Commands/{CreateDepartment/CreateDepartmentCommand.cs => AddDepartment/AddDepartmentCommand.cs} (67%) diff --git a/src/Api/Controllers/DepartmentsController.cs b/src/Api/Controllers/DepartmentsController.cs index 6a010c00..465738a9 100644 --- a/src/Api/Controllers/DepartmentsController.cs +++ b/src/Api/Controllers/DepartmentsController.cs @@ -1,5 +1,5 @@ using Application.Common.Models; -using Application.Departments.Commands.CreateDepartment; +using Application.Departments.Commands.AddDepartment; using Application.Departments.Queries.GetAllDepartments; using Application.Identity; using Application.Users.Queries; @@ -20,7 +20,7 @@ public class DepartmentsController : ApiControllerBase [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status409Conflict)] - public async Task>> CreateDepartment([FromBody] CreateDepartmentCommand command) + public async Task>> AddDepartment([FromBody] AddDepartmentCommand command) { var result = await Mediator.Send(command); return Ok(Result.Succeed(result)); diff --git a/src/Api/Controllers/DocumentsController.cs b/src/Api/Controllers/DocumentsController.cs index 7d491b2b..a4b4d8bf 100644 --- a/src/Api/Controllers/DocumentsController.cs +++ b/src/Api/Controllers/DocumentsController.cs @@ -1,12 +1,10 @@ using Application.Common.Models; using Application.Common.Models.Dtos.Physical; -using Application.Departments.Commands.CreateDepartment; using Application.Documents.Commands.ImportDocument; using Application.Documents.Queries.GetAllDocumentsPaginated; using Application.Documents.Queries.GetDocumentById; using Application.Documents.Queries.GetDocumentTypes; using Application.Identity; -using Application.Users.Queries; using Infrastructure.Identity.Authorization; using Microsoft.AspNetCore.Mvc; @@ -14,13 +12,13 @@ namespace Api.Controllers; public class DocumentsController : ApiControllerBase { + [RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)] [HttpPost] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status409Conflict)] - [RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)] public async Task>> ImportDocument([FromBody] ImportDocumentCommand command) { var result = await Mediator.Send(command); @@ -31,7 +29,6 @@ public async Task>> ImportDocument([FromBody] I [HttpGet("types")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] - public async Task>>> GetAllDocumentTypes() { var result = await Mediator.Send(new GetAllDocumentTypesQuery()); @@ -61,6 +58,9 @@ public async Task>>> GetAllDocume } [HttpGet("{id:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task>> GetDocumentById(Guid id) { var query = new GetDocumentByIdQuery() diff --git a/src/Api/Controllers/FoldersController.cs b/src/Api/Controllers/FoldersController.cs index 77664c44..e37a6691 100644 --- a/src/Api/Controllers/FoldersController.cs +++ b/src/Api/Controllers/FoldersController.cs @@ -10,9 +10,9 @@ namespace Api.Controllers; public class FoldersController : ApiControllerBase { - [RequiresRole(IdentityData.Roles.Staff)] + [RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)] [HttpPost] - [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -23,7 +23,13 @@ public async Task>> AddFolder([FromBody] AddFolde return Ok(Result.Succeed(result)); } + [RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)] [HttpPut("disable")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status409Conflict)] public async Task>> DisableFolder([FromBody] DisableFolderCommand command) { var result = await Mediator.Send(command); diff --git a/src/Api/Controllers/LockersController.cs b/src/Api/Controllers/LockersController.cs index 1dd1b3ce..350ca406 100644 --- a/src/Api/Controllers/LockersController.cs +++ b/src/Api/Controllers/LockersController.cs @@ -14,6 +14,7 @@ public class LockersController : ApiControllerBase [RequiresRole(IdentityData.Roles.Staff)] [HttpPost] [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status409Conflict)] @@ -23,14 +24,26 @@ public async Task>> AddLocker([FromBody] AddLocke return Ok(Result.Succeed(result)); } + [RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)] [HttpPut("disable")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status409Conflict)] public async Task>> DisableLocker([FromBody] DisableLockerCommand command) { var result = await Mediator.Send(command); return Ok(Result.Succeed(result)); } + [RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)] [HttpPut("enable")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status409Conflict)] public async Task>> EnableLocker([FromBody] EnableLockerCommand command) { var result = await Mediator.Send(command); diff --git a/src/Api/Controllers/RoomsController.cs b/src/Api/Controllers/RoomsController.cs index 8d8916d6..0dfe4c75 100644 --- a/src/Api/Controllers/RoomsController.cs +++ b/src/Api/Controllers/RoomsController.cs @@ -15,8 +15,9 @@ public class RoomsController : ApiControllerBase [RequiresRole(IdentityData.Roles.Admin)] [HttpPost] [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task>> AddRoom(CreateRoomCommand command) { var result = await Mediator.Send(command); @@ -34,7 +35,7 @@ public async Task>> GetEmptyContainer return Ok(Result>.Succeed(result)); } - [HttpPut] + [HttpPut("disable")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status409Conflict)] [ProducesResponseType(StatusCodes.Status404NotFound)] diff --git a/src/Application/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs b/src/Application/Departments/Commands/AddDepartment/AddDepartmentCommand.cs similarity index 67% rename from src/Application/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs rename to src/Application/Departments/Commands/AddDepartment/AddDepartmentCommand.cs index 25eb8682..25ff73b2 100644 --- a/src/Application/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs +++ b/src/Application/Departments/Commands/AddDepartment/AddDepartmentCommand.cs @@ -6,14 +6,14 @@ using MediatR; using Microsoft.EntityFrameworkCore; -namespace Application.Departments.Commands.CreateDepartment; +namespace Application.Departments.Commands.AddDepartment; -public record CreateDepartmentCommand : IRequest +public record AddDepartmentCommand : IRequest { - public string Name { get; init; } + public string Name { get; init; } = null!; } -public class CreateDepartmentCommandHandler : IRequestHandler +public class CreateDepartmentCommandHandler : IRequestHandler { private readonly IApplicationDbContext _context; private readonly IMapper _mapper; @@ -23,9 +23,9 @@ public CreateDepartmentCommandHandler(IApplicationDbContext context, IMapper map _mapper = mapper; } - public async Task Handle(CreateDepartmentCommand request, CancellationToken cancellationToken) + public async Task Handle(AddDepartmentCommand request, CancellationToken cancellationToken) { - var department = await _context.Departments.FirstOrDefaultAsync(x => x.Name.Equals(request.Name), cancellationToken); + var department = await _context.Departments.FirstOrDefaultAsync(x => x.Name.Trim().ToLower().Equals(request.Name.Trim().ToLower()), cancellationToken); if (department is not null) { diff --git a/src/Application/Documents/Commands/ImportDocument/ImportDocumentCommand.cs b/src/Application/Documents/Commands/ImportDocument/ImportDocumentCommand.cs index 373e7202..7989bf5a 100644 --- a/src/Application/Documents/Commands/ImportDocument/ImportDocumentCommand.cs +++ b/src/Application/Documents/Commands/ImportDocument/ImportDocumentCommand.cs @@ -10,9 +10,9 @@ namespace Application.Documents.Commands.ImportDocument; public record ImportDocumentCommand : IRequest { - public string Title { get; init; } + public string Title { get; init; } = null!; public string? Description { get; init; } - public string DocumentType { get; init; } + public string DocumentType { get; init; } = null!; public Guid ImporterId { get; init; } public Guid FolderId { get; init; } } @@ -35,23 +35,23 @@ public async Task Handle(ImportDocumentCommand request, Cancellatio .FirstOrDefaultAsync(x => x.Id == request.ImporterId, cancellationToken); if (importer is null) { - throw new KeyNotFoundException("User does not exist"); + throw new KeyNotFoundException("User does not exist."); } var document = _context.Documents.FirstOrDefault(x => - x.Title.Equals(request.Title) + x.Title.Trim().ToLower().Equals(request.Title.Trim().ToLower()) && x.Importer != null && x.Importer.Id == request.ImporterId); if (document is not null) { - throw new ConflictException($"Document title already exists for user {importer.LastName}"); + throw new ConflictException($"Document title already exists for user {importer.LastName}."); } var folder = await _context.Folders .FirstOrDefaultAsync(x => x.Id == request.FolderId, cancellationToken); if (folder is null) { - throw new KeyNotFoundException("Folder does not exist"); + throw new KeyNotFoundException("Folder does not exist."); } var entity = new Document() diff --git a/src/Application/Folders/Commands/AddFolder/AddFolderCommand.cs b/src/Application/Folders/Commands/AddFolder/AddFolderCommand.cs index e3fd551d..bf974013 100644 --- a/src/Application/Folders/Commands/AddFolder/AddFolderCommand.cs +++ b/src/Application/Folders/Commands/AddFolder/AddFolderCommand.cs @@ -11,7 +11,7 @@ namespace Application.Folders.Commands.AddFolder; public record AddFolderCommand : IRequest { - public string Name { get; init; } + public string Name { get; init; } = null!; public string? Description { get; init; } public int Capacity { get; init; } public Guid LockerId { get; init; } @@ -42,7 +42,8 @@ public async Task Handle(AddFolderCommand request, CancellationToken throw new LimitExceededException("This locker cannot accept more folders."); } - var folder = await _context.Folders.FirstOrDefaultAsync(x => x.Name.Trim().Equals(request.Name.Trim()) && x.Locker.Id.Equals(request.LockerId), cancellationToken); + var folder = await _context.Folders.FirstOrDefaultAsync(x => x.Name.Trim().ToLower().Equals(request.Name.Trim().ToLower()) + && x.Locker.Id.Equals(request.LockerId), cancellationToken); if (folder is not null) { diff --git a/src/Application/Folders/Commands/DisableFolder/DisableFolderCommand.cs b/src/Application/Folders/Commands/DisableFolder/DisableFolderCommand.cs index d49909a6..adf845df 100644 --- a/src/Application/Folders/Commands/DisableFolder/DisableFolderCommand.cs +++ b/src/Application/Folders/Commands/DisableFolder/DisableFolderCommand.cs @@ -1,3 +1,4 @@ +using Application.Common.Exceptions; using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; using AutoMapper; @@ -34,7 +35,7 @@ public async Task Handle(DisableFolderCommand request, CancellationTo if (!folder.IsAvailable) { - throw new InvalidOperationException("Folder has already been disabled."); + throw new ConflictException("Folder has already been disabled."); } if (folder.NumberOfDocuments > 0) diff --git a/src/Application/Lockers/Commands/AddLocker/AddLockerCommand.cs b/src/Application/Lockers/Commands/AddLocker/AddLockerCommand.cs index f23cc079..7a47bd87 100644 --- a/src/Application/Lockers/Commands/AddLocker/AddLockerCommand.cs +++ b/src/Application/Lockers/Commands/AddLocker/AddLockerCommand.cs @@ -11,8 +11,8 @@ namespace Application.Lockers.Commands.AddLocker; public record AddLockerCommand : IRequest { - public string Name { get; init; } - public string Description { get; init; } + public string Name { get; init; } = null!; + public string? Description { get; init; } public Guid RoomId { get; init; } public int Capacity { get; init; } } @@ -44,16 +44,16 @@ public async Task Handle(AddLockerCommand request, CancellationToken ); } - var locker = await _context.Lockers.FirstOrDefaultAsync(x => x.Name.Trim().Equals(request.Name.Trim()) && x.Room.Id.Equals(request.RoomId)); + var locker = await _context.Lockers.FirstOrDefaultAsync(x => x.Name.Trim().ToLower().Equals(request.Name.Trim().ToLower()) && x.Room.Id.Equals(request.RoomId) ,cancellationToken); if (locker is not null) { - throw new ConflictException("Locker's name already exists."); + throw new ConflictException("Locker name already exists."); } var entity = new Locker { Name = request.Name.Trim(), - Description = request.Description, + Description = request.Description?.Trim(), NumberOfFolders = 0, Capacity = request.Capacity, Room = room, diff --git a/src/Application/Rooms/Commands/CreateRoom/CreateRoomCommand.cs b/src/Application/Rooms/Commands/CreateRoom/CreateRoomCommand.cs index 53c24c4d..ac10f4e2 100644 --- a/src/Application/Rooms/Commands/CreateRoom/CreateRoomCommand.cs +++ b/src/Application/Rooms/Commands/CreateRoom/CreateRoomCommand.cs @@ -9,8 +9,8 @@ namespace Application.Rooms.Commands.CreateRoom; public record CreateRoomCommand : IRequest { - public string Name { get; init; } - public string Description { get; init; } + public string Name { get; init; } = null!; + public string? Description { get; init; } public int Capacity { get; init; } } diff --git a/tests/Application.Tests.Integration/BaseClassFixture.cs b/tests/Application.Tests.Integration/BaseClassFixture.cs index 3411d01a..7e0d22c0 100644 --- a/tests/Application.Tests.Integration/BaseClassFixture.cs +++ b/tests/Application.Tests.Integration/BaseClassFixture.cs @@ -1,4 +1,4 @@ -using Application.Departments.Commands.CreateDepartment; +using Application.Departments.Commands.AddDepartment; using Bogus; using Domain.Common; using Domain.Entities; @@ -15,7 +15,7 @@ namespace Application.Tests.Integration; [Collection(nameof(BaseCollectionFixture))] public class BaseClassFixture { - protected readonly Faker _departmentGenerator = new Faker() + protected readonly Faker _departmentGenerator = new Faker() .RuleFor(x => x.Name, faker => faker.Commerce.Department()); protected static IServiceScopeFactory _scopeFactory = null!; diff --git a/tests/Application.Tests.Integration/CustomApiFactory.cs b/tests/Application.Tests.Integration/CustomApiFactory.cs index 04d44731..9d30c073 100644 --- a/tests/Application.Tests.Integration/CustomApiFactory.cs +++ b/tests/Application.Tests.Integration/CustomApiFactory.cs @@ -28,7 +28,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) var databaseSettings = GetConfiguration().GetSection(nameof(DatabaseSettings)).Get(); services.AddDbContext(options => { - options.UseNpgsql(databaseSettings.ConnectionString, optionsBuilder => optionsBuilder.UseNodaTime()); + options.UseNpgsql(databaseSettings?.ConnectionString, optionsBuilder => optionsBuilder.UseNodaTime()); }); }); } diff --git a/tests/Application.Tests.Integration/Folders/Commands/DisableFolderTests.cs b/tests/Application.Tests.Integration/Folders/Commands/DisableFolderTests.cs index 80528c3c..1dee5f9d 100644 --- a/tests/Application.Tests.Integration/Folders/Commands/DisableFolderTests.cs +++ b/tests/Application.Tests.Integration/Folders/Commands/DisableFolderTests.cs @@ -1,3 +1,4 @@ +using Application.Common.Exceptions; using Application.Folders.Commands.DisableFolder; using Domain.Entities.Physical; using FluentAssertions; @@ -121,7 +122,7 @@ public async Task ShouldThrowInvalidOperationException_WhenFolderIsAlreadyDisabl var result = async () => await SendAsync(disableFolderCommand); // Assert - await result.Should().ThrowAsync() + await result.Should().ThrowAsync() .WithMessage("Folder has already been disabled."); // Cleanup diff --git a/tests/Application.Tests.Integration/Lockers/Commands/AddLockerTests.cs b/tests/Application.Tests.Integration/Lockers/Commands/AddLockerTests.cs index 5cedf272..8f3d30e0 100644 --- a/tests/Application.Tests.Integration/Lockers/Commands/AddLockerTests.cs +++ b/tests/Application.Tests.Integration/Lockers/Commands/AddLockerTests.cs @@ -91,7 +91,7 @@ public async Task ShouldThrowConflictException_WhenLockerAlreadyExistsInTheSameR var action = async () => await SendAsync(addLockerCommand); // Assert - await action.Should().ThrowAsync().WithMessage("Locker's name already exists."); + await action.Should().ThrowAsync().WithMessage("Locker name already exists."); // Cleanup var roomEntity = await FindAsync(room.Id); From 0f95e755ae0ff86bf47ba637e48eced50a2852f0 Mon Sep 17 00:00:00 2001 From: kaitoz11 <43519768+kaitoz11@users.noreply.github.com> Date: Sat, 27 May 2023 00:21:09 +0700 Subject: [PATCH 5/5] refactor most api endpoints --- src/Api/Controllers/RoomsController.cs | 13 +++-- src/Api/Controllers/StaffsController.cs | 4 +- src/Api/Controllers/UsersController.cs | 11 ++-- .../AddDepartment/AddDepartmentCommand.cs | 4 +- .../Rooms/Commands/AddRoom/AddRoomCommand.cs | 51 +++++++++++++++++++ .../AddRoomCommandValidator.cs} | 6 +-- .../Commands/CreateRoom/CreateRoomCommand.cs | 41 --------------- .../DisableRoom/DisableRoomCommand.cs | 2 +- .../GetEmptyContainersPaginatedQuery.cs | 2 +- .../AddStaffCommand.cs} | 14 ++--- .../AddUserCommand.cs} | 32 ++++++------ .../AddUserCommandValidator.cs} | 19 ++++--- .../DisableUser/DisableUserCommand.cs | 3 +- ...partmentTests.cs => AddDepartmentTests.cs} | 4 +- .../Folders/Commands/AddFolderTests.cs | 4 +- .../Rooms/Commands/DisableRoomTests.cs | 2 +- .../{CreateUserTests.cs => AddUserTests.cs} | 8 +-- 17 files changed, 118 insertions(+), 102 deletions(-) create mode 100644 src/Application/Rooms/Commands/AddRoom/AddRoomCommand.cs rename src/Application/Rooms/Commands/{CreateRoom/CreateRoomCommandValidator.cs => AddRoom/AddRoomCommandValidator.cs} (82%) delete mode 100644 src/Application/Rooms/Commands/CreateRoom/CreateRoomCommand.cs rename src/Application/Staffs/Commands/{CreateStaff/CreateStaffCommand.cs => AddStaff/AddStaffCommand.cs} (67%) rename src/Application/Users/Commands/{CreateUser/CreateUserCommand.cs => AddUser/AddUserCommand.cs} (68%) rename src/Application/Users/Commands/{CreateUser/CreateUserCommandValidator.cs => AddUser/AddUserCommandValidator.cs} (67%) rename tests/Application.Tests.Integration/Departments/Commands/{CreateDepartmentTests.cs => AddDepartmentTests.cs} (91%) rename tests/Application.Tests.Integration/Users/Commands/{CreateUserTests.cs => AddUserTests.cs} (87%) diff --git a/src/Api/Controllers/RoomsController.cs b/src/Api/Controllers/RoomsController.cs index 0dfe4c75..39c156e9 100644 --- a/src/Api/Controllers/RoomsController.cs +++ b/src/Api/Controllers/RoomsController.cs @@ -1,7 +1,7 @@ using Application.Common.Models; using Application.Common.Models.Dtos.Physical; using Application.Identity; -using Application.Rooms.Commands.CreateRoom; +using Application.Rooms.Commands.AddRoom; using Application.Rooms.Commands.DisableRoom; using Application.Rooms.Commands.RemoveRoom; using Application.Rooms.Queries.GetEmptyContainersPaginated; @@ -18,7 +18,8 @@ public class RoomsController : ApiControllerBase [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task>> AddRoom(CreateRoomCommand command) + [ProducesResponseType(StatusCodes.Status409Conflict)] + public async Task>> AddRoom(AddRoomCommand command) { var result = await Mediator.Send(command); return Ok(Result.Succeed(result)); @@ -35,20 +36,24 @@ public async Task>> GetEmptyContainer return Ok(Result>.Succeed(result)); } + [RequiresRole(IdentityData.Roles.Admin)] [HttpPut("disable")] [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status409Conflict)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status409Conflict)] public async Task>> DisableRoom(DisableRoomCommand command) { var result = await Mediator.Send(command); return Ok(Result.Succeed(result)); } + [RequiresRole(IdentityData.Roles.Admin)] [HttpDelete] [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status409Conflict)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status409Conflict)] public async Task>> RemoveRoom(RemoveRoomCommand command) { var result = await Mediator.Send(command); diff --git a/src/Api/Controllers/StaffsController.cs b/src/Api/Controllers/StaffsController.cs index 255335ef..d714b2bf 100644 --- a/src/Api/Controllers/StaffsController.cs +++ b/src/Api/Controllers/StaffsController.cs @@ -1,6 +1,6 @@ using Application.Common.Models; using Application.Identity; -using Application.Staffs.Commands.CreateStaff; +using Application.Staffs.Commands.AddStaff; using Application.Users.Queries.Physical; using Infrastructure.Identity.Authorization; using Microsoft.AspNetCore.Mvc; @@ -14,7 +14,7 @@ public class StaffsController : ApiControllerBase [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task>> CreateStaff([FromBody] CreateStaffCommand command) + public async Task>> AddStaff([FromBody] AddStaffCommand command) { var result = await Mediator.Send(command); return Ok(Result.Succeed(result)); diff --git a/src/Api/Controllers/UsersController.cs b/src/Api/Controllers/UsersController.cs index 33aa1714..1d51f7f0 100644 --- a/src/Api/Controllers/UsersController.cs +++ b/src/Api/Controllers/UsersController.cs @@ -1,6 +1,6 @@ using Application.Common.Models; using Application.Identity; -using Application.Users.Commands.CreateUser; +using Application.Users.Commands.AddUser; using Application.Users.Commands.DisableUser; using Application.Users.Queries; using Application.Users.Queries.GetUsersByName; @@ -12,19 +12,19 @@ namespace Api.Controllers; public class UsersController : ApiControllerBase { + [RequiresRole(IdentityData.Roles.Admin)] [HttpPost] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status409Conflict)] - public async Task>> CreateUser([FromBody] CreateUserCommand command) + public async Task>> AddUser([FromBody] AddUserCommand command) { var result = await Mediator.Send(command); return Ok(Result.Succeed(result)); } - - [Authorize] + [RequiresRole(IdentityData.Roles.Admin)] [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] @@ -41,11 +41,12 @@ public async Task>>> GetUsersByName(s return Ok(Result>.Succeed(result)); } - [HttpPost("disable")] [RequiresRole(IdentityData.Roles.Admin)] + [HttpPost("disable")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status409Conflict)] public async Task>> DisableUser([FromBody] DisableUserCommand command) { var result = await Mediator.Send(command); diff --git a/src/Application/Departments/Commands/AddDepartment/AddDepartmentCommand.cs b/src/Application/Departments/Commands/AddDepartment/AddDepartmentCommand.cs index 25ff73b2..00f42e5c 100644 --- a/src/Application/Departments/Commands/AddDepartment/AddDepartmentCommand.cs +++ b/src/Application/Departments/Commands/AddDepartment/AddDepartmentCommand.cs @@ -13,11 +13,11 @@ public record AddDepartmentCommand : IRequest public string Name { get; init; } = null!; } -public class CreateDepartmentCommandHandler : IRequestHandler +public class AddDepartmentCommandHandler : IRequestHandler { private readonly IApplicationDbContext _context; private readonly IMapper _mapper; - public CreateDepartmentCommandHandler(IApplicationDbContext context, IMapper mapper) + public AddDepartmentCommandHandler(IApplicationDbContext context, IMapper mapper) { _context = context; _mapper = mapper; diff --git a/src/Application/Rooms/Commands/AddRoom/AddRoomCommand.cs b/src/Application/Rooms/Commands/AddRoom/AddRoomCommand.cs new file mode 100644 index 00000000..f255b988 --- /dev/null +++ b/src/Application/Rooms/Commands/AddRoom/AddRoomCommand.cs @@ -0,0 +1,51 @@ +using Application.Common.Exceptions; +using Application.Common.Interfaces; +using Application.Common.Models.Dtos.Physical; +using AutoMapper; +using Domain.Entities.Physical; +using MediatR; +using Microsoft.EntityFrameworkCore; + +namespace Application.Rooms.Commands.AddRoom; + +public record AddRoomCommand : IRequest +{ + public string Name { get; init; } = null!; + public string? Description { get; init; } + public int Capacity { get; init; } + +} + +public class AddRoomCommandHandler : IRequestHandler +{ + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + public AddRoomCommandHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task Handle(AddRoomCommand request, CancellationToken cancellationToken) + { + + var room = await _context.Rooms.FirstOrDefaultAsync(r => + r.Name.Trim().ToLower().Equals(request.Name.Trim().ToLower()), cancellationToken); + + if (room is not null) + { + throw new ConflictException("Room name already exists."); + } + + var entity = new Room + { + Name = request.Name.Trim(), + Description = request.Description?.Trim(), + NumberOfLockers = 0, + Capacity = request.Capacity + }; + var result = await _context.Rooms.AddAsync(entity, cancellationToken); + await _context.SaveChangesAsync(cancellationToken); + return _mapper.Map(result.Entity); + } +} \ No newline at end of file diff --git a/src/Application/Rooms/Commands/CreateRoom/CreateRoomCommandValidator.cs b/src/Application/Rooms/Commands/AddRoom/AddRoomCommandValidator.cs similarity index 82% rename from src/Application/Rooms/Commands/CreateRoom/CreateRoomCommandValidator.cs rename to src/Application/Rooms/Commands/AddRoom/AddRoomCommandValidator.cs index 14bfc7ca..bfa65d1a 100644 --- a/src/Application/Rooms/Commands/CreateRoom/CreateRoomCommandValidator.cs +++ b/src/Application/Rooms/Commands/AddRoom/AddRoomCommandValidator.cs @@ -1,12 +1,12 @@ using Application.Common.Interfaces; using FluentValidation; -namespace Application.Rooms.Commands.CreateRoom; +namespace Application.Rooms.Commands.AddRoom; -public class CreateRoomCommandValidator : AbstractValidator +public class AddRoomCommandValidator : AbstractValidator { private readonly IApplicationDbContext _context; - public CreateRoomCommandValidator(IApplicationDbContext context) + public AddRoomCommandValidator(IApplicationDbContext context) { _context = context; diff --git a/src/Application/Rooms/Commands/CreateRoom/CreateRoomCommand.cs b/src/Application/Rooms/Commands/CreateRoom/CreateRoomCommand.cs deleted file mode 100644 index ac10f4e2..00000000 --- a/src/Application/Rooms/Commands/CreateRoom/CreateRoomCommand.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Application.Common.Interfaces; -using Application.Common.Models.Dtos.Physical; -using Application.Users.Queries; -using AutoMapper; -using Domain.Entities.Physical; -using MediatR; - -namespace Application.Rooms.Commands.CreateRoom; - -public record CreateRoomCommand : IRequest -{ - public string Name { get; init; } = null!; - public string? Description { get; init; } - public int Capacity { get; init; } - -} - -public class CreateRoomCommandHandler : IRequestHandler -{ - private readonly IApplicationDbContext _context; - private readonly IMapper _mapper; - public CreateRoomCommandHandler(IApplicationDbContext context, IMapper mapper) - { - _context = context; - _mapper = mapper; - } - - public async Task Handle(CreateRoomCommand request, CancellationToken cancellationToken) - { - var entity = new Room - { - Name = request.Name, - Description = request.Description, - NumberOfLockers = 0, - Capacity = request.Capacity - }; - var result = await _context.Rooms.AddAsync(entity, cancellationToken); - await _context.SaveChangesAsync(cancellationToken); - return _mapper.Map(result.Entity); - } -} \ No newline at end of file diff --git a/src/Application/Rooms/Commands/DisableRoom/DisableRoomCommand.cs b/src/Application/Rooms/Commands/DisableRoom/DisableRoomCommand.cs index dbf28e54..91ca9bdb 100644 --- a/src/Application/Rooms/Commands/DisableRoom/DisableRoomCommand.cs +++ b/src/Application/Rooms/Commands/DisableRoom/DisableRoomCommand.cs @@ -37,7 +37,7 @@ public async Task Handle(DisableRoomCommand request, CancellationToken if (!room.IsAvailable) { - throw new InvalidOperationException("Room have already been disabled."); + throw new ConflictException("Room have already been disabled."); } var canNotDisable = await _context.Documents diff --git a/src/Application/Rooms/Queries/GetEmptyContainersPaginated/GetEmptyContainersPaginatedQuery.cs b/src/Application/Rooms/Queries/GetEmptyContainersPaginated/GetEmptyContainersPaginatedQuery.cs index dcd6cbf5..166881aa 100644 --- a/src/Application/Rooms/Queries/GetEmptyContainersPaginated/GetEmptyContainersPaginatedQuery.cs +++ b/src/Application/Rooms/Queries/GetEmptyContainersPaginated/GetEmptyContainersPaginatedQuery.cs @@ -29,7 +29,7 @@ public async Task> Handle(GetEmptyContainersPagina var room = await _context.Rooms.FirstOrDefaultAsync(x => x.Id == request.RoomId, cancellationToken); if (room is null) { - throw new KeyNotFoundException("Room does not exist"); + throw new KeyNotFoundException("Room does not exist."); } var lockers = _context.Lockers diff --git a/src/Application/Staffs/Commands/CreateStaff/CreateStaffCommand.cs b/src/Application/Staffs/Commands/AddStaff/AddStaffCommand.cs similarity index 67% rename from src/Application/Staffs/Commands/CreateStaff/CreateStaffCommand.cs rename to src/Application/Staffs/Commands/AddStaff/AddStaffCommand.cs index bf5fbfa1..409e8cf9 100644 --- a/src/Application/Staffs/Commands/CreateStaff/CreateStaffCommand.cs +++ b/src/Application/Staffs/Commands/AddStaff/AddStaffCommand.cs @@ -5,37 +5,37 @@ using MediatR; using Microsoft.EntityFrameworkCore; -namespace Application.Staffs.Commands.CreateStaff; +namespace Application.Staffs.Commands.AddStaff; -public record CreateStaffCommand : IRequest +public record AddStaffCommand : IRequest { public Guid UserId { get; init; } public Guid RoomId { get; init; } } -public class CreateStaffCommandHandler : IRequestHandler +public class AddStaffCommandHandler : IRequestHandler { private readonly IApplicationDbContext _context; private readonly IMapper _mapper; - public CreateStaffCommandHandler(IApplicationDbContext context, IMapper mapper) + public AddStaffCommandHandler(IApplicationDbContext context, IMapper mapper) { _context = context; _mapper = mapper; } - public async Task Handle(CreateStaffCommand request, CancellationToken cancellationToken) + public async Task Handle(AddStaffCommand request, CancellationToken cancellationToken) { var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == request.UserId, cancellationToken); if (user is null) { - throw new KeyNotFoundException("User does not exist"); + throw new KeyNotFoundException("User does not exist."); } var room = await _context.Rooms.FirstOrDefaultAsync(x => x.Id == request.RoomId, cancellationToken); if (room is null) { - throw new KeyNotFoundException("Room does not exist"); + throw new KeyNotFoundException("Room does not exist."); } var staff = new Staff diff --git a/src/Application/Users/Commands/CreateUser/CreateUserCommand.cs b/src/Application/Users/Commands/AddUser/AddUserCommand.cs similarity index 68% rename from src/Application/Users/Commands/CreateUser/CreateUserCommand.cs rename to src/Application/Users/Commands/AddUser/AddUserCommand.cs index c0ed1617..5f5f684c 100644 --- a/src/Application/Users/Commands/CreateUser/CreateUserCommand.cs +++ b/src/Application/Users/Commands/AddUser/AddUserCommand.cs @@ -9,37 +9,37 @@ using Microsoft.EntityFrameworkCore; using NodaTime; -namespace Application.Users.Commands.CreateUser; +namespace Application.Users.Commands.AddUser; -public record CreateUserCommand : IRequest +public record AddUserCommand : IRequest { - public string Username { get; init; } - public string Email { get; init; } - public string Password { get; init; } - public string FirstName { get; init; } - public string LastName { get; init; } + public string Username { get; init; } = null!; + public string Email { get; init; } = null!; + public string Password { get; init; } = null!; + public string? FirstName { get; init; } + public string? LastName { get; init; } public Guid DepartmentId { get; init; } - public string Role { get; init; } - public string Position { get; init; } + public string Role { get; init; } = null!; + public string? Position { get; init; } } -public class CreateUserCommandHandler : IRequestHandler +public class AddUserCommandHandler : IRequestHandler { private readonly IApplicationDbContext _context; private readonly IMapper _mapper; - public CreateUserCommandHandler(IApplicationDbContext context, IMapper mapper) + public AddUserCommandHandler(IApplicationDbContext context, IMapper mapper) { _context = context; _mapper = mapper; } - public async Task Handle(CreateUserCommand request, CancellationToken cancellationToken) + public async Task Handle(AddUserCommand request, CancellationToken cancellationToken) { var user = await _context.Users.FirstOrDefaultAsync( x => x.Username.Equals(request.Username) || x.Email.Equals(request.Email), cancellationToken); if (user is not null) { - throw new ConflictException("Username or Email has been taken"); + throw new ConflictException("Username or Email has been taken."); } var department = await _context.Departments @@ -47,7 +47,7 @@ public async Task Handle(CreateUserCommand request, CancellationToken c if (department is null) { - throw new KeyNotFoundException("Department does not exist"); + throw new KeyNotFoundException("Department does not exist."); } var entity = new User @@ -55,8 +55,8 @@ public async Task Handle(CreateUserCommand request, CancellationToken c Username = request.Username, PasswordHash = SecurityUtil.Hash(request.Password), Email = request.Email, - FirstName = request.FirstName.Trim(), - LastName = request.LastName.Trim(), + FirstName = request.FirstName?.Trim(), + LastName = request.LastName?.Trim(), Department = department, Role = request.Role, Position = request.Position, diff --git a/src/Application/Users/Commands/CreateUser/CreateUserCommandValidator.cs b/src/Application/Users/Commands/AddUser/AddUserCommandValidator.cs similarity index 67% rename from src/Application/Users/Commands/CreateUser/CreateUserCommandValidator.cs rename to src/Application/Users/Commands/AddUser/AddUserCommandValidator.cs index cfd82b1d..16d07c05 100644 --- a/src/Application/Users/Commands/CreateUser/CreateUserCommandValidator.cs +++ b/src/Application/Users/Commands/AddUser/AddUserCommandValidator.cs @@ -1,10 +1,11 @@ +using Application.Identity; using FluentValidation; -namespace Application.Users.Commands.CreateUser; +namespace Application.Users.Commands.AddUser; -public class CreateUserCommandValidator : AbstractValidator +public class AddUserCommandValidator : AbstractValidator { - public CreateUserCommandValidator() + public AddUserCommandValidator() { RuleLevelCascadeMode = CascadeMode.Stop; @@ -13,32 +14,30 @@ public CreateUserCommandValidator() .MaximumLength(50).WithMessage("Username cannot exceed 64 characters."); RuleFor(x => x.Email) + .NotEmpty().WithMessage("Email is required.") .EmailAddress().WithMessage("Require valid email.") - .MaximumLength(320).WithMessage("Email length too long"); + .MaximumLength(320).WithMessage("Email length too long."); RuleFor(x => x.Password) - .NotEmpty().WithMessage("Your password cannot be empty"); + .NotEmpty().WithMessage("Password is required."); RuleFor(x => x.Role) - .NotEmpty().WithMessage("Role cannot be empty.") + .NotEmpty().WithMessage("Role is required.") .MaximumLength(64).WithMessage("Role cannot exceed 64 characters.") .Must(BeNotAdmin).WithMessage("Cannot add a user as Administrator."); RuleFor(x => x.FirstName) - .NotEmpty().WithMessage("First name is required.") .MaximumLength(50).WithMessage("First name cannot exceed 50 characters."); RuleFor(x => x.LastName) - .NotEmpty().WithMessage("Last name is required.") .MaximumLength(50).WithMessage("Last name cannot exceed 50 characters."); RuleFor(x => x.Position) - .NotEmpty().WithMessage("Position cannot be empty.") .MaximumLength(64).WithMessage("Position cannot exceed 64 characters."); } private static bool BeNotAdmin(string role) { - return !role.Equals("Administrator"); + return !role.Equals(IdentityData.Roles.Admin); } } \ No newline at end of file diff --git a/src/Application/Users/Commands/DisableUser/DisableUserCommand.cs b/src/Application/Users/Commands/DisableUser/DisableUserCommand.cs index 505961e6..43abac74 100644 --- a/src/Application/Users/Commands/DisableUser/DisableUserCommand.cs +++ b/src/Application/Users/Commands/DisableUser/DisableUserCommand.cs @@ -1,3 +1,4 @@ +using Application.Common.Exceptions; using Application.Common.Interfaces; using Application.Users.Queries; using AutoMapper; @@ -31,7 +32,7 @@ public async Task Handle(DisableUserCommand request, CancellationToken if (!user.IsActive) { - throw new InvalidOperationException("User has already been disabled."); + throw new ConflictException("User has already been disabled."); } user.IsActive = false; diff --git a/tests/Application.Tests.Integration/Departments/Commands/CreateDepartmentTests.cs b/tests/Application.Tests.Integration/Departments/Commands/AddDepartmentTests.cs similarity index 91% rename from tests/Application.Tests.Integration/Departments/Commands/CreateDepartmentTests.cs rename to tests/Application.Tests.Integration/Departments/Commands/AddDepartmentTests.cs index e54ead7c..8823a9fb 100644 --- a/tests/Application.Tests.Integration/Departments/Commands/CreateDepartmentTests.cs +++ b/tests/Application.Tests.Integration/Departments/Commands/AddDepartmentTests.cs @@ -6,9 +6,9 @@ namespace Application.Tests.Integration.Departments.Commands; -public class CreateDepartmentTests : BaseClassFixture +public class AddDepartmentTests : BaseClassFixture { - public CreateDepartmentTests(CustomApiFactory apiFactory) : base(apiFactory) + public AddDepartmentTests(CustomApiFactory apiFactory) : base(apiFactory) { } diff --git a/tests/Application.Tests.Integration/Folders/Commands/AddFolderTests.cs b/tests/Application.Tests.Integration/Folders/Commands/AddFolderTests.cs index db3c863c..8832e0c3 100644 --- a/tests/Application.Tests.Integration/Folders/Commands/AddFolderTests.cs +++ b/tests/Application.Tests.Integration/Folders/Commands/AddFolderTests.cs @@ -2,7 +2,7 @@ using Application.Common.Models.Dtos.Physical; using Application.Folders.Commands.AddFolder; using Application.Lockers.Commands.AddLocker; -using Application.Rooms.Commands.CreateRoom; +using Application.Rooms.Commands.AddRoom; using Bogus; using Domain.Entities.Physical; using Domain.Exceptions; @@ -18,7 +18,7 @@ public class AddFolderTests : BaseClassFixture .RuleFor(f => f.Description, faker => faker.Commerce.ProductDescription()) .RuleFor(f => f.Capacity, faker => faker.Random.Int(1,9999)); - private readonly Faker _roomGenerator = new Faker() + private readonly Faker _roomGenerator = new Faker() .RuleFor(r => r.Name, faker => faker.Commerce.ProductName()) .RuleFor(r => r.Description, faker => faker.Commerce.ProductDescription()) .RuleFor(r => r.Capacity, faker => faker.Random.Int(1,9999)); diff --git a/tests/Application.Tests.Integration/Rooms/Commands/DisableRoomTests.cs b/tests/Application.Tests.Integration/Rooms/Commands/DisableRoomTests.cs index 8b83f4da..9fefabc4 100644 --- a/tests/Application.Tests.Integration/Rooms/Commands/DisableRoomTests.cs +++ b/tests/Application.Tests.Integration/Rooms/Commands/DisableRoomTests.cs @@ -112,7 +112,7 @@ public async Task ShouldThrowInvalidOperationException_WhenRoomIsNotAvailable() var action = async () => await SendAsync(command); // Assert - await action.Should().ThrowAsync() + await action.Should().ThrowAsync() .WithMessage("Room have already been disabled."); // Cleanup diff --git a/tests/Application.Tests.Integration/Users/Commands/CreateUserTests.cs b/tests/Application.Tests.Integration/Users/Commands/AddUserTests.cs similarity index 87% rename from tests/Application.Tests.Integration/Users/Commands/CreateUserTests.cs rename to tests/Application.Tests.Integration/Users/Commands/AddUserTests.cs index 53c1fbdb..52a431c9 100644 --- a/tests/Application.Tests.Integration/Users/Commands/CreateUserTests.cs +++ b/tests/Application.Tests.Integration/Users/Commands/AddUserTests.cs @@ -1,4 +1,4 @@ -using Application.Users.Commands.CreateUser; +using Application.Users.Commands.AddUser; using Bogus; using Domain.Entities; using FluentAssertions; @@ -6,9 +6,9 @@ namespace Application.Tests.Integration.Users.Commands; -public class CreateUserTests : BaseClassFixture +public class AddUserTests : BaseClassFixture { - private readonly Faker _userGenerator = new Faker() + private readonly Faker _userGenerator = new Faker() .RuleFor(x => x.Username, faker => faker.Person.UserName) .RuleFor(x => x.Email, faker => faker.Person.Email) .RuleFor(x => x.FirstName, faker => faker.Person.FirstName) @@ -16,7 +16,7 @@ public class CreateUserTests : BaseClassFixture .RuleFor(x => x.Password, faker => faker.Random.String()) .RuleFor(x => x.Role, faker => faker.Random.Word()) .RuleFor(x => x.Position, faker => faker.Random.Word()); - public CreateUserTests(CustomApiFactory apiFactory) : base(apiFactory) + public AddUserTests(CustomApiFactory apiFactory) : base(apiFactory) { }