public async Task <IActionResult> Edit(string id, [Bind("Id,Name,Abv,GlasswareId,StyleId,IsOrganic,IsRetired,BreweryDBBreweryId")] BreweryDBBeer breweryDBBeer) { if (id != breweryDBBeer.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(breweryDBBeer); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BreweryDBBeerExists(breweryDBBeer.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["BreweryDBBreweryId"] = new SelectList(_context.BreweryDBBeer, "Id", "Id"); return(View(breweryDBBeer)); }
public async Task <IActionResult> GetBreweryBeers(string breweryDBBreweryId) { var breweryBeersUrl = (APIKeys.BreweryDBAPIURL + "brewery/" + breweryDBBreweryId + "/beers/?key=" + APIKeys.BreweryDBAPIKey); try { HttpResponseMessage response = await client.GetAsync(breweryBeersUrl); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <JObject>(responseBody); var beers = result["data"].ToList(); List <BreweryDBBeer> breweryDBBeers = new List <BreweryDBBeer>(); for (var i = 0; i < beers.Count; i++) { BreweryDBBeer breweryDBBeer = new BreweryDBBeer(); var id = beers[i]["id"]; breweryDBBeer.BreweryDBBeerId = id.ToObject <string>(); var name = beers[i]["name"]; breweryDBBeer.Name = name.ToObject <string>(); if (beers[i]["style"] != null) { var style = beers[i]["style"]["name"]; if (style != null) { breweryDBBeer.StyleName = style.ToObject <string>(); } } var abv = beers[i]["abv"]; if (abv != null) { breweryDBBeer.Abv = abv.ToObject <double>(); } string findBeerBreweryURL = (APIKeys.BreweryDBAPIURL + "beer/" + breweryDBBeer.BreweryDBBeerId + "/breweries/?key=" + APIKeys.BreweryDBAPIKey); HttpResponseMessage thisResponse = await client.GetAsync(findBeerBreweryURL); thisResponse.EnsureSuccessStatusCode(); string thisBreweryResponseBody = await thisResponse.Content.ReadAsStringAsync(); var thisBreweryResult = JsonConvert.DeserializeObject <JObject>(thisBreweryResponseBody); var brewereyName = thisBreweryResult["data"][0]["name"]; breweryDBBeer.BreweryName = brewereyName.ToObject <string>(); breweryDBBeers.Add(breweryDBBeer); } return(View(breweryDBBeers)); } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); } return(View()); }
public async Task <IActionResult> Create([Bind("Id,Name,Abv,GlasswareId,StyleId,IsOrganic,IsRetired,BreweryDBBreweryId")] BreweryDBBeer breweryDBBeer) { if (ModelState.IsValid) { _context.Add(breweryDBBeer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["BreweryDBBreweryId"] = new SelectList(_context.BreweryDBBeer, "Id", "Id"); return(View(breweryDBBeer)); }
public async Task <IActionResult> GetBeers() { //Establish all beer query URL string breweryDBUrl = (APIKeys.BreweryDBAPIURL + "beers/?key=" + APIKeys.BreweryDBAPIKey); try { HttpResponseMessage response = await client.GetAsync(breweryDBUrl); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var thisResult = JsonConvert.DeserializeObject <JObject>(responseBody); var beers = thisResult["data"].ToList(); List <BreweryDBBeer> breweryDBBeers = new List <BreweryDBBeer>(); //LATER either break out below into different function or create new view/action and only query for brewery if they click it for (var i = 0; i < beers.Count; i++) { BreweryDBBeer breweryDBBeer = new BreweryDBBeer(); var id = beers[i]["id"]; breweryDBBeer.BreweryDBBeerId = id.ToObject <string>(); var name = beers[i]["name"]; breweryDBBeer.Name = name.ToObject <string>(); if (beers[i]["style"] != null) { var styleId = beers[i]["style"]["id"]; if (styleId != null) { breweryDBBeer.StyleId = styleId.ToObject <int>(); } var style = beers[i]["style"]["name"]; if (style != null) { breweryDBBeer.StyleName = style.ToObject <string>(); } } var abv = beers[i]["abv"]; if (abv != null) { breweryDBBeer.Abv = abv.ToObject <double>(); } var ibu = beers[i]["ibu"]; if (ibu != null) { breweryDBBeer.Ibu = ibu.ToObject <double>(); } var description = beers[i]["description"]; if (description != null) { breweryDBBeer.Description = description.ToObject <string>(); } string findBeerBreweryURL = (APIKeys.BreweryDBAPIURL + "beer/" + breweryDBBeer.BreweryDBBeerId + "/breweries/?key=" + APIKeys.BreweryDBAPIKey); HttpResponseMessage thisResponse = await client.GetAsync(findBeerBreweryURL); thisResponse.EnsureSuccessStatusCode(); string thisBreweryResponseBody = await thisResponse.Content.ReadAsStringAsync(); var thisBreweryResult = JsonConvert.DeserializeObject <JObject>(thisBreweryResponseBody); var brewereyName = thisBreweryResult["data"][0]["name"]; breweryDBBeer.BreweryName = brewereyName.ToObject <string>(); breweryDBBeers.Add(breweryDBBeer); } return(View(breweryDBBeers)); } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); } return(View()); }