public async Task <Response> Insert(ClientDTO client)
        {
            Response        response = new Response();
            ClientValidator validate = new ClientValidator();

            ValidationResult result = validate.Validate(client);

            Response password = PasswordValidator.CheckPassword(client.Password, client.BirthDate);

            //Verifica se a senha está dentro dos padrões, caso esteja, hasheia e ela
            if (password.HasErrors())
            {
                response.Errors.Add(password.Errors.ToString());
            }
            else
            {
                client.Password = HashUtils.HashPassword(client.Password);
            }

            if (!result.IsValid)
            {
                foreach (var failure in result.Errors)
                {
                    //Caso haja erros no validator, serão adicionados na response.Errors do client
                    response.Errors.Add("Property " + failure.PropertyName + " failed validation. Error was: " + "(" + failure.ErrorMessage + ")");
                }

                return(response);
            }

            if (response.HasErrors())
            {
                return(response);
            }
            else
            {
                try
                {
                    client.AccessLevel        = DataTransferObject.Enums.EAccessLevel.Client;
                    client.SystemEntranceDate = DateTime.Now;
                    client.IsActive           = true;

                    return(response = await _iClientRepository.Insert(client));
                }
                catch (Exception ex)
                {
                    _log.Error(ex + "\nStackTrace: " + ex.StackTrace);
                    response.Errors.Add("DataBase error, contact the system owner");
                    return(response);
                }
            }
        }
        public async Task <Response> Insert(EmployeeDTO employee)
        {
            Response          response = new Response();
            EmployeeValidator validate = new EmployeeValidator();
            ValidationResult  result   = validate.Validate(employee);
            Response          password = PasswordValidator.CheckPassword(employee.Password, employee.BirthDate);

            //Verifica se a senha está dentro dos padrões, caso esteja, hasheia e ela
            if (password.HasErrors())
            {
                response.Errors.Add(password.Errors.ToString());
            }
            else
            {
                employee.Password = HashUtils.HashPassword(employee.Password);
            }

            //result.MergeValidationErrors(response);


            if (!result.IsValid)
            {
                foreach (var failure in result.Errors)
                {
                    response.Errors.Add("Property " + failure.PropertyName + " failed validation. Error was: " + "(" + failure.ErrorMessage + ")");
                }

                return(response);
            }

            if (response.HasErrors())
            {
                return(response);
            }
            else
            {
                try
                {
                    employee.SystemEntranceDate = DateTime.Now;
                    employee.IsActive           = true;

                    return(response = await _iEmployeeRepository.Insert(employee));
                }
                catch (Exception ex)
                {
                    _log.Error(ex + "\nStackTrace: " + ex.StackTrace);
                    response.Errors.Add("DataBase error, contact the system owner");
                    return(response);
                }
            }
        }
        public async Task <Response> Update(ClientDTO client)
        {
            Response         response = new Response();
            ClientValidator  validate = new ClientValidator();
            ValidationResult result   = validate.Validate(client);
            Response         password = PasswordValidator.CheckPassword(client.Password, client.BirthDate);

            //Verifica se a senha está dentro dos padrões, caso esteja, hasheia e ela
            if (password.HasErrors())
            {
                response.Errors.Add(password.Errors.ToString());
            }
            else
            {
                client.Password = HashUtils.HashPassword(client.Password);
            }

            if (!result.IsValid)
            {
                foreach (var failure in result.Errors)
                {
                    response.Errors.Add("Property " + failure.PropertyName + " failed validation. Error was: " + "(" + failure.ErrorMessage + ")");
                }

                return(response);
            }


            if (response.HasErrors())
            {
                return(response);
            }
            else
            {
                try
                {
                    return(response = await _iClientRepository.Update(client));
                }
                catch (Exception ex)
                {
                    _log.Error(ex + "\nStackTrace: " + ex.StackTrace);
                    response.Errors.Add("DataBase error, contact the system owner");
                    return(response);
                }
            }
        }
示例#4
0
        public async Task <Response> Update(EmployeeDTO employee)
        {
            Response          response = new Response();
            EmployeeValidator validate = new EmployeeValidator();
            ValidationResult  result   = validate.Validate(employee);
            Response          password = PasswordValidator.CheckPassword(employee.Password, employee.BirthDate);

            //Verifica se a senha está dentro dos padrões, caso esteja, hasheia e ela
            if (password.HasErrors())
            {
                response.Errors.Add(password.Errors.ToString());
            }
            else
            {
                employee.Password = HashUtils.HashPassword(employee.Password);
            }

            //result.MergeValidationErrors(response);


            if (!result.IsValid)
            {
                foreach (var failure in result.Errors)
                {
                    response.Errors.Add("Property " + failure.PropertyName + " failed validation. Error was: " + "(" + failure.ErrorMessage + ")");
                }

                return(response);
            }

            if (response.HasErrors())
            {
                return(response);
            }
            else
            {
                return(await _iEmployeeRepository.Update(employee));
            }
        }