Exemple #1
0
        public IActionResult Get([FromQuery] PlantQueryParameters queryParams)
        {
            queryParams.Take      = (queryParams.Take < 1 || queryParams.Take > 200) ? 50 : queryParams.Take;
            queryParams.PageIndex = (queryParams.PageIndex < 0) ? 0 : queryParams.PageIndex;

            List <UserPlant> plantList = _plantData.Get(queryParams);

            if (plantList == null)
            {
                return(NotFound());
            }

            IEnumerable <UserPlantDisplayViewModel> models =
                plantList.Select(p => new UserPlantDisplayViewModel(p));

            PaginationModel nextPreviousQuary = new PaginationModel
            {
                Next     = GetNextPreviousURL(queryParams, 1),
                Previous = GetNextPreviousURL(queryParams, -1)
            };

            Response.Headers.Add(
                "x-Pagination", JsonConvert.SerializeObject(nextPreviousQuary)
                );

            return(Ok(models));
        }
Exemple #2
0
        public List <UserPlant> Get(PlantQueryParameters options)
        {
            IQueryable <UserPlant> query = _dbContext.UserPlant
                                           .Include(plant => plant.PlantType)
                                           .Where(x => x.IsDeleted == options.IsDeleted);

            if (!String.IsNullOrWhiteSpace(options.OwnerID))
            {
                query = query.Where(x => x.OwnerID == options.OwnerID);
            }

            if (!String.IsNullOrWhiteSpace(options.Term))
            {
                query = query.Where(plant =>
                                    plant.NickName.Contains(options.Term) ||
                                    plant.WherePurchased.Contains(options.Term) ||
                                    plant.PlantType.LatinName.Contains(options.Term) ||
                                    plant.PlantType.CommonName.Contains(options.Term)
                                    );
            }

            if (!string.IsNullOrWhiteSpace(options.OrderBy))
            {
                string[] sortArray = ValidateSortStrings(options.OrderBy);
                if (sortArray != null)
                {
                    foreach (string field in sortArray)
                    {
                        query = query.OrderBy(field);
                    }
                }
            }

            var list = query.Skip(options.PageIndex * options.Take).Take(options.Take).ToList();

            return(list);
        }