Пример #1
0
 public Review AddReview(Review review)
 {
     if (review == null)
         return new Review();
     var newReview = Reviews.Add(review);
     SaveChanges();
     return newReview;
 }
Пример #2
0
        public bool AddNewReview(int locationId, string placeId, string category, string review, string name, string locationName, string stars, string captchaResponse)
        {
            if (String.IsNullOrEmpty(captchaResponse))
                return false;

            if (!ValidateCaptcha(captchaResponse))
                return false;

            if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(stars))
                return false;

            name = name.ToUpper();
            var numStars = Convert.ToInt32(stars);

            if (String.IsNullOrEmpty(category) ||
                category.Equals("GENERAL", StringComparison.InvariantCultureIgnoreCase))
            {
                category = "GENERAL";
            }
            category = category.ToUpper();
            var cat = unitOfWork.CategoryRepository.Get(c => c.Name.Equals(category, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

            try
            {
                // No reviews exist
                // Create location and add review
                if (locationId == 0 && !String.IsNullOrEmpty(placeId))
                {
                    var newLoc = new Location { placeId = placeId, Name = locationName };
                    using (var scope = new TransactionScope())
                    {
                        newLoc = unitOfWork.LocationRepository.Insert(newLoc);
                        unitOfWork.Save();
                        var newReview = new Review
                        {
                            Location = newLoc,
                            ReviewText = review,
                            Category = cat,
                            Stars = numStars,
                            Author = name,
                            IsActive = true
                        };
                        AddReview(newReview);
                        scope.Complete();
                        return true;
                    }
                }
                // Reviews exist
                // Create review
                else
                {
                    var location = unitOfWork.LocationRepository.Get(l => l.LocationId == locationId).FirstOrDefault();
                    if (location == null || String.IsNullOrEmpty(review))
                        return false;

                    using (var scope = new TransactionScope())
                    {
                        if (cat == null)
                        {
                            cat = unitOfWork.CategoryRepository.Insert(new Category { Name = category });
                            unitOfWork.Save();
                        }

                        var newReview = new Review
                        {
                            Location = location,
                            ReviewText = review,
                            Category = cat,
                            Author = name,
                            Stars = numStars,
                            IsActive = true
                        };
                        AddReview(newReview);
                        scope.Complete();
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
Пример #3
0
 public void AddReview(Review review)
 {
     unitOfWork.ReviewsRepository.context.Entry<Location>(review.Location).State = EntityState.Unchanged;
     unitOfWork.ReviewsRepository.Insert(review);
     unitOfWork.Save();
 }
Пример #4
0
        public TestDatabaseContext()
        {
            var l1 = new Location
            {
                LocationId = 1,
                Address = "a street",
                City = "New York",
                State = "NY",
                Zip = "10000",
                Name = "l1",
                XCoordinate = 25.2,
                YCoordinate = 30.1,
                placeId = "a"
            };

            var l2 = new Location
            {
                LocationId = 2,
                Address = "b street",
                City = "New York",
                State = "NY",
                Zip = "10001",
                Name = "l2",
                XCoordinate = 22.2,
                YCoordinate = 32.1,
                placeId = "b"
            };

            locations = new List<Location>();
            locations.Add(l1);
            locations.Add(l2);

            var c1 = new Category
            {
                CategoryId = 1,
                Name = "General"
            };

            var c2 = new Category
            {
                CategoryId = 2,
                Name = "Food"
            };
            var c3 = new Category
            {
                CategoryId = 3,
                Name = "Parking"
            };

            categories = new List<Category>();
            categories.Add(c1);
            categories.Add(c2);
            categories.Add(c3);

            var r1 = new Review
            {
                ReviewId = 1,
                Author = "A",
                Category = c1,
                Location = l1,
                ReviewText = "Pretty good.",
                Stars = 8,
                IsActive = true
            };
            var r2 = new Review
            {
                ReviewId = 2,
                Author = "B",
                Category = c2,
                Location = l2,
                ReviewText = "Plenty of parking spaces",
                Stars = 10,
                IsActive = true
            };
            var r3 = new Review
            {
                ReviewId = 3,
                Author = "B",
                Category = c2,
                Location = l2,
                ReviewText = "Plenty of parking spaces",
                Stars = 10,
                IsActive = true
            };
            var r4 = new Review
            {
                ReviewId = 4,
                Author = "B",
                Category = c1,
                Location = l2,
                ReviewText = "Plenty of parking spaces",
                Stars = 10
            };

            reviews = new List<Review>();
            reviews.Add(r1);
            reviews.Add(r2);
            reviews.Add(r3);
            reviews.Add(r4);
        }