From c66c88b1af61da8195639f38f8f3b7840a5efe39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20H=C3=B8llund=20Kristensen?= Date: Thu, 11 Dec 2025 14:45:03 +0100 Subject: [PATCH] Updated register command to use our new register response --- .../Register/RegisterCommandHandlerTests.cs | 3 ++- .../DTOs/Login/RegisterResponse.cs | 9 +++++++++ .../Auth/Commands/Register/RegisterCommand.cs | 3 ++- .../Commands/Register/RegisterCommandHandler.cs | 15 ++++++++------- 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 AutoStack.Application/DTOs/Login/RegisterResponse.cs diff --git a/AutoStack.Application.Tests/Features/Auth/Commands/Register/RegisterCommandHandlerTests.cs b/AutoStack.Application.Tests/Features/Auth/Commands/Register/RegisterCommandHandlerTests.cs index bd356e8..f890d59 100644 --- a/AutoStack.Application.Tests/Features/Auth/Commands/Register/RegisterCommandHandlerTests.cs +++ b/AutoStack.Application.Tests/Features/Auth/Commands/Register/RegisterCommandHandlerTests.cs @@ -68,7 +68,8 @@ public async Task Handle_WithValidData_ShouldReturnSuccess() var result = await _handler.Handle(command, CancellationToken.None); result.IsSuccess.Should().BeTrue(); - result.Value.Should().BeTrue(); + result.Value.Should().NotBeNull(); + result.Value.UserId.Should().NotBeEmpty(); } [Fact] diff --git a/AutoStack.Application/DTOs/Login/RegisterResponse.cs b/AutoStack.Application/DTOs/Login/RegisterResponse.cs new file mode 100644 index 0000000..0edb0dd --- /dev/null +++ b/AutoStack.Application/DTOs/Login/RegisterResponse.cs @@ -0,0 +1,9 @@ +namespace AutoStack.Application.DTOs.Login; + +/// +/// Response DTO for user registration containing the newly created user's ID. +/// +/// The unique identifier of the newly registered user. +public record RegisterResponse( + Guid UserId +); diff --git a/AutoStack.Application/Features/Auth/Commands/Register/RegisterCommand.cs b/AutoStack.Application/Features/Auth/Commands/Register/RegisterCommand.cs index 32b3d35..50cacbe 100644 --- a/AutoStack.Application/Features/Auth/Commands/Register/RegisterCommand.cs +++ b/AutoStack.Application/Features/Auth/Commands/Register/RegisterCommand.cs @@ -1,4 +1,5 @@ using AutoStack.Application.Common.Interfaces.Commands; +using AutoStack.Application.DTOs.Login; namespace AutoStack.Application.Features.Auth.Commands.Register; @@ -9,4 +10,4 @@ namespace AutoStack.Application.Features.Auth.Commands.Register; /// The username for the new user /// The password for the new user /// The password confirmation -public record RegisterCommand(string Email, string Username, string Password, string ConfirmPassword) : ICommand; \ No newline at end of file +public record RegisterCommand(string Email, string Username, string Password, string ConfirmPassword) : ICommand; \ No newline at end of file diff --git a/AutoStack.Application/Features/Auth/Commands/Register/RegisterCommandHandler.cs b/AutoStack.Application/Features/Auth/Commands/Register/RegisterCommandHandler.cs index 84b17e4..cb0ae5d 100644 --- a/AutoStack.Application/Features/Auth/Commands/Register/RegisterCommandHandler.cs +++ b/AutoStack.Application/Features/Auth/Commands/Register/RegisterCommandHandler.cs @@ -3,6 +3,7 @@ using AutoStack.Application.Common.Interfaces.Auth; using AutoStack.Application.Common.Interfaces.Commands; using AutoStack.Application.Common.Models; +using AutoStack.Application.DTOs.Login; using AutoStack.Domain.Entities; using AutoStack.Domain.Enums; using AutoStack.Domain.Repositories; @@ -13,7 +14,7 @@ namespace AutoStack.Application.Features.Auth.Commands.Register; /// /// Handles the registration command by creating a new user account /// -public class RegisterCommandHandler : ICommandHandler +public class RegisterCommandHandler : ICommandHandler { private readonly IUserRepository _userRepository; private readonly IPasswordHasher _passwordHasher; @@ -43,8 +44,8 @@ public RegisterCommandHandler( /// /// The registration command containing user details /// The cancellation token - /// A result indicating success or failure with an error message - public async Task> Handle(RegisterCommand request, CancellationToken cancellationToken) + /// A result containing the registered user's ID or failure with an error message + public async Task> Handle(RegisterCommand request, CancellationToken cancellationToken) { if (await _userRepository.EmailExists(request.Email.ToLower(), cancellationToken)) { @@ -69,7 +70,7 @@ await _auditLogService.LogAsync(new AuditLogRequest // Ignore logging failures } - return Result.Failure("Email already exists"); + return Result.Failure("Email already exists"); } if (await _userRepository.UsernameExists(request.Username.ToLower(), cancellationToken)) @@ -95,12 +96,12 @@ await _auditLogService.LogAsync(new AuditLogRequest // Ignore logging failures } - return Result.Failure("Username already exists"); + return Result.Failure("Username already exists"); } if (request.Password != request.ConfirmPassword) { - return Result.Failure("Passwords do not match"); + return Result.Failure("Passwords do not match"); } var user = User.CreateUser(request.Email.ToLower(), request.Username.ToLower()); @@ -143,7 +144,7 @@ await _auditLogService.LogAsync(new AuditLogRequest // Ignore logging failures } - return Result.Success(true); + return Result.Success(new RegisterResponse(user.Id)); } private async Task SendVerificationEmail(string email, string username, string code, int expiryMinutes)