395 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			395 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Core.Cerberos.Adapters;
 | |
| using Core.Cerberos.Application.UseCases.Users.Input;
 | |
| using Core.Cerberos.Application.UseCases.Users.Ports;
 | |
| using Core.Cerberos.External.Clients;
 | |
| using Core.Cerberos.External.Clients.Requests;
 | |
| using FluentValidation;
 | |
| using Lib.Architecture.BuildingBlocks;
 | |
| using Lib.Architecture.BuildingBlocks.Helpers;
 | |
| namespace Core.Cerberos.Application.UseCases.Users
 | |
| {
 | |
|     public class UserHandler :
 | |
|         IComponentHandler<ChangeUserStatusRequest>,
 | |
|         IComponentHandler<GetAllUsersRequest>,
 | |
|         IComponentHandler<UpdateUserRequest>,
 | |
|         IComponentHandler<GetUserRequest>,
 | |
|         IComponentHandler<GetUserByEmailRequest>,
 | |
|         IComponentHandler<CreateUserRequest>,
 | |
|         IComponentHandler<AddProjectToUserRequest>,
 | |
|         IComponentHandler<RemoveProjectFromUserRequest>,
 | |
|         IComponentHandler<AddCompanyToUserRequest>,
 | |
|         IComponentHandler<RemoveCompanyFromUserRequest>,
 | |
|         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 ICerberosServiceClient _cerberosDALService;
 | |
| 
 | |
|         public UserHandler(
 | |
|             IUserPort port,
 | |
|             IValidator<ChangeUserStatusRequest> changeUserStatusValidator,
 | |
|             IValidator<CreateUserRequest> registerUserValidator,
 | |
|             IValidator<UpdateUserRequest> updateUserValidator,
 | |
|             ICerberosServiceClient cerberosDALService)
 | |
|         {
 | |
|             _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));
 | |
|             _cerberosDALService = cerberosDALService ?? throw new ArgumentNullException(nameof(cerberosDALService));
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(GetUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _cerberosDALService.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 _cerberosDALService.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 _cerberosDALService.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 _cerberosDALService.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 _cerberosDALService.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,
 | |
|                     Companies = command.Companies,
 | |
|                     Projects = command.Projects,
 | |
|                 };
 | |
| 
 | |
|                 var result = await _cerberosDALService.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,
 | |
|                     Companies = command.Companies,
 | |
|                     Projects = command.Projects
 | |
|                 };
 | |
| 
 | |
|                 var result = await _cerberosDALService.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 _cerberosDALService.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 _cerberosDALService.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(AddCompanyToUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _cerberosDALService.AddCompanyToUserAsync(command.UserId, command.CompanyId, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(RemoveCompanyFromUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _cerberosDALService.RemoveCompanyToUserAsync(command.UserId, command.CompanyId, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(AddProjectToUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _cerberosDALService.AddProjectToUserAsync(command.UserId, command.ProjectId, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public async ValueTask ExecuteAsync(RemoveProjectFromUserRequest command, CancellationToken cancellationToken = default)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 ArgumentNullException.ThrowIfNull(command);
 | |
| 
 | |
|                 var result = await _cerberosDALService.RemoveProjectToUserAsync(command.UserId, command.ProjectId, 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 _cerberosDALService.GetTokenAdapter(command.Email, cancellationToken).ConfigureAwait(false);
 | |
| 
 | |
| 
 | |
|                 if (result == null)
 | |
|                 {
 | |
|                     _port.NoContentSuccess();
 | |
|                     return;
 | |
|                 }
 | |
| 
 | |
|                 _port.Success(result);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ApiResponseHelper.EvaluatePort(ex, _port);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| } | 
