Ejemplo n.º 1
0
        public void TestGetBeersByName_ShouldReturnMultipleBeers()
        {
            string testName = "sco";

            //Getting beers for test Name
            PunkApi api      = new PunkApi();
            var     apiBeers = api.GetBeersByName(testName);

            //Getting beers using Controller
            BeerController controller       = new BeerController();
            var            controllerResult = controller.Get(testName);

            ////Deserializing
            //var controllerBeers = JsonConvert.DeserializeObject<List<BeerDTO>>(controllerResult.ToString());

            //Assert.AreEqual(apiBeers.Count, controllerBeers.Count);
        }
Ejemplo n.º 2
0
        // POST api/beer?name=[beer_name]
        /// <summary>
        /// Gets a name in the parameters and response back a message
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public HttpResponseMessage Get(string name)
        {
            PunkApi                     punkObj = new PunkApi();
            HttpResponseMessage         message;
            Dictionary <string, string> messageDictionary = new Dictionary <string, string>();

            try
            {
                //Getting Beer results by name
                List <BeerInfo> result = punkObj.GetBeersByName(name);

                //Loading database json file
                List <BeerRating> currentRatings = GetCurrentRatings();

                //Doing Linq to combine results to ratings and reviews
                var combinedResult = from x in result
                                     select new BeerDTO
                {
                    Id          = x.Id,
                    Name        = x.Name,
                    Description = x.Description,
                    UserRatings = (from y in currentRatings
                                   where x.Id == y.Id
                                   select new BeerRatingResponse
                    {
                        Username = y.Username,
                        Rating = y.Rating,
                        Comments = y.Comments
                    }).ToArray()
                };

                message = Request.CreateResponse(HttpStatusCode.OK, combinedResult);
            }
            catch (Exception ex)
            {
                name = name == null ? "[null]" : name;
                messageDictionary.Add("error", "Could not process your request for name: " + name + " Error: " + ex.Message);
                message = Request.CreateResponse(HttpStatusCode.BadRequest, messageDictionary);
            }

            return(message);
        }