public async Task UpdateShippingAddressAsync(string userId, MUser_UpdateShippingAddress shippingAddress) { using var log = BeginFunction(nameof(UserMicroService), nameof(UpdateShippingAddressAsync), userId, shippingAddress); try { //await Assert(SecurityPolicy.IsAuthorized, userId).ConfigureAwait(false); using var ctx = QuiltContextFactory.Create(); var dbUserProfile = ctx.GetUserProfile(userId); var dbUserAddress = dbUserProfile.UserAddresses.Where(r => r.AddressTypeCode == AddressTypeCodes.Shipping).SingleOrDefault(); if (shippingAddress != null) { if (dbUserAddress == null) { dbUserAddress = new UserAddress() { UserProfile = dbUserProfile, AddressTypeCodeNavigation = ctx.AddressType(AddressTypeCodes.Shipping) }; _ = ctx.UserAddresses.Add(dbUserAddress); } dbUserAddress.AddressLine1 = shippingAddress.AddressLine1; dbUserAddress.AddressLine2 = shippingAddress.AddressLine2; dbUserAddress.City = shippingAddress.City; dbUserAddress.StateCode = shippingAddress.StateCode; dbUserAddress.PostalCode = shippingAddress.PostalCode; dbUserAddress.CountryCode = shippingAddress.CountryCode; } else { if (dbUserAddress != null) { _ = ctx.UserAddresses.Remove(dbUserAddress); } } _ = await ctx.SaveChangesAsync().ConfigureAwait(false); } catch (Exception ex) { log.Exception(ex); throw; } }
public async Task <ActionResult> EditShippingAddress(ProfileEditShippingAddressModel model) { //if (!ControllerModelFactory.IsValidPostalCode(model.PostalCode)) //{ // ModelState.AddModelError("PostalCode", "Invalid postal code."); //} if (!ModelState.IsValid) { if (model.StateCodes == null) { model.StateCodes = GetStateCodes(string.IsNullOrEmpty(model.StateCode)); } return(View(model)); } try { var svcShippingAddressData = new MUser_UpdateShippingAddress() { AddressLine1 = Trim(model.AddressLine1), AddressLine2 = Trim(model.AddressLine2), City = Trim(model.City), StateCode = Trim(model.StateCode), PostalCode = ModelFactory.ParsePostalCode(model.PostalCode), CountryCode = Trim(model.CountryCode) ?? "US" }; await UserMicroService.UpdateShippingAddressAsync(GetUserId(), svcShippingAddressData); AddFeedbackMessage(Feedback.FeedbackMessageTypes.Informational, "Shipping address changed."); return(RedirectToAction("Index")); } catch (ServiceException ex) { AddModelErrors(ex); return(View()); } }