/// <summary> /// Updates the name, email, password and phone number of the customer with the specified id. /// </summary> /// <param name="request">The request containing the information that should be updated for the customer.</param> /// <param name="customerId">The id of the customer the changes should be applied to.</param> /// <returns></returns> public async Task <EditCustomerResponse> EditCustomerAsync(EditCustomerRequest request, string customerId) { var customer = await _unitOfWork.CustomerRepository.FindByIDAsync(customerId); await _unitOfWork.IdentityUserRepository.TransactionWrapper(async() => { customer.Name = request.Name; customer.PhoneNumber = request.PhoneNumber; if (request.ChangePassword == true) { var password = request.Password; var oldPassword = request.OldPassword; await _unitOfWork.IdentityUserRepository.ChangePasswordAsync(customer, password, oldPassword); } if (customer.Email != request.Email) { await _unitOfWork.IdentityUserRepository.ChangeEmailAsync(customer, request.Email); } _unitOfWork.CustomerRepository.Update(customer); await _unitOfWork.SaveChangesAsync(); }); var customerDto = _mapper.Map <CustomerDto>(customer); var response = new EditCustomerResponse { Customer = customerDto }; return(response); }
//Edit Customer public async Task <CustomerResponse> EditCustomer(EditCustomerRequest req) { string jsonRequest = CommonService.JsonSerializer <EditCustomerRequest>(req); string jsonResponse = await PostRequest(jsonRequest, "editcustomer", req.CustomerID); return(CommonService.JsonDeSerializer <CustomerResponse>(jsonResponse)); }
public async Task <IActionResult> EditCustomer(int?id) { if (id == null) { return(NotFound()); } var request = new EditCustomerRequest(id); var response = await _mediator.Send(request); return(View(response)); }
public void Email_WhenSet_ValidatesInput(string email, int numberOfErrors) { var request = new EditCustomerRequest { Email = email, Name = ValidName, PhoneNumber = ValidPhoneNumber, ChangePassword = false }; Assert.That(ValidateModelHelper.ValidateModel(request).Count, Is.EqualTo(numberOfErrors)); }
public void Name_WhenSetToWhiteSpaceOrNull_ValidationFails(string name, int numberOfErrors) { var request = new EditCustomerRequest { Email = ValidEmail, Name = name, PhoneNumber = ValidPhoneNumber, ChangePassword = false }; Assert.That(ValidateModelHelper.ValidateModel(request).Count, Is.EqualTo(numberOfErrors)); }
public IHttpActionResult Edit(EditCustomerRequest request, CancellationToken cancellationToken) { try { var command = mapper.Map <EditCustomerCommand>(request); var response = mediator.Send(command, cancellationToken); return(Ok(response)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public void Name_WhenSet_ValidatesLengthIsMin3Max255(int numberOfCharsInName, int numberOfErrors) { var name = GenerateString(numberOfCharsInName); var request = new EditCustomerRequest { Email = ValidEmail, Name = name.ToString(), ChangePassword = false, PhoneNumber = ValidPhoneNumber }; Assert.That(ValidateModelHelper.ValidateModel(request).Count, Is.EqualTo(numberOfErrors)); }
public void Password_WhenSet_ValidatesThatBothPasswordsIsPresentAndThatTheyAreEqual(string first, string second, int numberOfErrors) { var request = new EditCustomerRequest { Email = ValidEmail, Password = first, RepeatedPassword = second, Name = ValidName, PhoneNumber = ValidPhoneNumber, ChangePassword = false }; Assert.That(ValidateModelHelper.ValidateModel(request).Count, Is.EqualTo(numberOfErrors)); }
public async Task <IActionResult> Edit([FromHeader] string authorization, [FromBody] EditCustomerRequest request) { var customerId = User.Claims.FirstOrDefault(x => x.Type == Constants.UserIdClaim)?.Value; if (string.IsNullOrEmpty(customerId)) { throw new UserIdInvalidException( $"The supplied JSON Web Token does not contain a valid value in the '{ Constants.UserIdClaim }' claim."); } var response = await _customerService.EditCustomerAsync(request, customerId); return(Ok(response)); }
public async Task EditCustomerAsync_EditingCustomerSucceeds__ReturnsAEditCustomerResponse() { //Arrange var request = new EditCustomerRequest { Email = "*****@*****.**", Name = "Axel", Password = "******", RepeatedPassword = "******", PhoneNumber = "66666666", OldPassword = "******", ChangePassword = true }; var customer = new Customer { Id = "SomeId", Email = "*****@*****.**", PhoneNumber = "11111111", Name = "Hans" }; _unitOfWork.IdentityUserRepository.AddIdentityUserAsync(customer, request.OldPassword) .ReturnsForAnyArgs(IdentityResult.Success); _unitOfWork.IdentityUserRepository.SignInAsync(customer.Email, request.OldPassword) .ReturnsForAnyArgs(SignInResult.Success); var customerDto = new CustomerDto { Email = request.Email, Name = request.Name, PhoneNumber = request.PhoneNumber }; var editCustomerResponse = new EditCustomerResponse { Customer = customerDto }; _unitOfWork.CustomerRepository.FindByIDAsync(null).ReturnsForAnyArgs(customer); _mapper.Map <CustomerDto>(null).ReturnsForAnyArgs(customerDto); //Act var response = await _customerService.EditCustomerAsync(request, customer.Id); //Assert Assert.That(response.Customer, Is.EqualTo(editCustomerResponse.Customer)); }
public void Edit_CustomerIdEmpty_ThrowsUserIdInvalidException() { var request = new EditCustomerRequest(); _customerController.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(Constants.UserIdClaim, "") })) } }; Assert.ThrowsAsync <UserIdInvalidException>(async() => await _customerController.Edit(null, request)); }
public async Task Edit_Success_ReturnsOkResponse() { _customerController.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(Constants.UserIdClaim, "SomeCustomerId") })) } }; var request = new EditCustomerRequest(); var response = await _customerController.Edit(null, request) as ObjectResult; Assert.That(response.StatusCode, Is.EqualTo(StatusCodes.Status200OK)); }