Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions AutoStack.Application/DTOs/Login/RegisterResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AutoStack.Application.DTOs.Login;

/// <summary>
/// Response DTO for user registration containing the newly created user's ID.
/// </summary>
/// <param name="UserId">The unique identifier of the newly registered user.</param>
public record RegisterResponse(
Guid UserId
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoStack.Application.Common.Interfaces.Commands;
using AutoStack.Application.DTOs.Login;

namespace AutoStack.Application.Features.Auth.Commands.Register;

Expand All @@ -9,4 +10,4 @@ namespace AutoStack.Application.Features.Auth.Commands.Register;
/// <param name="Username">The username for the new user</param>
/// <param name="Password">The password for the new user</param>
/// <param name="ConfirmPassword">The password confirmation</param>
public record RegisterCommand(string Email, string Username, string Password, string ConfirmPassword) : ICommand<bool>;
public record RegisterCommand(string Email, string Username, string Password, string ConfirmPassword) : ICommand<RegisterResponse>;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +14,7 @@ namespace AutoStack.Application.Features.Auth.Commands.Register;
/// <summary>
/// Handles the registration command by creating a new user account
/// </summary>
public class RegisterCommandHandler : ICommandHandler<RegisterCommand, bool>
public class RegisterCommandHandler : ICommandHandler<RegisterCommand, RegisterResponse>
{
private readonly IUserRepository _userRepository;
private readonly IPasswordHasher _passwordHasher;
Expand Down Expand Up @@ -43,8 +44,8 @@ public RegisterCommandHandler(
/// </summary>
/// <param name="request">The registration command containing user details</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>A result indicating success or failure with an error message</returns>
public async Task<Result<bool>> Handle(RegisterCommand request, CancellationToken cancellationToken)
/// <returns>A result containing the registered user's ID or failure with an error message</returns>
public async Task<Result<RegisterResponse>> Handle(RegisterCommand request, CancellationToken cancellationToken)
{
if (await _userRepository.EmailExists(request.Email.ToLower(), cancellationToken))
{
Expand All @@ -69,7 +70,7 @@ await _auditLogService.LogAsync(new AuditLogRequest
// Ignore logging failures
}

return Result<bool>.Failure("Email already exists");
return Result<RegisterResponse>.Failure("Email already exists");
}

if (await _userRepository.UsernameExists(request.Username.ToLower(), cancellationToken))
Expand All @@ -95,12 +96,12 @@ await _auditLogService.LogAsync(new AuditLogRequest
// Ignore logging failures
}

return Result<bool>.Failure("Username already exists");
return Result<RegisterResponse>.Failure("Username already exists");
}

if (request.Password != request.ConfirmPassword)
{
return Result<bool>.Failure("Passwords do not match");
return Result<RegisterResponse>.Failure("Passwords do not match");
}

var user = User.CreateUser(request.Email.ToLower(), request.Username.ToLower());
Expand Down Expand Up @@ -143,7 +144,7 @@ await _auditLogService.LogAsync(new AuditLogRequest
// Ignore logging failures
}

return Result<bool>.Success(true);
return Result<RegisterResponse>.Success(new RegisterResponse(user.Id));
}

private async Task SendVerificationEmail(string email, string username, string code, int expiryMinutes)
Expand Down
Loading