Esempio n. 1
0
 public UnitOfWork(CMMIContext context)
 {
     this.context = context;
     Restaurants  = new RestaurantRepository(context);
     Reviews      = new ReviewRepository(context);
     Users        = new UserRepository(context);
 }
Esempio n. 2
0
        //public async Task<IEnumerable<ReviewViewModel>> GetRestaurantReviews(long id, bool approvedOnly = true)
        //{
        //    using (var ctx = new CMMIContext())
        //    {
        //        var entity = await ctx.Restaurants.FindAsync(id);

        //        if (entity == null) throw new NotFoundException("Restaurant not found.");

        //        var query = ctx.Reviews.Where(x => x.RestaurantId == id);

        //        if (approvedOnly) query = query.Where(x => x.Approved);

        //        var results = await query.ToListAsync();

        //        return results.Select(x => new ReviewViewModel(x));
        //    }
        //}

        public async Task <IEnumerable <RestaurantCityViewModel> > GetRestaurantsGroupedByCity(string city, bool approvedOnly = true)
        {
            using (var ctx = new CMMIContext())
            {
                var query = ctx.Restaurants.AsQueryable();

                if (city != null)
                {
                    query = query.Where(x => x.City == city);
                }

                if (approvedOnly)
                {
                    query = query.Where(x => x.Approved);
                }

                var results = await query.OrderBy(x => x.City).ToListAsync();

                return(results
                       .GroupBy(x => x.City)
                       .Select(x => new RestaurantCityViewModel
                {
                    City = x.Key,
                    Restaurants = x.Select(y => new RestaurantViewModel(y))
                }));
            }
        }
 public Restaurant GetExistingRestaurant(Restaurant restaurant)
 {
     using (var context = new CMMIContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         return(unitOfWork.Restaurants.GetRestaurantByProperties(restaurant));
     }
 }
 public void AddRestaurant(Restaurant restaurant)
 {
     using (var context = new CMMIContext())
     {
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         unitOfWork.Restaurants.Add(restaurant);
         unitOfWork.Save();
     }
 }
Esempio n. 5
0
 public User GetUser(User user)
 {
     using (var context = new CMMIContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         return(unitOfWork.Users.GetUser(user));
     }
 }
 public Restaurant GetRestaurantById(long restaurantId)
 {
     using (var context = new CMMIContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         return(unitOfWork.Restaurants.GetRestaurantById(restaurantId));
     }
 }
 public IEnumerable <Review> GetByUser(UserRequest user)
 {
     using (var context = new CMMIContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         return(unitOfWork.Reviews.GetReviewsByUser(user));
     }
 }
Esempio n. 8
0
 public void AddUser(User user)
 {
     using (var context = new CMMIContext())
     {
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         unitOfWork.Users.Add(user);
         unitOfWork.Save();
     }
 }
 public void AddReviewForRestaurant(ReviewRequest review)
 {
     using (var context = new CMMIContext())
     {
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         unitOfWork.Reviews.Add(review);
         unitOfWork.Save();
     }
 }
Esempio n. 10
0
 public IEnumerable <Review> GetByRestaurantId(long restaurantId)
 {
     using (var context = new CMMIContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         return(unitOfWork.Reviews.GetReviewsByRestaurantId(restaurantId).ToList());
     }
 }
Esempio n. 11
0
 public void TestCleanup()
 {
     using (var context = new CMMIContext())
     {
         repository = new UserRepository(context);
         repository.Remove(user.UserId);
         repository.Save();
     }
 }
 public IEnumerable <Restaurant> GetAllRestaurantsByAddress(RestaurantRequest address)
 {
     using (var context = new CMMIContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         return(unitOfWork.Restaurants.GetRestaurantsByAddress(address));
     }
 }
Esempio n. 13
0
 public void RemoveRestaurantReview(long reviewId)
 {
     using (var context = new CMMIContext())
     {
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         unitOfWork.Reviews.Remove(reviewId);
         unitOfWork.Save();
     }
 }
 private void AddRestaurantRecordsForTest()
 {
     using (var context = new CMMIContext())
     {
         restaurantRepository = new RestaurantRepository(context);
         restaurantRepository.Add(restaurantToAdd);
         context.SaveChanges();
         currentRestaurant = restaurantRepository.GetRestaurantByProperties(restaurantToAdd);
     }
 }
 private void AddReviewRecordsForTest()
 {
     using (var context = new CMMIContext())
     {
         repository = new ReviewRepository(context);
         repository.Add(review);
         context.SaveChanges();
         currentReview = repository.GetReviewsByRestaurantId(currentRestaurant.RestaurantId).FirstOrDefault();
     }
 }
 public void TestCleanup()
 {
     using (var context = new CMMIContext())
     {
         repository = new RestaurantRepository(context);
         var restaurant = repository.GetRestaurantById(currentRowNumber);
         repository.Remove(restaurant.RestaurantId);
         context.SaveChanges();
     }
 }
 public void GetRestaurantByIdTest()
 {
     using (var context = new CMMIContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         repository = new RestaurantRepository(context);
         var result = repository.GetRestaurantById(currentRowNumber);
         Assert.IsNotNull(result);
     }
 }
 public void GetRestaurantsByAddressTest()
 {
     using (var context = new CMMIContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         repository = new RestaurantRepository(context);
         var result = repository.GetRestaurantsByAddress(restaurantRequest);
         Assert.IsNotNull(result);
     }
 }
Esempio n. 19
0
 private void AddUserRecordsForTest()
 {
     using (var context = new CMMIContext())
     {
         repository = new UserRepository(context);
         repository.Add(user);
         context.SaveChanges();
         currentUser = repository.GetUser(user);
     }
 }
Esempio n. 20
0
 public async Task <IEnumerable <string> > GetCityTypeAhead(string search)
 {
     using (var ctx = new CMMIContext())
     {
         return(await ctx.Restaurants
                .Where(x => x.Approved && x.City.StartsWith(search))
                .Select(x => x.City)
                .Distinct()
                .ToArrayAsync());
     }
 }
 public void GetRestaurantsByCityTest()
 {
     using (var context = new CMMIContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         repository = new RestaurantRepository(context);
         var result       = repository.GetRestaurantsByCity("city").ToList();
         var singleResult = result.FirstOrDefault(x => x.ContactInformation.Address.City == "city");
         Assert.IsNotNull(singleResult);
     }
 }
 private void AddTestRecordToDatabase()
 {
     using (var context = new CMMIContext())
     {
         repository = new RestaurantRepository(context);
         repository.Add(restaurantToAdd);
         context.SaveChanges();
         var restaurant = repository.GetRestaurantByProperties(restaurantToAdd);
         currentRowNumber = restaurant.RestaurantId;
     }
 }
Esempio n. 23
0
        public void GetUsersTest()
        {
            List <User> result;

            using (var context = new CMMIContext())
            {
                repository = new UserRepository(context);
                result     = repository.GetUsers().ToList();
            }
            Assert.IsNotNull(result);
        }
Esempio n. 24
0
        public async Task UpdateRestaurantReviewDetails(CMMIContext ctx, Restaurant restaurant)
        {
            var restaurantReviews = restaurant.Reviews.Where(x => x.Approved).ToList();

            if (restaurantReviews.Any())
            {
                restaurant.Rating      = restaurantReviews.Average(x => x.Rating);
                restaurant.ReviewCount = (short)restaurantReviews.Count();

                await ctx.SaveChangesAsync();
            }
        }
 public void GetReviewsByRestaurantTest()
 {
     using (var context = new CMMIContext())
     {
         repository    = new ReviewRepository(context);
         currentReview = repository.GetReviewsByRestaurantId(currentReview.RestaurantId).FirstOrDefault();
     }
     Assert.IsNotNull(currentReview);
     Assert.AreEqual(currentReview.Comment, review.Comment);
     Assert.AreEqual(currentReview.RestaurantId, review.RestaurantId);
     Assert.AreEqual(currentReview.Score, review.Score);
     Assert.AreEqual(currentReview.UserId, review.UserId);
 }
 public void AddTest()
 {
     using (var context = new CMMIContext())
     {
         repository = new ReviewRepository(context);
         repository.Add(review);
         repository.Save();
         var result = repository.GetReviewsByRestaurantId(currentReview.RestaurantId).ToList();
         Assert.AreEqual(result.Count, 2);
         repository.Remove(result.First().ReviewId);
         currentReview = result.Last();
     }
 }
 public void GetReviewTest()
 {
     using (var context = new CMMIContext())
     {
         repository    = new ReviewRepository(context);
         currentReview = repository.GetReview(currentReview.ReviewId);
     }
     Assert.IsNotNull(currentReview);
     Assert.AreEqual(currentReview.Comment, review.Comment);
     Assert.AreEqual(currentReview.RatingDateTime.Date, review.RatingDateTime.Date);
     Assert.AreEqual(currentReview.RestaurantId, review.RestaurantId);
     Assert.AreEqual(currentReview.Score, review.Score);
     Assert.AreEqual(currentReview.UserId, review.UserId);
 }
Esempio n. 28
0
        public async Task Reject(long id)
        {
            using (var ctx = new CMMIContext())
            {
                var entity = await ctx.Restaurants.FindAsync(id);

                if (entity == null)
                {
                    throw new NotFoundException("Restaurant not found.");
                }

                entity.Approved = false;
                await ctx.SaveChangesAsync();
            }
        }
 public void TestCleanup()
 {
     using (var context = new CMMIContext())
     {
         repository = new ReviewRepository(context);
         repository.Remove(currentReview.ReviewId);
         context.SaveChanges();
         userRepository = new UserRepository(context);
         userRepository.Remove(user.UserId);
         context.SaveChanges();
         restaurantRepository = new RestaurantRepository(context);
         restaurantRepository.Remove(currentRestaurant.RestaurantId);
         context.SaveChanges();
     }
 }
Esempio n. 30
0
        public async Task <IEnumerable <RestaurantViewModel> > GetRestaurantsByCity(string city, bool approvedOnly = true)
        {
            using (var ctx = new CMMIContext())
            {
                var query = ctx.Restaurants.Where(x => x.City == city);

                if (approvedOnly)
                {
                    query = query.Where(x => x.Approved);
                }

                var results = await query.ToListAsync();

                return(results.Select(x => new RestaurantViewModel(x)));
            }
        }