Exemplo n.º 1
0
        public async Task <HttpResponseMessage> GetRatings(string query)
        {
            if (string.IsNullOrEmpty(query))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Search query is missing"));
            }

            try
            {
                (HttpStatusCode Status, string Data)result = await WebRequestService.GetRequest($"{api}/beers?beer_name={query}");

                if (result.Status != HttpStatusCode.OK || string.IsNullOrEmpty(result.Data))
                {
                    return(Request.CreateResponse(HttpStatusCode.BadGateway, "Proxy server did not return a valid response"));
                }

                ICollection <BeerItem> beerList = PunkService.GetRating(result.Data);
                if (beerList == null || beerList.Count < 1)
                {
                    return(Request.CreateResponse(HttpStatusCode.NoContent, "Search query did not match any beers"));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, beerList));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Exemplo n.º 2
0
        public async Task <HttpResponseMessage> RateBeer(int id, [FromBody] UserRating rating)
        {
            try
            {
                if (rating == null || rating.Rating < 0 || rating.Rating > 5)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Rating is Invalid"));
                }

                //validate Id
                var result = await WebRequestService.GetRequest($"{api}/beers/{id}");

                if (result.Item1 == HttpStatusCode.OK && !string.IsNullOrEmpty(result.Item2))
                {
                    JArray list = JArray.Parse(result.Item2);
                    if (list.Count < 1)
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadGateway, "Beer doesn't exists"));
                    }

                    PunkService.SaveRating(id, rating);
                    return(Request.CreateResponse(HttpStatusCode.OK, "Ratings saved"));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Beer Id"));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }