0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-07-02 03:22:57 +00:00

Apply suggestions and fix warnings

This commit is contained in:
alex289 2023-03-22 19:25:44 +01:00
parent df5530c726
commit 96b82e2d6f
No known key found for this signature in database
GPG Key ID: 573F77CD2D87F863
20 changed files with 47 additions and 73 deletions

View File

@ -36,7 +36,7 @@ public class UserController : ApiController
} }
[Authorize] [Authorize]
[HttpGet("{id}")] [HttpGet("{id:guid}")]
[SwaggerOperation("Get a user by id")] [SwaggerOperation("Get a user by id")]
[SwaggerResponse(200, "Request successful", typeof(ResponseMessage<UserViewModel>))] [SwaggerResponse(200, "Request successful", typeof(ResponseMessage<UserViewModel>))]
public async Task<IActionResult> GetUserByIdAsync( public async Task<IActionResult> GetUserByIdAsync(
@ -67,7 +67,7 @@ public class UserController : ApiController
} }
[Authorize] [Authorize]
[HttpDelete("{id}")] [HttpDelete("{id:guid}")]
[SwaggerOperation("Delete a user")] [SwaggerOperation("Delete a user")]
[SwaggerResponse(200, "Request successful", typeof(ResponseMessage<Guid>))] [SwaggerResponse(200, "Request successful", typeof(ResponseMessage<Guid>))]
public async Task<IActionResult> DeleteUserAsync([FromRoute] Guid id) public async Task<IActionResult> DeleteUserAsync([FromRoute] Guid id)

View File

@ -135,7 +135,7 @@ TokenValidationParameters CreateTokenValidationParameters()
return result; return result;
} }
// Needed for integration tests webapplication factory // Needed for integration tests web application factory
public partial class Program public partial class Program
{ {
} }

View File

@ -22,9 +22,10 @@ public sealed class GetAllUsersQueryHandlerTests
_fixture.VerifyNoDomainNotification(); _fixture.VerifyNoDomainNotification();
result.Should().NotBeNull(); var userViewModels = result.ToArray();
result.Should().ContainSingle(); userViewModels.Should().NotBeNull();
result.FirstOrDefault()!.Id.Should().Be(_fixture.ExistingUserId); userViewModels.Should().ContainSingle();
userViewModels.FirstOrDefault()!.Id.Should().Be(_fixture.ExistingUserId);
} }
[Fact] [Fact]

View File

@ -23,4 +23,4 @@ public sealed class UserViewModel
Role = user.Role Role = user.Role
}; };
} }
} }

View File

@ -21,8 +21,8 @@ public sealed class ChangePasswordCommandTestFixture : CommandHandlerFixtureBase
User.Object); User.Object);
} }
public ChangePasswordCommandHandler CommandHandler { get; set; } public ChangePasswordCommandHandler CommandHandler { get; }
public Mock<IUserRepository> UserRepository { get; set; } private Mock<IUserRepository> UserRepository { get; }
public Entities.User SetupUser() public Entities.User SetupUser()
{ {

View File

@ -87,7 +87,7 @@ public sealed class ChangePasswordCommandValidationTests :
ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword); ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword);
} }
private ChangePasswordCommand CreateTestCommand( private static ChangePasswordCommand CreateTestCommand(
string? password = null, string? newPassword = null) string? password = null, string? newPassword = null)
{ {
return new( return new(

View File

@ -176,7 +176,7 @@ public sealed class CreateUserCommandValidationTests :
ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword); ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword);
} }
private CreateUserCommand CreateTestCommand( private static CreateUserCommand CreateTestCommand(
Guid? userId = null, Guid? userId = null,
string? email = null, string? email = null,
string? surName = null, string? surName = null,

View File

@ -31,7 +31,7 @@ public sealed class DeleteUserCommandValidationTests :
"User id may not be empty"); "User id may not be empty");
} }
private DeleteUserCommand CreateTestCommand(Guid? userId = null) private static DeleteUserCommand CreateTestCommand(Guid? userId = null)
{ {
return new(userId ?? Guid.NewGuid()); return new(userId ?? Guid.NewGuid());
} }

View File

@ -120,7 +120,7 @@ public sealed class LoginUserCommandValidationTests :
ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword); ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword);
} }
private LoginUserCommand CreateTestCommand( private static LoginUserCommand CreateTestCommand(
string? email = null, string? email = null,
string? password = null) string? password = null)
{ {

View File

@ -51,7 +51,7 @@ public sealed class ApiUser : IUser
if (!string.IsNullOrWhiteSpace(claim?.Value)) if (!string.IsNullOrWhiteSpace(claim?.Value))
{ {
return claim?.Value!; return claim.Value;
} }
throw new ArgumentException("Could not parse user email"); throw new ArgumentException("Could not parse user email");

View File

@ -9,7 +9,7 @@ namespace CleanArchitecture.Domain.Commands;
public abstract class CommandHandlerBase public abstract class CommandHandlerBase
{ {
protected readonly IMediatorHandler _bus; protected readonly IMediatorHandler Bus;
private readonly DomainNotificationHandler _notifications; private readonly DomainNotificationHandler _notifications;
private readonly IUnitOfWork _unitOfWork; private readonly IUnitOfWork _unitOfWork;
@ -18,12 +18,12 @@ public abstract class CommandHandlerBase
IUnitOfWork unitOfWork, IUnitOfWork unitOfWork,
INotificationHandler<DomainNotification> notifications) INotificationHandler<DomainNotification> notifications)
{ {
_bus = bus; Bus = bus;
_unitOfWork = unitOfWork; _unitOfWork = unitOfWork;
_notifications = (DomainNotificationHandler)notifications; _notifications = (DomainNotificationHandler)notifications;
} }
public async Task<bool> CommitAsync() protected async Task<bool> CommitAsync()
{ {
if (_notifications.HasNotifications()) if (_notifications.HasNotifications())
{ {
@ -35,7 +35,7 @@ public abstract class CommandHandlerBase
return true; return true;
} }
await _bus.RaiseEventAsync( await Bus.RaiseEventAsync(
new DomainNotification( new DomainNotification(
"Commit", "Commit",
"Problem occured while saving the data. Please try again.", "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) protected async Task NotifyAsync(string key, string message, string code)
{ {
await _bus.RaiseEventAsync( await Bus.RaiseEventAsync(
new DomainNotification(key, message, code)); new DomainNotification(key, message, code));
} }
protected async Task NotifyAsync(DomainNotification notification) protected async Task NotifyAsync(DomainNotification notification)
{ {
await _bus.RaiseEventAsync(notification); await Bus.RaiseEventAsync(notification);
} }
protected async ValueTask<bool> TestValidityAsync(CommandBase command) protected async ValueTask<bool> TestValidityAsync(CommandBase command)

View File

@ -65,7 +65,7 @@ public sealed class ChangePasswordCommandHandler : CommandHandlerBase,
if (await CommitAsync()) if (await CommitAsync())
{ {
await _bus.RaiseEventAsync(new PasswordChangedEvent(user.Id)); await Bus.RaiseEventAsync(new PasswordChangedEvent(user.Id));
} }
} }
} }

View File

@ -37,7 +37,7 @@ public sealed class CreateUserCommandHandler : CommandHandlerBase,
if (existingUser != null) if (existingUser != null)
{ {
await _bus.RaiseEventAsync( await Bus.RaiseEventAsync(
new DomainNotification( new DomainNotification(
request.MessageType, request.MessageType,
$"There is already a User with Id {request.UserId}", $"There is already a User with Id {request.UserId}",
@ -49,7 +49,7 @@ public sealed class CreateUserCommandHandler : CommandHandlerBase,
if (existingUser != null) if (existingUser != null)
{ {
await _bus.RaiseEventAsync( await Bus.RaiseEventAsync(
new DomainNotification( new DomainNotification(
request.MessageType, request.MessageType,
$"There is already a User with Email {request.Email}", $"There is already a User with Email {request.Email}",
@ -71,7 +71,7 @@ public sealed class CreateUserCommandHandler : CommandHandlerBase,
if (await CommitAsync()) if (await CommitAsync())
{ {
await _bus.RaiseEventAsync(new UserCreatedEvent(user.Id)); await Bus.RaiseEventAsync(new UserCreatedEvent(user.Id));
} }
} }
} }

View File

@ -62,7 +62,7 @@ public sealed class DeleteUserCommandHandler : CommandHandlerBase,
if (await CommitAsync()) if (await CommitAsync())
{ {
await _bus.RaiseEventAsync(new UserDeletedEvent(request.UserId)); await Bus.RaiseEventAsync(new UserDeletedEvent(request.UserId));
} }
} }
} }

View File

@ -76,7 +76,7 @@ public sealed class LoginUserCommandHandler : CommandHandlerBase,
_tokenSettings); _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[] var claims = new[]
{ {

View File

@ -38,7 +38,7 @@ public sealed class UpdateUserCommandHandler : CommandHandlerBase,
if (user == null) if (user == null)
{ {
await _bus.RaiseEventAsync( await Bus.RaiseEventAsync(
new DomainNotification( new DomainNotification(
request.MessageType, request.MessageType,
$"There is no User with Id {request.UserId}", $"There is no User with Id {request.UserId}",
@ -70,7 +70,7 @@ public sealed class UpdateUserCommandHandler : CommandHandlerBase,
if (await CommitAsync()) if (await CommitAsync())
{ {
await _bus.RaiseEventAsync(new UserUpdatedEvent(user.Id)); await Bus.RaiseEventAsync(new UserUpdatedEvent(user.Id));
} }
} }
} }

View File

@ -31,31 +31,4 @@ public abstract class Entity
{ {
Deleted = false; 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 + "]";
}
} }

View File

@ -16,9 +16,9 @@ public sealed class DomainNotificationHandlerTests
[Fact] [Fact]
public void Should_Handle_DomainNotification() public void Should_Handle_DomainNotification()
{ {
var key = "Key"; const string key = "Key";
var value = "Value"; const string value = "Value";
var code = "Code"; const string code = "Code";
var domainNotification = new DomainNotification(key, value, code); var domainNotification = new DomainNotification(key, value, code);
var domainNotificationHandler = new DomainNotificationHandler(); var domainNotificationHandler = new DomainNotificationHandler();
@ -29,9 +29,9 @@ public sealed class DomainNotificationHandlerTests
[Fact] [Fact]
public void Should_Handle_DomainNotification_Overload() public void Should_Handle_DomainNotification_Overload()
{ {
var key = "Key"; const string key = "Key";
var value = "Value"; const string value = "Value";
var code = "Code"; const string code = "Code";
var domainNotification = new DomainNotification(key, value, code); var domainNotification = new DomainNotification(key, value, code);
var domainNotificationHandler = new DomainNotificationHandler(); var domainNotificationHandler = new DomainNotificationHandler();
@ -42,9 +42,9 @@ public sealed class DomainNotificationHandlerTests
[Fact] [Fact]
public void DomainNotification_HasNotifications_After_Handling_One() public void DomainNotification_HasNotifications_After_Handling_One()
{ {
var key = "Key"; const string key = "Key";
var value = "Value"; const string value = "Value";
var code = "Code"; const string code = "Code";
var domainNotification = new DomainNotification(key, value, code); var domainNotification = new DomainNotification(key, value, code);
var domainNotificationHandler = new DomainNotificationHandler(); var domainNotificationHandler = new DomainNotificationHandler();

View File

@ -10,9 +10,9 @@ public sealed class DomainNotificationTests
[Fact] [Fact]
public void Should_Create_DomainNotification_Instance() public void Should_Create_DomainNotification_Instance()
{ {
var key = "Key"; const string key = "Key";
var value = "Value"; const string value = "Value";
var code = "Code"; const string code = "Code";
var domainNotification = new DomainNotification( var domainNotification = new DomainNotification(
key, value, code); key, value, code);
@ -26,9 +26,9 @@ public sealed class DomainNotificationTests
[Fact] [Fact]
public void Should_Create_DomainNotification_Overload_Instance() public void Should_Create_DomainNotification_Overload_Instance()
{ {
var key = "Key"; const string key = "Key";
var value = "Value"; const string value = "Value";
var code = "Code"; const string code = "Code";
var domainNotification = new DomainNotification( var domainNotification = new DomainNotification(
key, value, code); key, value, code);

View File

@ -19,9 +19,9 @@ public sealed class InMemoryBusTests
var inMemoryBus = new InMemoryBus(mediator.Object); var inMemoryBus = new InMemoryBus(mediator.Object);
var key = "Key"; const string key = "Key";
var value = "Value"; const string value = "Value";
var code = "Code"; const string code = "Code";
var domainEvent = new DomainNotification(key, value, code); var domainEvent = new DomainNotification(key, value, code);