Example #1
0
        public async Task <ActionResult <dynamic> > UpdateUser([FromBody] User user)
        {
            List <string> validations_erro = new List <string>();
            bool          updadtePassword  = false;

            if (string.IsNullOrEmpty(user.Name))
            {
                validations_erro.Add("User name is required");
            }

            if (string.IsNullOrEmpty(user.CPF))
            {
                validations_erro.Add("User CPF is required");
            }

            if (user.Id <= 0)
            {
                validations_erro.Add("User id is required");
            }

            if (string.IsNullOrEmpty(user.Email))
            {
                validations_erro.Add("User e-mail is required");
            }

            if (user.Role <= 0)
            {
                validations_erro.Add("User role is required");
            }

            if (!string.IsNullOrEmpty(user.Password))
            {
                if (!(!string.IsNullOrEmpty(user.Login) && !string.IsNullOrEmpty(user.Password)))
                {
                    validations_erro.Add("User login and password is required");
                }
                else
                {
                    updadtePassword = true;
                    user.Password   = BCryptService.GenerateBCryptHash(user.Password, workfactor);
                }
            }

            try
            {
                var responseCPFValid = HttpService.Get(urlValidateCPF + "/" + user.CPF);
                if (responseCPFValid.httpStatusCode != 200 || responseCPFValid.message != "Autorizado")
                {
                    validations_erro.Add("CPF não é valido");
                }

                var older_user_cpf = userRepository.GetUser(user.CPF);
                if (older_user_cpf != null && older_user_cpf.Id != user.Id)
                {
                    validations_erro.Add("Already exists user with this CPF");
                }

                var older_user_login = userRepository.GetUserLogin(user.Login);
                if (older_user_login != null && older_user_cpf.Id != user.Id)
                {
                    validations_erro.Add("Already exists user with this Login");
                }
            }
            catch (Exception ex1)
            {
                return(CatchError(ex1, string.Format("Update user id({0}), validate CPF", user.Id)));
            }

            if (validations_erro.Count() > 0)
            {
                return(BadRequest(new { success = false, data = new { }, messages = validations_erro }));
            }

            try
            {
                user.Name  = user.Name.ToUpper();
                user.Login = user.Login.ToLower();
                user.Email = user.Email.ToLower();
                User data_user = userRepository.Save(user, updadtePassword);
                return(Ok(new { success = true, data = data_user, messages = "Item successfull updated" }));
            }
            catch (Exception ex2)
            {
                return(CatchError(ex2, string.Format("Update user id({0})", user.Id)));
            }
        }