private static bool ValidAddress(Address address) { return address != null && !string.IsNullOrEmpty(address.Line1) && !string.IsNullOrEmpty(address.Line2) && !string.IsNullOrEmpty(address.Town) && !string.IsNullOrEmpty(address.County) && !string.IsNullOrEmpty(address.Postcode); }
public ActionResult Index(Address address) { var geoLocation = _locationCalculator.CalculateLocation(address.Postcode); if (ValidAddress(address) && geoLocation != null) { address.Postcode = address.Postcode.Replace(" ", "").ToUpper(); address.Latitude = geoLocation.Latitude; address.Longitude = geoLocation.Longitude; address.UserId = User.UserId; _addressRepository.UpdateAddress(address); TempData["ViewModel"] = new AddressViewModel { Message = "Address updated", Success = true, Address = address}; } else { TempData["ViewModel"] = new AddressViewModel {Message = "Please enter valid address", Success = false, Address = address}; } return RedirectToAction("Index"); }
public void UpdateAddress(Address address) { using (var connection = new SqlConnection(_connectionString)) { const string sql = @" merge Address as target using (values (@UserId)) as source (UserId) on target.UserId = source.UserId when matched then update SET Line1 = @Line1, Line2 = @Line2, Town = @Town, County = @County, Postcode = @Postcode, Latitude = @Latitude, Longitude = @Longitude when not matched then insert (UserId, Line1, Line2, Town, County, Postcode, Latitude, Longitude) values (@UserId, @Line1, @Line2, @Town, @County, @Postcode, @Latitude, @Longitude);"; connection.Execute(sql, address); } }