コード例 #1
0
        public async Task <IActionResult> GetAll([FromQuery] RestaurantPaginationModel model)
        {
            GetAllRestaurantsResponseModel result = await _restaurantService.GetAllAsync(User.Identity.Name, model);

            return(Ok(result));
        }
コード例 #2
0
        public async Task <GetAllRestaurantsResponseModel> GetAllAsync(string userId, RestaurantPaginationModel filter)
        {
            List <RestaurantListItem>     list        = new List <RestaurantListItem>();
            List <RestaurantEntity>       restaurants = new List <RestaurantEntity>();
            IQueryable <RestaurantEntity> a           = null;

            if (filter.Cuisines != null && filter.Cuisines.Length > 0)
            {
                a = _context.Restaurants
                    .AsNoTracking()
                    .Include(x => x.Reviews)
                    .Include(x => x.Cuisines)
                    .Where(x => !x.IsBlock && x.Cuisines
                           .Any(x => filter.Cuisines.Contains(x.CuisineId)));
            }
            else
            {
                a = _context.Restaurants
                    .AsNoTracking()
                    .Include(x => x.Reviews)
                    .Include(x => x.Cuisines)
                    .Where(x => !x.IsBlock);
            }

            if (filter.DeliveryTypes != null && filter.DeliveryTypes.Length > 0)
            {
                a = a.Where(x => x.Deliveries
                            .Any(x => filter.DeliveryTypes.Contains(x.DeliveryId)));
            }

            a = a.Where(x => x.PriceFrom <= filter.PriceFrom && x.PriceTo >= filter.PriceTo);
            Expression <Func <RestaurantEntity, object> > orderExp = x => x.Id;
            int total = 0;

            if (filter.Sort == "near" && filter.Latitude > 0 && filter.Longitude > 0)
            {
                Point point = new Point(filter.Latitude, filter.Longitude)
                {
                    SRID = 4326
                };
                restaurants = await a.Where(c => c.Address.Location.Distance(point) < 100).ToListAsync();
            }
            else if (filter.Sort == "rating" || filter.Sort == "new")
            {
                orderExp = filter.Sort switch {
                    "new" => x => x.CreatedAt,
                    "popular" => x => x.Rating,
                    _ => x => x.Id
                };

                total = await a.CountAsync();

                restaurants = await a.OrderByDescending(orderExp).ToListAsync();
            }
            else
            {
                orderExp = filter.Sort switch {
                    "abc" => x => x.Name[0],
                    "price" => x => x.PriceFrom,
                    _ => x => x.Id
                };
                total = await a.CountAsync();

                restaurants = await a.OrderBy(orderExp).ToListAsync();
            }

            return(new GetAllRestaurantsResponseModel {
                Restaurants = await GetRestaurantsAsync(restaurants, userId),
                Total = total
            });
        }