public async System.Threading.Tasks.Task <IEnumerable <dynamic> > GetAsync(string id) { string path = "https://api.punkapi.com/v2/beers/?beer_name=" + id; var client = _clientFactory.CreateClient(); HttpResponseMessage response = await client.GetAsync(path); if (response.IsSuccessStatusCode) { Db_Json_Path db_Json_Path = new Db_Json_Path(_env); var Samochod = response.Content.ReadAsStringAsync(); List <PunkBeerViewModel> json = JsonConvert.DeserializeObject <List <PunkBeerViewModel> >(Samochod.Result); //load the data from database.json List <BeerRatingViewModel> punkBeers = new List <BeerRatingViewModel>(); if (_fileSystem.File.Exists(db_Json_Path.database_json_Path)) { using (FileStream fs = (FileStream)_fileSystem.File.OpenRead(db_Json_Path.database_json_Path)) { punkBeers = await System.Text.Json.JsonSerializer.DeserializeAsync <List <BeerRatingViewModel> >(fs); } } //combine the 2 collections collect beer type and list of comments left and return json response to client return(from c in json join p in punkBeers on c.id equals p.BeerId into ps select new { c.id, c.name, c.description, userRatings = ps.Select(ds => new { ds.Rating, ds.Username, ds.Comments }) }); } //call to service failed return no data return(null); }
// POST api/<controller> /// <summary> /// action is using filter to provide validation /// </summary> /// <param name="id">The beer Id in Punk to search for</param> /// <param name="rating">object representing user rating, email and comment about the beer</param> /// <returns>status message showing if the request was successful or failed</returns> //mvccore 2.2 [ApiController automatically performs the validation check that the action filter did in .Net webapi] //[Filters.VintriFilter] public async System.Threading.Tasks.Task <System.Net.Http.HttpResponseMessage> PostAsync([FromQuery] int id, [FromBody] BeerRatingViewModel rating) { string path = "https://api.punkapi.com/v2/beers/" + id; var client = _clientFactory.CreateClient(); HttpResponseMessage response = await client.GetAsync(path); // Beerd BeerRes = null; if (response.IsSuccessStatusCode) { //read the result and convert it to a .net object var jsonResult = response.Content.ReadAsStringAsync(); List <BeerRatingViewModel> json = JsonConvert.DeserializeObject <List <BeerRatingViewModel> >(jsonResult.Result); //make sure their is a result in our collection if (json.First() != null) { var val = json.First(); //pass values frombody into values collected from punk api val.BeerId = id; val.Comments = rating.Comments; val.Rating = rating.Rating; val.Username = rating.Username; Db_Json_Path db_Json_Path = new Db_Json_Path(_env); try { List <BeerRatingViewModel> punkBeers = new List <BeerRatingViewModel>(); //check and load any entrys in database.json if (_fileSystem.File.Exists(db_Json_Path.database_json_Path)) { using (FileStream fs = (FileStream)_fileSystem.File.OpenRead(db_Json_Path.database_json_Path)) { punkBeers = await System.Text.Json.JsonSerializer.DeserializeAsync <List <BeerRatingViewModel> >(fs); } } punkBeers.Add(val); //write out the new database.json file using (FileStream fs = (FileStream)_fileSystem.File.OpenWrite(db_Json_Path.database_json_Path)) { await System.Text.Json.JsonSerializer.SerializeAsync(fs, punkBeers); } } catch (Exception) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } } } else { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } return(new HttpResponseMessage(HttpStatusCode.OK)); }