Пример #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);
        }
Пример #2
0
        public HttpResponseMessage Post(int id, [FromBody] BeerRating rating)
        {
            HttpResponseMessage message = null;
            List <string>       errors  = new List <string>();

            PunkApi punkObj = new PunkApi();

            if (!punkObj.CheckIdExists(id))
            {
                errors.Add("ID does not exist");
            }

            if (rating.Rating < 1 || rating.Rating > 5)
            {
                errors.Add("Not a valid rating. Should be between 1 and 5");
            }

            if (errors.Count > 0)
            {
                Dictionary <string, string[]> messageDictionary = new Dictionary <string, string[]>();
                messageDictionary.Add("errors", errors.ToArray());
                message = Request.CreateResponse(HttpStatusCode.BadRequest, messageDictionary);
            }
            else
            {
                Dictionary <string, string> messageDictionary = new Dictionary <string, string>();

                //Appending JSON to file (database.json)
                try
                {
                    rating.Id = id; //Attaching id to rating object
                    WriteToJsonFile(rating);

                    messageDictionary.Add("message", "Rating Successfully Added");
                    message = Request.CreateResponse(HttpStatusCode.OK, messageDictionary);
                }
                catch (IOException ex)
                {
                    messageDictionary.Add("error", "Could not write to JSON file. \n Error: " + ex.Message);
                    message = Request.CreateResponse(HttpStatusCode.InternalServerError, messageDictionary);
                }
            }

            return(message);
        }
Пример #3
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);
        }
Пример #4
0
 public BeerController()
 {
     PunkApi = new PunkApi();
 }