Exemplo n.º 1
0
        public IHttpActionResult EditCustomer([FromBody] EditedCustomer editedCustomer)
        {
            var result = this.serviceHandler.CustomerService.UpdateCustomer(editedCustomer, this.repositoriesHandler, this.serviceHandler.PasswordService, this.resourcesProvider);

            if (result.Success)
            {
                return(Ok(result.SuccessMessage));
            }
            else
            {
                return(Content(HttpStatusCode.ExpectationFailed, result.Error));
            }
        }
Exemplo n.º 2
0
        public Result UpdateCustomer(EditedCustomer editedCustomer, IRepositoriesHandler repositoriesHandler, IPasswordService passwordService, IResourcesProvider resourcesProvider)
        {
            var result       = new Result();
            var isLoginExist = repositoriesHandler.СustomerRepository.GetCollection().Any(x => x.Login == editedCustomer.Login && x.CustomerID != editedCustomer.CustomerID);

            if (isLoginExist)
            {
                result.Success = false;
                result.Error   = resourcesProvider.GetGeneralResource("LogExist");
                return(result);
            }

            if (editedCustomer.CustomerID == 0)
            {
                result.Success = false;
                result.Error   = resourcesProvider.GetGeneralResource("CusIdNull");
                return(result);
            }

            if (!IsCutomerDataValid(editedCustomer.Login, editedCustomer.CurrentPassword))
            {
                result.Success = false;
                result.Error   = resourcesProvider.GetGeneralResource("NoLogPass");
                return(result);
            }

            var storedCustomer = repositoriesHandler.СustomerRepository.GetById(editedCustomer.CustomerID);

            if (passwordService.ComparePasswords(editedCustomer.CurrentPassword, storedCustomer.Password))
            {
                storedCustomer.CustomerID = editedCustomer.CustomerID;
                storedCustomer.FirstName  = editedCustomer.FirstName;
                storedCustomer.LastName   = editedCustomer.LastName;
                storedCustomer.Login      = editedCustomer.Login;
                storedCustomer.Password   = string.IsNullOrEmpty(editedCustomer.NewPassword) ? storedCustomer.Password : passwordService.HashPassword(editedCustomer.NewPassword);
                repositoriesHandler.СustomerRepository.Update(storedCustomer);
                result.Success        = true;
                result.SuccessMessage = resourcesProvider.GetGeneralResource("CusUpdt");
                return(result);
            }
            else
            {
                result.Success = false;
                result.Error   = resourcesProvider.GetGeneralResource("PassDontMatch");
                return(result);
            }
        }