From 96b82e2d6fbd67e2dab1fe2514a21919749d5ecd Mon Sep 17 00:00:00 2001 From: alex289 Date: Wed, 22 Mar 2023 19:25:44 +0100 Subject: [PATCH] Apply suggestions and fix warnings --- .../Controllers/UserController.cs | 4 +-- CleanArchitecture.Api/Program.cs | 2 +- .../Users/GetAllUsersQueryHandlerTests.cs | 7 ++--- .../viewmodels/Users/UserViewModel.cs | 2 +- .../ChangePasswordCommandTestFixture.cs | 4 +-- .../ChangePasswordCommandValidationTests.cs | 2 +- .../CreateUserCommandValidationTests.cs | 2 +- .../DeleteUserCommandValidationTests.cs | 2 +- .../LoginUserCommandValidationTests.cs | 2 +- CleanArchitecture.Domain/ApiUser.cs | 2 +- .../Commands/CommandHandlerBase.cs | 12 ++++----- .../ChangePasswordCommandHandler.cs | 2 +- .../CreateUser/CreateUserCommandHandler.cs | 6 ++--- .../DeleteUser/DeleteUserCommandHandler.cs | 2 +- .../LoginUser/LoginUserCommandHandler.cs | 2 +- .../UpdateUser/UpdateUserCommandHandler.cs | 4 +-- CleanArchitecture.Domain/Entities/Entity.cs | 27 ------------------- .../DomainNotificationHandlerTests.cs | 18 ++++++------- .../DomainNotificationTests.cs | 12 ++++----- .../InMemoryBusTests.cs | 6 ++--- 20 files changed, 47 insertions(+), 73 deletions(-) diff --git a/CleanArchitecture.Api/Controllers/UserController.cs b/CleanArchitecture.Api/Controllers/UserController.cs index 7df8108..d3986dd 100644 --- a/CleanArchitecture.Api/Controllers/UserController.cs +++ b/CleanArchitecture.Api/Controllers/UserController.cs @@ -36,7 +36,7 @@ public class UserController : ApiController } [Authorize] - [HttpGet("{id}")] + [HttpGet("{id:guid}")] [SwaggerOperation("Get a user by id")] [SwaggerResponse(200, "Request successful", typeof(ResponseMessage))] public async Task GetUserByIdAsync( @@ -67,7 +67,7 @@ public class UserController : ApiController } [Authorize] - [HttpDelete("{id}")] + [HttpDelete("{id:guid}")] [SwaggerOperation("Delete a user")] [SwaggerResponse(200, "Request successful", typeof(ResponseMessage))] public async Task DeleteUserAsync([FromRoute] Guid id) diff --git a/CleanArchitecture.Api/Program.cs b/CleanArchitecture.Api/Program.cs index 6bfc87b..b828705 100644 --- a/CleanArchitecture.Api/Program.cs +++ b/CleanArchitecture.Api/Program.cs @@ -135,7 +135,7 @@ TokenValidationParameters CreateTokenValidationParameters() return result; } -// Needed for integration tests webapplication factory +// Needed for integration tests web application factory public partial class Program { } \ No newline at end of file diff --git a/CleanArchitecture.Application.Tests/Queries/Users/GetAllUsersQueryHandlerTests.cs b/CleanArchitecture.Application.Tests/Queries/Users/GetAllUsersQueryHandlerTests.cs index 7f2f3a5..816c374 100644 --- a/CleanArchitecture.Application.Tests/Queries/Users/GetAllUsersQueryHandlerTests.cs +++ b/CleanArchitecture.Application.Tests/Queries/Users/GetAllUsersQueryHandlerTests.cs @@ -22,9 +22,10 @@ public sealed class GetAllUsersQueryHandlerTests _fixture.VerifyNoDomainNotification(); - result.Should().NotBeNull(); - result.Should().ContainSingle(); - result.FirstOrDefault()!.Id.Should().Be(_fixture.ExistingUserId); + var userViewModels = result.ToArray(); + userViewModels.Should().NotBeNull(); + userViewModels.Should().ContainSingle(); + userViewModels.FirstOrDefault()!.Id.Should().Be(_fixture.ExistingUserId); } [Fact] diff --git a/CleanArchitecture.Application/viewmodels/Users/UserViewModel.cs b/CleanArchitecture.Application/viewmodels/Users/UserViewModel.cs index cc0ac67..53f2aa9 100644 --- a/CleanArchitecture.Application/viewmodels/Users/UserViewModel.cs +++ b/CleanArchitecture.Application/viewmodels/Users/UserViewModel.cs @@ -23,4 +23,4 @@ public sealed class UserViewModel Role = user.Role }; } -} +} \ No newline at end of file diff --git a/CleanArchitecture.Domain.Tests/CommandHandler/User/ChangePassword/ChangePasswordCommandTestFixture.cs b/CleanArchitecture.Domain.Tests/CommandHandler/User/ChangePassword/ChangePasswordCommandTestFixture.cs index 77e8e55..74f24c7 100644 --- a/CleanArchitecture.Domain.Tests/CommandHandler/User/ChangePassword/ChangePasswordCommandTestFixture.cs +++ b/CleanArchitecture.Domain.Tests/CommandHandler/User/ChangePassword/ChangePasswordCommandTestFixture.cs @@ -21,8 +21,8 @@ public sealed class ChangePasswordCommandTestFixture : CommandHandlerFixtureBase User.Object); } - public ChangePasswordCommandHandler CommandHandler { get; set; } - public Mock UserRepository { get; set; } + public ChangePasswordCommandHandler CommandHandler { get; } + private Mock UserRepository { get; } public Entities.User SetupUser() { diff --git a/CleanArchitecture.Domain.Tests/CommandHandler/User/ChangePassword/ChangePasswordCommandValidationTests.cs b/CleanArchitecture.Domain.Tests/CommandHandler/User/ChangePassword/ChangePasswordCommandValidationTests.cs index 3b0339e..39c8186 100644 --- a/CleanArchitecture.Domain.Tests/CommandHandler/User/ChangePassword/ChangePasswordCommandValidationTests.cs +++ b/CleanArchitecture.Domain.Tests/CommandHandler/User/ChangePassword/ChangePasswordCommandValidationTests.cs @@ -87,7 +87,7 @@ public sealed class ChangePasswordCommandValidationTests : ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword); } - private ChangePasswordCommand CreateTestCommand( + private static ChangePasswordCommand CreateTestCommand( string? password = null, string? newPassword = null) { return new( diff --git a/CleanArchitecture.Domain.Tests/CommandHandler/User/CreateUser/CreateUserCommandValidationTests.cs b/CleanArchitecture.Domain.Tests/CommandHandler/User/CreateUser/CreateUserCommandValidationTests.cs index f3bbe6d..05a8d1b 100644 --- a/CleanArchitecture.Domain.Tests/CommandHandler/User/CreateUser/CreateUserCommandValidationTests.cs +++ b/CleanArchitecture.Domain.Tests/CommandHandler/User/CreateUser/CreateUserCommandValidationTests.cs @@ -176,7 +176,7 @@ public sealed class CreateUserCommandValidationTests : ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword); } - private CreateUserCommand CreateTestCommand( + private static CreateUserCommand CreateTestCommand( Guid? userId = null, string? email = null, string? surName = null, diff --git a/CleanArchitecture.Domain.Tests/CommandHandler/User/DeleteUser/DeleteUserCommandValidationTests.cs b/CleanArchitecture.Domain.Tests/CommandHandler/User/DeleteUser/DeleteUserCommandValidationTests.cs index 4ace62f..a2e6e45 100644 --- a/CleanArchitecture.Domain.Tests/CommandHandler/User/DeleteUser/DeleteUserCommandValidationTests.cs +++ b/CleanArchitecture.Domain.Tests/CommandHandler/User/DeleteUser/DeleteUserCommandValidationTests.cs @@ -31,7 +31,7 @@ public sealed class DeleteUserCommandValidationTests : "User id may not be empty"); } - private DeleteUserCommand CreateTestCommand(Guid? userId = null) + private static DeleteUserCommand CreateTestCommand(Guid? userId = null) { return new(userId ?? Guid.NewGuid()); } diff --git a/CleanArchitecture.Domain.Tests/CommandHandler/User/LoginUser/LoginUserCommandValidationTests.cs b/CleanArchitecture.Domain.Tests/CommandHandler/User/LoginUser/LoginUserCommandValidationTests.cs index 96fa7c3..fd85ce8 100644 --- a/CleanArchitecture.Domain.Tests/CommandHandler/User/LoginUser/LoginUserCommandValidationTests.cs +++ b/CleanArchitecture.Domain.Tests/CommandHandler/User/LoginUser/LoginUserCommandValidationTests.cs @@ -120,7 +120,7 @@ public sealed class LoginUserCommandValidationTests : ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword); } - private LoginUserCommand CreateTestCommand( + private static LoginUserCommand CreateTestCommand( string? email = null, string? password = null) { diff --git a/CleanArchitecture.Domain/ApiUser.cs b/CleanArchitecture.Domain/ApiUser.cs index 15912af..285bfd6 100644 --- a/CleanArchitecture.Domain/ApiUser.cs +++ b/CleanArchitecture.Domain/ApiUser.cs @@ -51,7 +51,7 @@ public sealed class ApiUser : IUser if (!string.IsNullOrWhiteSpace(claim?.Value)) { - return claim?.Value!; + return claim.Value; } throw new ArgumentException("Could not parse user email"); diff --git a/CleanArchitecture.Domain/Commands/CommandHandlerBase.cs b/CleanArchitecture.Domain/Commands/CommandHandlerBase.cs index 14d6877..54a557b 100644 --- a/CleanArchitecture.Domain/Commands/CommandHandlerBase.cs +++ b/CleanArchitecture.Domain/Commands/CommandHandlerBase.cs @@ -9,7 +9,7 @@ namespace CleanArchitecture.Domain.Commands; public abstract class CommandHandlerBase { - protected readonly IMediatorHandler _bus; + protected readonly IMediatorHandler Bus; private readonly DomainNotificationHandler _notifications; private readonly IUnitOfWork _unitOfWork; @@ -18,12 +18,12 @@ public abstract class CommandHandlerBase IUnitOfWork unitOfWork, INotificationHandler notifications) { - _bus = bus; + Bus = bus; _unitOfWork = unitOfWork; _notifications = (DomainNotificationHandler)notifications; } - public async Task CommitAsync() + protected async Task CommitAsync() { if (_notifications.HasNotifications()) { @@ -35,7 +35,7 @@ public abstract class CommandHandlerBase return true; } - await _bus.RaiseEventAsync( + await Bus.RaiseEventAsync( new DomainNotification( "Commit", "Problem occured while saving the data. Please try again.", @@ -46,13 +46,13 @@ public abstract class CommandHandlerBase protected async Task NotifyAsync(string key, string message, string code) { - await _bus.RaiseEventAsync( + await Bus.RaiseEventAsync( new DomainNotification(key, message, code)); } protected async Task NotifyAsync(DomainNotification notification) { - await _bus.RaiseEventAsync(notification); + await Bus.RaiseEventAsync(notification); } protected async ValueTask TestValidityAsync(CommandBase command) diff --git a/CleanArchitecture.Domain/Commands/Users/ChangePassword/ChangePasswordCommandHandler.cs b/CleanArchitecture.Domain/Commands/Users/ChangePassword/ChangePasswordCommandHandler.cs index 8b5ef24..38a85dd 100644 --- a/CleanArchitecture.Domain/Commands/Users/ChangePassword/ChangePasswordCommandHandler.cs +++ b/CleanArchitecture.Domain/Commands/Users/ChangePassword/ChangePasswordCommandHandler.cs @@ -65,7 +65,7 @@ public sealed class ChangePasswordCommandHandler : CommandHandlerBase, if (await CommitAsync()) { - await _bus.RaiseEventAsync(new PasswordChangedEvent(user.Id)); + await Bus.RaiseEventAsync(new PasswordChangedEvent(user.Id)); } } } \ No newline at end of file diff --git a/CleanArchitecture.Domain/Commands/Users/CreateUser/CreateUserCommandHandler.cs b/CleanArchitecture.Domain/Commands/Users/CreateUser/CreateUserCommandHandler.cs index 382a529..a639591 100644 --- a/CleanArchitecture.Domain/Commands/Users/CreateUser/CreateUserCommandHandler.cs +++ b/CleanArchitecture.Domain/Commands/Users/CreateUser/CreateUserCommandHandler.cs @@ -37,7 +37,7 @@ public sealed class CreateUserCommandHandler : CommandHandlerBase, if (existingUser != null) { - await _bus.RaiseEventAsync( + await Bus.RaiseEventAsync( new DomainNotification( request.MessageType, $"There is already a User with Id {request.UserId}", @@ -49,7 +49,7 @@ public sealed class CreateUserCommandHandler : CommandHandlerBase, if (existingUser != null) { - await _bus.RaiseEventAsync( + await Bus.RaiseEventAsync( new DomainNotification( request.MessageType, $"There is already a User with Email {request.Email}", @@ -71,7 +71,7 @@ public sealed class CreateUserCommandHandler : CommandHandlerBase, if (await CommitAsync()) { - await _bus.RaiseEventAsync(new UserCreatedEvent(user.Id)); + await Bus.RaiseEventAsync(new UserCreatedEvent(user.Id)); } } } \ No newline at end of file diff --git a/CleanArchitecture.Domain/Commands/Users/DeleteUser/DeleteUserCommandHandler.cs b/CleanArchitecture.Domain/Commands/Users/DeleteUser/DeleteUserCommandHandler.cs index d40495f..1fc4970 100644 --- a/CleanArchitecture.Domain/Commands/Users/DeleteUser/DeleteUserCommandHandler.cs +++ b/CleanArchitecture.Domain/Commands/Users/DeleteUser/DeleteUserCommandHandler.cs @@ -62,7 +62,7 @@ public sealed class DeleteUserCommandHandler : CommandHandlerBase, if (await CommitAsync()) { - await _bus.RaiseEventAsync(new UserDeletedEvent(request.UserId)); + await Bus.RaiseEventAsync(new UserDeletedEvent(request.UserId)); } } } \ No newline at end of file diff --git a/CleanArchitecture.Domain/Commands/Users/LoginUser/LoginUserCommandHandler.cs b/CleanArchitecture.Domain/Commands/Users/LoginUser/LoginUserCommandHandler.cs index c93e2c1..06cb8d7 100644 --- a/CleanArchitecture.Domain/Commands/Users/LoginUser/LoginUserCommandHandler.cs +++ b/CleanArchitecture.Domain/Commands/Users/LoginUser/LoginUserCommandHandler.cs @@ -76,7 +76,7 @@ public sealed class LoginUserCommandHandler : CommandHandlerBase, _tokenSettings); } - public static string BuildToken(string email, UserRole role, Guid id, TokenSettings tokenSettings) + private static string BuildToken(string email, UserRole role, Guid id, TokenSettings tokenSettings) { var claims = new[] { diff --git a/CleanArchitecture.Domain/Commands/Users/UpdateUser/UpdateUserCommandHandler.cs b/CleanArchitecture.Domain/Commands/Users/UpdateUser/UpdateUserCommandHandler.cs index 02d87cd..6f882e3 100644 --- a/CleanArchitecture.Domain/Commands/Users/UpdateUser/UpdateUserCommandHandler.cs +++ b/CleanArchitecture.Domain/Commands/Users/UpdateUser/UpdateUserCommandHandler.cs @@ -38,7 +38,7 @@ public sealed class UpdateUserCommandHandler : CommandHandlerBase, if (user == null) { - await _bus.RaiseEventAsync( + await Bus.RaiseEventAsync( new DomainNotification( request.MessageType, $"There is no User with Id {request.UserId}", @@ -70,7 +70,7 @@ public sealed class UpdateUserCommandHandler : CommandHandlerBase, if (await CommitAsync()) { - await _bus.RaiseEventAsync(new UserUpdatedEvent(user.Id)); + await Bus.RaiseEventAsync(new UserUpdatedEvent(user.Id)); } } } \ No newline at end of file diff --git a/CleanArchitecture.Domain/Entities/Entity.cs b/CleanArchitecture.Domain/Entities/Entity.cs index 395e70e..99a272a 100644 --- a/CleanArchitecture.Domain/Entities/Entity.cs +++ b/CleanArchitecture.Domain/Entities/Entity.cs @@ -31,31 +31,4 @@ public abstract class Entity { Deleted = false; } - - public override bool Equals(object? obj) - { - var compareTo = obj as Entity; - - if (ReferenceEquals(this, compareTo)) - { - return true; - } - - if (compareTo is null) - { - return false; - } - - return Id == compareTo.Id; - } - - public override int GetHashCode() - { - return GetType().GetHashCode() * 907 + Id.GetHashCode(); - } - - public override string ToString() - { - return GetType().Name + " [Id=" + Id + "]"; - } } \ No newline at end of file diff --git a/CleanArchitecture.Infrastructure.Tests/DomainNotificationHandlerTests.cs b/CleanArchitecture.Infrastructure.Tests/DomainNotificationHandlerTests.cs index 4ebf2a2..15a24ed 100644 --- a/CleanArchitecture.Infrastructure.Tests/DomainNotificationHandlerTests.cs +++ b/CleanArchitecture.Infrastructure.Tests/DomainNotificationHandlerTests.cs @@ -16,9 +16,9 @@ public sealed class DomainNotificationHandlerTests [Fact] public void Should_Handle_DomainNotification() { - var key = "Key"; - var value = "Value"; - var code = "Code"; + const string key = "Key"; + const string value = "Value"; + const string code = "Code"; var domainNotification = new DomainNotification(key, value, code); var domainNotificationHandler = new DomainNotificationHandler(); @@ -29,9 +29,9 @@ public sealed class DomainNotificationHandlerTests [Fact] public void Should_Handle_DomainNotification_Overload() { - var key = "Key"; - var value = "Value"; - var code = "Code"; + const string key = "Key"; + const string value = "Value"; + const string code = "Code"; var domainNotification = new DomainNotification(key, value, code); var domainNotificationHandler = new DomainNotificationHandler(); @@ -42,9 +42,9 @@ public sealed class DomainNotificationHandlerTests [Fact] public void DomainNotification_HasNotifications_After_Handling_One() { - var key = "Key"; - var value = "Value"; - var code = "Code"; + const string key = "Key"; + const string value = "Value"; + const string code = "Code"; var domainNotification = new DomainNotification(key, value, code); var domainNotificationHandler = new DomainNotificationHandler(); diff --git a/CleanArchitecture.Infrastructure.Tests/DomainNotificationTests.cs b/CleanArchitecture.Infrastructure.Tests/DomainNotificationTests.cs index f34d1c0..14d90b6 100644 --- a/CleanArchitecture.Infrastructure.Tests/DomainNotificationTests.cs +++ b/CleanArchitecture.Infrastructure.Tests/DomainNotificationTests.cs @@ -10,9 +10,9 @@ public sealed class DomainNotificationTests [Fact] public void Should_Create_DomainNotification_Instance() { - var key = "Key"; - var value = "Value"; - var code = "Code"; + const string key = "Key"; + const string value = "Value"; + const string code = "Code"; var domainNotification = new DomainNotification( key, value, code); @@ -26,9 +26,9 @@ public sealed class DomainNotificationTests [Fact] public void Should_Create_DomainNotification_Overload_Instance() { - var key = "Key"; - var value = "Value"; - var code = "Code"; + const string key = "Key"; + const string value = "Value"; + const string code = "Code"; var domainNotification = new DomainNotification( key, value, code); diff --git a/CleanArchitecture.Infrastructure.Tests/InMemoryBusTests.cs b/CleanArchitecture.Infrastructure.Tests/InMemoryBusTests.cs index f3ed4d8..382ccc2 100644 --- a/CleanArchitecture.Infrastructure.Tests/InMemoryBusTests.cs +++ b/CleanArchitecture.Infrastructure.Tests/InMemoryBusTests.cs @@ -19,9 +19,9 @@ public sealed class InMemoryBusTests var inMemoryBus = new InMemoryBus(mediator.Object); - var key = "Key"; - var value = "Value"; - var code = "Code"; + const string key = "Key"; + const string value = "Value"; + const string code = "Code"; var domainEvent = new DomainNotification(key, value, code);