Esempio n. 1
0
        public IActionResult Login([FromQuery] string usu, [FromQuery] string clave)
        {
            bool         uowStatus    = false;
            Notification notification = new Notification();
            Employee     employee     = new Employee();

            try
            {
                Specification <Employee> specification = GetLogingSpecification(usu, clave);
                uowStatus = _unitOfWork.BeginTransaction();
                List <Employee> Employees = _securityRepository.GetList(specification);
                _unitOfWork.Commit(uowStatus);

                if (Employees.FirstOrDefault() == null)
                {
                    throw new ArgumentException("Employee is not logged in");
                }

                EmployeeDto EmployeesDto = _empleadoLoginAssembler.toDto(Employees.FirstOrDefault());
                var         token        = GenerateToken(EmployeesDto.Username);
                return(Ok(responseHandler.getOkCommandResponse("bearer " + token, Constants.HttpStatus.Success, EmployeesDto)));
            }
            catch (ArgumentException ex)
            {
                _unitOfWork.Rollback(uowStatus);
                Console.WriteLine(ex.StackTrace);
                return(Unauthorized());
            }
            catch (Exception ex)
            {
                _unitOfWork.Rollback(uowStatus);
                Console.WriteLine(ex.StackTrace);
                return(StatusCode(StatusCodes.Status500InternalServerError, responseHandler.getAppExceptionResponse()));
            }
        }
Esempio n. 2
0
        public IActionResult FindByUsername([FromQuery] string Username)
        {
            bool uowStatus = false;

            try
            {
                Employee     employee     = new Employee();
                Notification notification = employee.ValidateFindByUsername(Username);

                if (notification.HasErrors())
                {
                    throw new ArgumentException(notification.ErrorMessage());
                }

                Specification <Employee> specification = GetFindByUsername(Username);
                uowStatus = _unitOfWork.BeginTransaction();
                employee  = _employeeRepository.FindByAnySpecificField(specification);
                _unitOfWork.Commit(uowStatus);
                EmployeeDto employeeDto = _employeeAssembler.toDto(employee);
                return(StatusCode(StatusCodes.Status200OK, employeeDto));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(_responseHandler.getAppCustomErrorResponse(ex.Message)));
            }
            catch (Exception ex)
            {
                _unitOfWork.Rollback(uowStatus);
                Console.WriteLine(ex.StackTrace);
                return(StatusCode(StatusCodes.Status500InternalServerError, this._responseHandler.getAppExceptionResponse()));
            }
        }
Esempio n. 3
0
        public IActionResult Login(UserCredentialsDTO userCredentialsDTO)
        {
            if (userCredentialsDTO == null)
            {
                throw new ArgumentException("Credentials are missing");
            }

            bool uowStatus = false;

            try
            {
                Specification <Employee> specification = GetLogingSpecification(userCredentialsDTO.Username, userCredentialsDTO.Password);
                uowStatus = _unitOfWork.BeginTransaction();
                List <Employee> employees = _securityRepository.GetList(specification);
                _unitOfWork.Commit(uowStatus);

                if (employees.FirstOrDefault() == null)
                {
                    throw new ArgumentException("Employee is not logged in");
                }

                EmployeeDto employeeDto = _empleadoLoginAssembler.toDto(employees.FirstOrDefault());
                var         token       = Token.GenerateToken(employeeDto.Username);
                return(Ok(responseHandler.getOkCommandResponse("bearer " + token, Constants.HttpStatus.Success, employeeDto)));
            }
            catch (ArgumentException ex)
            {
                _unitOfWork.Rollback(uowStatus);
                Console.WriteLine(ex.StackTrace);
                return(Unauthorized());
            }
            catch (Exception ex)
            {
                _unitOfWork.Rollback(uowStatus);
                Console.WriteLine(ex.StackTrace);
                return(StatusCode(StatusCodes.Status500InternalServerError, responseHandler.getAppExceptionResponse()));
            }
        }