예제 #1
0
        public async Task <PageResult <RestaurantDTO> > GetAll(RestaurantQuery query)
        {
            var baseQuery = await _efCoreRestaurantRepository.GetAll();

            var selected = baseQuery.Where(r => query.searchPhare == null || (r.Name.ToLower().Contains(query.searchPhare.ToLower()) || r.Description.ToLower().Contains(query.searchPhare.ToLower())));

            if (!string.IsNullOrEmpty(query.SortBy))
            {
                var columnsSelector = new Dictionary <string, Expression <Func <Restaurant, object> > >
                {
                    { nameof(Restaurant.Name), r => r.Name },
                    { nameof(Restaurant.Category), r => r.Category },
                    { nameof(Restaurant.Description), r => r.Description }
                };

                var selectedColumn = columnsSelector[query.SortBy];

                selected = query.SortDirection == SortDirection.ASC ?
                           selected.AsQueryable().OrderBy(selectedColumn) :
                           selected.AsQueryable().OrderByDescending(selectedColumn);
            }
            var restaurants = selected
                              .Skip(query.pageSize * (query.pageNumber - 1))
                              .Take(query.pageSize)
                              .ToList();

            var restaurantDtos = _mapper.Map <List <RestaurantDTO> >(restaurants);

            var totalItemCount = selected.Count();

            return(new PageResult <RestaurantDTO>(restaurantDtos, totalItemCount, query.pageSize, query.pageNumber));
        }
예제 #2
0
        public PagedResult <RestaurantDto> GetAll(RestaurantQuery query)
        {
            var baseQuery = _dbContext
                            .Restaurants
                            .Include(r => r.Address)
                            .Include(r => r.Dishes)
                            .Where(r => query.SearchPhrase == null ||
                                   (r.Name.ToLower().Contains(query.SearchPhrase.ToLower()) ||
                                    r.Description.ToLower().Contains(query.SearchPhrase.ToLower())));

            if (!string.IsNullOrWhiteSpace(query.SortBy))
            {
                var columnsSelector = new Dictionary <string, Expression <Func <Restaurant, object> > >
                {
                    { nameof(Restaurant.Name), r => r.Name },
                    { nameof(Restaurant.Description), r => r.Description },
                    { nameof(Restaurant.Category), r => r.Category }
                };

                baseQuery = query.SortDirection == SortDirection.ASC ?
                            baseQuery.OrderBy(columnsSelector[query.SortBy]) :
                            baseQuery.OrderByDescending(columnsSelector[query.SortBy]);
            }

            var totalItemsCount = baseQuery.Count();

            var restaurants = baseQuery
                              .Skip(query.PageSize * (query.PageNumber - 1))
                              .Take(query.PageSize)
                              .ToList();

            var restaurantsDtos = _mapper.Map <List <RestaurantDto> >(restaurants);

            return(new PagedResult <RestaurantDto>(restaurantsDtos, totalItemsCount, query.PageSize, query.PageNumber));
        }
        public async Task <IActionResult> GetAll()
        {
            await Db.Connection.OpenAsync();

            var query  = new RestaurantQuery(Db);
            var result = await query.GetAll();

            return(new OkObjectResult(result));
        }
        [Authorize(Policy = "AtLeast20")] //autoryzacja odpowiadająca wartości claim zadeklaroanego w startup
        public ActionResult <IEnumerable <RestaurantDto> > GetAll([FromQuery] RestaurantQuery query)
        {
            var restaurantDtos = _restaurantService.GetAll(query);

            //var restaurantDtos = restaurant.Select(r => new RestaurantDto()
            //{
            //   Name = r.Name,
            //    Category = r.Category,
            //    City = r.Address.City
            //});
            return(Ok(restaurantDtos));
        }
        public async Task <IActionResult> GetOne(int id)
        {
            await Db.Connection.OpenAsync();

            var query  = new RestaurantQuery(Db);
            var result = await query.GetOneRestaurant(id);

            if (result is null)
            {
                return(new NotFoundResult());
            }
            return(new OkObjectResult(result));
        }
        public async Task <IActionResult> PutOne(int id, [FromBody] Restaurant body)
        {
            await Db.Connection.OpenAsync();

            var query  = new RestaurantQuery(Db);
            var result = await query.GetOneRestaurant(id);

            if (result is null)
            {
                return(new NotFoundResult());
            }
            result.address    = body.address;
            result.name       = body.name;
            result.priceRange = body.priceRange;
            await result.UpdateOne();

            return(new OkObjectResult(result));
        }
예제 #7
0
        public async Task <RestaurantsResult> Get([FromQuery] RestaurantQuery query)
        {
            try
            {
                if (query.CityId < 1)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(new RestaurantsResult {
                        Status = "Error", Message = "CityId is unspecified"
                    });
                }

                int page = Math.Max(1, query.Page);

                var totalCount = await _store.GetRestaurantCount(query.CityId);

                var pageSize  = int.Parse(_config["RestaurantsCatalog:RestaurantsPageSize"]);
                var totalPage = (int)Math.Ceiling(totalCount / (double)pageSize);

                var restaurants = page <= totalPage ?
                                  await _store.GetRestaurants(query.CityId, pageSize, pageSize *(page - 1)) :
                                  null;

                return(new RestaurantsResult
                {
                    Status = "OK",
                    Page = page,
                    TotalPage = totalPage,
                    Restaurants = restaurants
                });
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(new RestaurantsResult {
                    Status = "Error", Message = ex.Message
                });
            }
        }
        public ActionResult <IEnumerable <RestaurantDto> > GetAll([FromQuery] RestaurantQuery query)
        {
            var restaurants = _service.GetAll(query);

            return(Ok(restaurants));
        }
예제 #9
0
        // [Authorize(Policy = "Atleast20")] just for testing
        //[Authorize(Policy = "CreatedAtleast2Restaurants")] works fine
        public async Task <ActionResult <RestaurantDTO> > GetAll([FromQuery] RestaurantQuery query)
        {
            var result = await restaurantService.GetAll(query);

            return(Ok(result));
        }
예제 #10
0
        public ActionResult <PagedResult <RestaurantDto> > GetAll([FromQuery] RestaurantQuery query)
        {
            var restaurantsDtos = _restaurantService.GetAll(query);

            return(Ok(restaurantsDtos));
        }
예제 #11
0
 public IEnumerable <Restaurant> GetRestaurants(RestaurantQuery query)
 {
     throw new NotImplementedException();
 }
예제 #12
0
 public IEnumerable <Restaurant> Get([FromQuery] RestaurantQuery query)
 {
     return(null);
 }