public virtual async Task <JsonResult> UpdateUserAddress(EditUserProfileAddressViewModel model) { if (!ModelState.IsValid) { return(JsonModelStateErrors()); } return(await JsonAsync(_addressService.UpdateUserAddressAsync(model))); }
/// <summary> /// Update user address /// </summary> /// <param name="model"></param> /// <returns></returns> public virtual async Task <ResultModel> UpdateUserAddressAsync(EditUserProfileAddressViewModel model) { var resultModel = new ResultModel(); var user = (await _userManager.GetCurrentUserAsync()).Result; if (user == null) { return(new NotAuthorizedResultModel().ToBase()); } var currentAddress = await _context.UserAddresses.FirstOrDefaultAsync(x => x.Id.Equals(model.Id)); if (currentAddress == null && model.Id != Guid.Empty) { if (!await _context.UserAddresses.AnyAsync(x => x.UserId == user.Id)) { var newAddress = _mapper.Map <AddNewAddressViewModel>(model); newAddress.IsDefault = true; var addReq = await AddAddressAsync(newAddress); return(addReq.ToBase()); } } if (currentAddress == null) { resultModel.Errors.Add(new ErrorModel(string.Empty, "Address not found")); return(resultModel); } if (model.IsDefault) { _context.UserAddresses .Where(x => x.UserId.Equals(currentAddress.UserId)) .ToList().ForEach(b => b.IsDefault = false); } currentAddress.CountryId = model.CountryId; currentAddress.StateOrProvinceId = model.CityId; currentAddress.AddressLine1 = model.AddressLine1; currentAddress.AddressLine2 = model.AddressLine2; currentAddress.ContactName = model.ContactName; currentAddress.Phone = model.Phone; currentAddress.ZipCode = model.ZipCode; currentAddress.IsDefault = model.IsDefault; currentAddress.Region = model.Region; _context.Update(currentAddress); var result = await _context.PushAsync(); return(result); }
public virtual async Task <IActionResult> EditUserProfileAddress(Guid?addressId) { if (!addressId.HasValue) { return(NotFound()); } var currentAddress = await _profileContext.UserAddresses .FirstOrDefaultAsync(x => x.Id.Equals(addressId.Value)); var cityBySelectedCountry = await _profileContext.StateOrProvinces .AsNoTracking() .Where(x => x.CountryId.Equals(currentAddress.CountryId)) .Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }).ToListAsync(); if (currentAddress == null) { return(NotFound()); } var model = new EditUserProfileAddressViewModel { Id = currentAddress.Id, CountrySelectListItems = await GetCountrySelectList(), AddressLine1 = currentAddress.AddressLine1, AddressLine2 = currentAddress.AddressLine2, Phone = currentAddress.Phone, ContactName = currentAddress.ContactName, ZipCode = currentAddress.ZipCode, CountryId = currentAddress.CountryId, CityId = currentAddress.StateOrProvinceId, SelectedStateOrProvinceSelectListItems = cityBySelectedCountry, IsDefault = currentAddress.IsDefault }; return(PartialView("Partial/_EditUserProfileAddress", model)); }