Пример #1
0
        public void SendRatings(RatingInformationDTO Ratings)
        {
            client.Authenticator = new NtlmAuthenticator();
            client.AddDefaultHeader("Host", "localhost:12672");
            client.AddDefaultHeader("Cache-Control", "max-age=0");
            client.AddDefaultHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            client.AddDefaultHeader("Upgrade-Insecure-Requests", "1");
            client.AddDefaultHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36");
            client.AddDefaultHeader("Accept-Encoding", "gzip, deflate, sdch");
            client.AddDefaultHeader("Accept-Language", "en-GB,en;q=0.8,en-US;q=0.6");

            var request = new RestRequest("PostRatings", Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddBody(Ratings);

            var response = client.Execute<RatingInformationDTO>(request);
        }
        public IHttpActionResult PostRating(RatingInformationDTO RatingToAdd)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = RequestContext.Principal;

            //try
            //{

            foreach (RatingDTO rating in RatingToAdd.Ratings)
            {
                Models.Rating newRating = new Models.Rating();
                newRating.ProductID = RatingToAdd.ProductID;
                newRating.userID = user.Identity.Name;
                newRating.RatingCategory = rating.RatedArea;
                newRating.rating = rating.Score;

                if (db.Ratings.Any(x => x.ProductID == newRating.ProductID && x.userID == newRating.userID && x.RatingCategory == newRating.RatingCategory))
                {
                    db.Ratings.Single(x => x.ProductID == newRating.ProductID && x.userID == newRating.userID && x.RatingCategory == newRating.RatingCategory).rating = newRating.rating;
                }
                else
                {
                    db.Ratings.Add(newRating);
                }
                db.SaveChanges();

            }

            //db.Ratings.Add(rating);

            //try
            //{
            //    db.SaveChanges();
               // }
            //catch (DbUpdateException)
               // {
            //    if (RatingExists(rating.userID))
            //    {
            //        return Conflict();
            //    }
            //    else
            //    {
            //        throw;
            //    }
               // }

            return CreatedAtRoute("DefaultApi", new { id = RatingToAdd.ProductID  }, RatingToAdd);
        }
        public void PostRatings(RatingInformationDTO RatingToAdd)
        {
            var user = RequestContext.Principal;

            //try
            //{

            foreach (RatingDTO rating in RatingToAdd.Ratings)
            {
                Models.Rating newRating = new Models.Rating();
                newRating.ProductID = RatingToAdd.ProductID;
                newRating.userID = user.Identity.Name;
                newRating.RatingCategory = rating.RatedArea;
                newRating.rating = rating.Score;
                //db.RatingDetails.Add(newRating);
                db.SaveChanges();
            }

            // do stuff to store the rating here

            //catch (Exception ex)
            //{
            //    throw new Exception("Cannot add a rating", ex);
            //}
        }