예제 #1
0
        public async System.Threading.Tasks.Task <System.Net.Http.HttpResponseMessage> PostAsync([FromUri] int id, [FromBody] BeerRatingViewModel rating)
        {
            //send request to punk api for data based off of the beer ID
            string path = "https://api.punkapi.com/v2/beers/" + id;
            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;
                    try
                    {
                        List <BeerRatingViewModel> punkBeers = new List <BeerRatingViewModel>();
                        //check and load any entrys in database.json
                        if (File.Exists(Models.Db_Json_Path.database_json_Path))
                        {
                            using (FileStream fs = File.OpenRead(Models.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 = File.OpenWrite(Models.Db_Json_Path.database_json_Path))
                        {
                            await System.Text.Json.JsonSerializer.SerializeAsync(fs, punkBeers);
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                        return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Could not locate a Beer with that Id"));
                    }
                }
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Could not locate a Beer with that Id"));
            }
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
예제 #2
0
        // 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));
        }