299 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			299 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Core.Thalos.Application.UseCases.Users.Input;
 | |
| using Core.Thalos.Application.UseCases.Users.Ports;
 | |
| using Core.Thalos.BuildingBlocks;
 | |
| using Core.Thalos.External.Clients;
 | |
| using Core.Thalos.External.Clients.Requests;
 | |
| using FluentValidation;
 | |
| using Lib.Architecture.BuildingBlocks;
 | |
| using Lib.Architecture.BuildingBlocks.Helpers;
 | |
| namespace Core.Thalos.Application.UseCases.Users
 | |
| {
 | |
|     public class UserHandler :
 | |
|         IComponentHandler<ChangeUserStatusRequest>,
 | |
|         IComponentHandler<GetAllUsersRequest>,
 | |
|         IComponentHandler<UpdateUserRequest>,
 | |
|         IComponentHandler<GetUserRequest>,
 | |
|         IComponentHandler<GetUserByEmailRequest>,
 | |
|         IComponentHandler<CreateUserRequest>,
 | |
|         IComponentHandler<LoginUserRequest>,
 | |
|         IComponentHandler<LogoutUserRequest>,
 | |
|         IComponentHandler<ValidateUserExistenceRequest>,
 | |
|         IComponentHandler<GetTokenAdapterRequest>
 | |
|     {
 | |
|         private readonly IUserPort _port;
 | |
|         private readonly IValidator<ChangeUserStatusRequest> _changeUserStatusValidator;
 | |
|         private readonly IValidator<CreateUserRequest> _registerUserValidator;
 | |
|         private readonly IValidator<UpdateUserRequest> _updateUserValidator;
 | |
|         private readonly IThalosServiceClient _thalosDALService;
 | |
| 
 | |
|         public UserHandler(
 | |
|             IUserPort port,
 | |
|             IValidator<ChangeUserStatusRequest> changeUserStatusValidator,
 | |
|             IValidator<CreateUserRequest> registerUserValidator,
 | |
|             IValidator<UpdateUserRequest> updateUserValidator,
 | |
|             IThalosServiceClient thalosDALService)
 | |
|         {
 | |
|             _port = port ?? throw new ArgumentNullException(nameof(port));
 | |
|             _changeUserStatusValidator = changeUserStatusValidator ?? throw new ArgumentNullException(nameof(changeUserStatusValidator));
 | |
|             _registerUserValidator = registerUserValidator ?? throw new ArgumentNullException(nameof(registerUserValidator));
 | |
|             _updateUserValidator = updateUserValidator ?? throw new ArgumentNullException(nameof(updateUserValidator));
 | |
|             _thalosDALService = thalosDALService ?? throw new ArgumentNullException(nameof(thalosDALService));
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(GetUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _thalosDALService.GetUserByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(GetUserByEmailRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _thalosDALService.GetUserByEmailAsync(command.Email, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(ValidateUserExistenceRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _thalosDALService.ValidateUserExistence(command.Email, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(GetAllUsersRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var _result = await _thalosDALService.GetAllUsersAsync().ConfigureAwait(false);
 | |
|                 if (!_result.Any())
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
|                 _port.Success(_result.ToList());
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(ChangeUserStatusRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 if (!command.IsValid(_changeUserStatusValidator))
 | |
|                 {
 | |
|                     _port.ValidationErrors(command.Notifications);
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 var result = await _thalosDALService.ChangeUserStatusAsync(command.Id, command.Status, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(CreateUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 if (!command.IsValid(_registerUserValidator))
 | |
|                 {
 | |
|                     _port.ValidationErrors(command.Notifications);
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 var request = new UserRequest
 | |
|                 {
 | |
|                     Email = command.Email,
 | |
|                     Name = command.Name,
 | |
|                     MiddleName = command.MiddleName,
 | |
|                     LastName = command.LastName,
 | |
|                     RoleId = command.RoleId,
 | |
|                 };
 | |
| 
 | |
|                 var result = await _thalosDALService.CreateUserAsync(request, command.SendInvitation, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(UpdateUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 if (!command.IsValid(_updateUserValidator))
 | |
|                 {
 | |
|                     _port.ValidationErrors(command.Notifications);
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 var request = new UserAdapter
 | |
|                 {
 | |
|                     Id = command.Id,
 | |
|                     Email = command.Email,
 | |
|                     Name = command.Name,
 | |
|                     MiddleName = command.MiddleName,
 | |
|                     LastName = command.LastName,
 | |
|                     RoleId = command.RoleId,
 | |
|                 };
 | |
| 
 | |
|                 var result = await _thalosDALService.UpdateUserAsync(request, request.Id, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(LoginUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _thalosDALService.LoginUserAsync(command.Email, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(LogoutUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _thalosDALService.LogoutUserAsync(command.Email, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(GetTokenAdapterRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _thalosDALService.GetTokenAdapter(command.Email, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| } | 
