Skip to main content

Punk API 18hour Experiment - Step #5


        // PUT api/Ratings/
        [HttpPost]
        public void Post([FromBody] RatingDTO dto)
        {
            ///
            /// Load the string to RatingDTO
            /// To Query the List using the id
            /// If Found update, if not Insert
            ///
            var row = data.Where(x => x.id == dto.beerid).FirstOrDefault();

            if(row==null)
            {
                BeerWithRatingsDTO temp = new BeerWithRatingsDTO();
                temp.comments.Add(new RatingShortDTO() { username = dto.username, userrating = dto.userrating, comment = dto.comment });

                data.Add(temp);
            }
            else ////row!=null
            {
                var comment = row.comments.Where(x => x.username == dto.username).FirstOrDefault();

                if (comment == null)
                    row.comments.Add(new RatingShortDTO() { username = dto.username, userrating = dto.userrating, comment = dto.comment });
                else
                {
                    comment.username = dto.username;
                    comment.userrating = dto.userrating;
                    comment.comment = dto.comment;
                }
            }

            //persist the data back into File
            string newString = JsonConvert.SerializeObject(data, Formatting.Indented);
            File.WriteAllText("data.json", newString);

        }

Comments