private async Task <AddressDataModel> GetAddressData(Address address) //gets address informations and request it's details from the google api { string request = "https://maps.googleapis.com/maps/api/geocode/json?"; //The base google api request uri request = request + "address=" + address.StreetNumber + "+" + address.Route.Replace(" ", "+") + "," + "+" + address.City.Replace(" ", "+") + "," + "+" + address.Country.Replace(" ", "+") + "," + "+" + address.PLZ + "," + "&key=AIzaSyB_0wHwmbg5aMjbUxMvT3SxjlE_aSnYlp4"; //api key string httpResponseMessage = await httpClient.GetStringAsync(request); //requesting AddressDataModel deserilaiyedResponse = JsonConvert.DeserializeObject <AddressDataModel>(httpResponseMessage); //Deserializing the request result from string to AddressDataModel return(deserilaiyedResponse); }
public async Task <bool> Add(Address address) //adding and a address to the database { try { AddressDataModel deserilaiyedResponse = await GetAddressData(address); //returns a data model of the json returned from the api if (deserilaiyedResponse.status == "OK") { address.Lat = deserilaiyedResponse.results[0].geometry.location.lat; //adding latitude value to the passed in model address.Lng = deserilaiyedResponse.results[0].geometry.location.lng; //adding longitude value to the passed in model } var entity = appDbContext.Addresses.Add(address); //Saving the complete model return(await(appDbContext.SaveChangesAsync()) > 0); //Committing the change to the database (retrun true if at least on line is changed) } catch (Exception e) { throw; } }
public async Task <bool> Edit(Address address) //editing a passed in address { try { AddressDataModel deserilaiyedResponse = await GetAddressData(address); if (deserilaiyedResponse.status == "OK") { address.Lat = deserilaiyedResponse.results[0].geometry.location.lat; //adding latitude value to the passed in model address.Lng = deserilaiyedResponse.results[0].geometry.location.lng; //adding longitude value to the passed in model } var entity = appDbContext.Addresses.Attach(address); //Updating the complete model entity.State = Microsoft.EntityFrameworkCore.EntityState.Modified; //Setting the state as modfiied so that changes can be commited return((await appDbContext.SaveChangesAsync()) > 0); //Committing the change to the database } catch (Exception e) { throw e; } }