public ActionResult Add([Bind]DeliveryAddressViewModel model) { if (model == null) { throw new ArgumentNullException("model"); } var result = new DeliveryAddressViewModel(); var validator = new DeliveryAddressViewModelValidator(); var validationResult = validator.Validate(model); if (validationResult.IsValid) { var request = model.ConvertToAddDeliveryAddressRequest(); request.IdentityToken = this.formsAuthentication.GetAuthenticationToken(); var response = this.addressService.AddOrUpdateDeliveryAddress(request); result = response.ConvertToDeliveryAddressViewModel(); } else { result.MessageType = MessageType.Error.ToString(); result.BrokenRules = new List<BusinessRule>(); foreach (var failure in validationResult.Errors) { result.BrokenRules.Add(new BusinessRule(failure.PropertyName, failure.ErrorMessage)); } } result = CopyDeliveryAddressViewModel(model, result); var jsonNetResult = new JsonNetResult { Formatting = (Formatting)Newtonsoft.Json.Formatting.Indented, Data = result }; return jsonNetResult; }
/// <summary> /// The copy delivery address view model. /// </summary> /// <param name="source"> /// The source. /// </param> /// <param name="destination"> /// The destination. /// </param> /// <returns> /// The TdService.UI.Web.ViewModels.Account.DeliveryAddressViewModel. /// </returns> public static DeliveryAddressViewModel CopyDeliveryAddressViewModel( DeliveryAddressViewModel source, DeliveryAddressViewModel destination) { if (destination.Id == 0) { destination.Id = source.Id; } if (string.IsNullOrWhiteSpace(destination.AddressName)) { destination.AddressName = source.AddressName; } if (string.IsNullOrWhiteSpace(destination.FirstName)) { destination.FirstName = source.FirstName; } if (string.IsNullOrWhiteSpace(destination.LastName)) { destination.LastName = source.LastName; } if (string.IsNullOrWhiteSpace(destination.AddressLine1)) { destination.AddressLine1 = source.AddressLine1; } if (string.IsNullOrWhiteSpace(destination.AddressLine2)) { destination.AddressLine2 = source.AddressLine2; } if (string.IsNullOrWhiteSpace(destination.City)) { destination.City = source.City; } destination.CountryCode = source.CountryCode; if (string.IsNullOrWhiteSpace(destination.State)) { destination.State = source.State; } if (string.IsNullOrWhiteSpace(destination.Region)) { destination.Region = source.Region; } if (string.IsNullOrWhiteSpace(destination.ZipCode)) { destination.ZipCode = source.ZipCode; } if (string.IsNullOrWhiteSpace(destination.Phone)) { destination.Phone = source.Phone; } return destination; }
public void ShouldBeAbleToUpdateDeliveryAddress() { // arrange var controller = new AddressController(this.formsAuthentication, this.addressService); var viewModel = new DeliveryAddressViewModel { Id = 1, AddressName = "Test 1", AddressLine1 = "street", AddressLine2 = "flat", City = "Minsk", CountryCode = "BY", Region = null, State = null, FirstName = "Vitali", LastName = "Hatalski", Phone = null, ZipCode = "1233", AddressLine3 = null }; // act var actual = controller.Update(viewModel) as JsonNetResult; // assert Assert.That(actual, Is.Not.Null); Debug.Assert(actual != null, "actual != null"); var model = actual.Data as DeliveryAddressViewModel; Debug.Assert(model != null, "model != null"); Assert.That(model.MessageType, Is.EqualTo("Success")); Assert.That(model.Id, Is.EqualTo(1)); Assert.That(model.AddressName, Is.EqualTo("Test 1")); Assert.That(model.AddressLine1, Is.EqualTo("street")); Assert.That(model.AddressLine2, Is.EqualTo("flat")); Assert.That(model.AddressLine3, Is.Null); Assert.That(model.City, Is.EqualTo("Minsk")); Assert.That(model.CountryCode, Is.EqualTo("BY")); Assert.That(model.Region, Is.Null); Assert.That(model.State, Is.Null); Assert.That(model.FirstName, Is.EqualTo("Vitali")); Assert.That(model.LastName, Is.EqualTo("Hatalski")); Assert.That(model.Phone, Is.Null); Assert.That(model.ZipCode, Is.EqualTo("1233")); }