0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-06-30 10:33:43 +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]
[HttpGet("{id}")]
[HttpGet("{id:guid}")]
[SwaggerOperation("Get a user by id")]
[SwaggerResponse(200, "Request successful", typeof(ResponseMessage<UserViewModel>))]
public async Task<IActionResult> 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<Guid>))]
public async Task<IActionResult> DeleteUserAsync([FromRoute] Guid id)

View File

@ -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
{
}

View File

@ -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]

View File

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

View File

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

View File

@ -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(

View File

@ -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,

View File

@ -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());
}

View File

@ -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)
{

View File

@ -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");

View File

@ -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<DomainNotification> notifications)
{
_bus = bus;
Bus = bus;
_unitOfWork = unitOfWork;
_notifications = (DomainNotificationHandler)notifications;
}
public async Task<bool> CommitAsync()
protected async Task<bool> 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<bool> TestValidityAsync(CommandBase command)

View File

@ -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));
}
}
}

View File

@ -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));
}
}
}

View File

@ -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));
}
}
}

View File

@ -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[]
{

View File

@ -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));
}
}
}

View File

@ -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 + "]";
}
}

View File

@ -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();

View File

@ -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);

View File

@ -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);