Modified commands to use auth and current user

This commit is contained in:
Veljko Tosic
2026-02-12 19:51:27 +01:00
parent 072d4e3f46
commit df2130ec24
6 changed files with 78 additions and 4 deletions

View File

@@ -0,0 +1,6 @@
using AipsCore.Application.Abstract.Command;
using AipsCore.Application.Authentication;
namespace AipsCore.Application.Models.User.Command.LogIn;
public record LogInUserCommand(string Email, string Password) : ICommand<Token>;

View File

@@ -0,0 +1,28 @@
using AipsCore.Application.Abstract.Command;
using AipsCore.Application.Abstract.UserContext;
using AipsCore.Application.Authentication;
using AipsCore.Domain.Abstract;
using AipsCore.Domain.Models.User.External;
namespace AipsCore.Application.Models.User.Command.LogIn;
public class LogInUserCommandHandler : ICommandHandler<LogInUserCommand, Token>
{
private readonly IUserRepository _userRepository;
private readonly ITokenProvider _tokenProvider;
private readonly IUnitOfWork _unitOfWork;
public LogInUserCommandHandler(IUserRepository userRepository, ITokenProvider tokenProvider, IUnitOfWork unitOfWork)
{
_userRepository = userRepository;
_tokenProvider = tokenProvider;
_unitOfWork = unitOfWork;
}
public async Task<Token> Handle(LogInUserCommand command, CancellationToken cancellationToken = default)
{
var loginResult = await _userRepository.LoginWithEmailAndPasswordAsync(command.Email, command.Password, cancellationToken);
return new Token(_tokenProvider.Generate(loginResult.User, loginResult.Roles));
}
}

View File

@@ -0,0 +1,9 @@
using AipsCore.Application.Abstract.Command;
using AipsCore.Domain.Models.User.ValueObjects;
namespace AipsCore.Application.Models.User.Command.SignUp;
public record SignUpUserCommand(
string Username,
string Email,
string Password) : ICommand<UserId>;

View File

@@ -0,0 +1,27 @@
using AipsCore.Application.Abstract.Command;
using AipsCore.Domain.Abstract;
using AipsCore.Domain.Models.User.External;
using AipsCore.Domain.Models.User.ValueObjects;
namespace AipsCore.Application.Models.User.Command.SignUp;
public class SignUpUserCommandHandler : ICommandHandler<SignUpUserCommand, UserId>
{
private readonly IUserRepository _userRepository;
private readonly IUnitOfWork _unitOfWork;
public SignUpUserCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork)
{
_userRepository = userRepository;
_unitOfWork = unitOfWork;
}
public async Task<UserId> Handle(SignUpUserCommand command, CancellationToken cancellationToken = default)
{
var user = Domain.Models.User.User.Create(command.Email, command.Username);
await _userRepository.SignUpWithPasswordAsync(user, command.Password, cancellationToken);
return user.Id;
}
}