// GET api/assessments
        public PagedModel <AssessmentModel> GetAll(int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }

            var pagedAssessments = manager.GetAll(page: page, pageSize: PAGE_SIZE);

            var helper = new UrlHelper(Request);

            var links = new List <LinkModel>();

            if (page > 1 && (page - 1) < pagedAssessments.TotalPages)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Assessments", new { page = page - 1 }), "prevPage"));
            }

            if (page < pagedAssessments.TotalPages)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Assessments", new { page = page + 1 }), "nextPage"));
            }

            return(new PagedModel <AssessmentModel>()
            {
                TotalRecords = pagedAssessments.TotalRecords,
                TotalPages = pagedAssessments.TotalPages,
                Links = links,
                PagedData = pagedAssessments.PagedData.Select(a => TheModelFactory.Create(a)).ToList()
            });
        }
Example #2
0
        public HttpResponseMessage Get(bool includeMeasures = true, int page = 0)
        {
            IQueryable <Food> query;

            if (includeMeasures)
            {
                query = TheRepository.GetAllFoodsWithMeasures()
                        .OrderBy(food => food.Description);
            }
            else
            {
                query = TheRepository.GetAllFoods()
                        .OrderBy(food => food.Description);
            }

            int totalCount = query.Count();
            int totalPages = (int)Math.Ceiling((double)totalCount / PAGE_SIZE);

            UrlHelper urlHelper = new UrlHelper(Request);

            List <LinkModel> links = new List <LinkModel>();

            if (page > 0)
            {
                links.Add(TheModelFactory.CreateLink(urlHelper.Link("Foods", new
                {
                    page = page - 1,
                }), "previousPage"));
            }

            if (page < totalPages - 1)
            {
                links.Add(TheModelFactory.CreateLink(urlHelper.Link("Foods", new
                {
                    page = page + 1,
                }), "nextPage"));
            }

            IEnumerable <FoodModel> results = query
                                              .Skip(PAGE_SIZE * page)
                                              .Take(PAGE_SIZE)
                                              .ToList()
                                              .Select(f => TheModelFactory.Create(f));

            return(Request.CreateResponse(HttpStatusCode.OK, new
            {
                TotalCount = totalCount,
                TotalPages = totalPages,
                Links = links,
                Results = results,
            }));
        }
        public object Get(bool includeMeasures = true, int page = 0)
        {
            IQueryable <Food> query;
            var repo = new CountingKsRepository(new CountingKsContext());

            if (includeMeasures)
            {
                query = TheRepository.GetAllFoodsWithMeasures();
            }
            else
            {
                query = TheRepository.GetAllFoods();
            }

            var baseQuery = query.OrderBy(f => f.Description);

            var totalCount = baseQuery.Count();
            var totalPages = Math.Ceiling((double)totalCount / PAGE_SIZE);

            var helper = new UrlHelper(Request);

            var links = new List <LinkModel>();

            if (page > 0)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Food", new { page = page - 1 }), "PreviousPage"));
            }

            if (page < totalPages - 1)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Food", new { page = page + 1 }), "NextPage"));
            }

            //var prevUrl = page > 0 ? helper.Link("Food", new { page = page - 1}) : "" ;
            //var nextUrl = page < totalPages - 1 ? helper.Link("Food", new { page = page + 1}) : "";

            var results = baseQuery.Skip(PAGE_SIZE * page)
                          .ToList()
                          .Select(f => TheModelFactory.Create(f));



            return(new
            {
                TotalCount = totalCount,
                TotalPages = totalPages,
                Links = links,
                Results = results
            });
        }
Example #4
0
        public object Get(bool includeTags = true, int page = 0)
        {
            IQueryable <Book> query;

            if (includeTags)
            {
                query = TheRepository.GetAllBooksWithTags();
            }
            else
            {
                query = TheRepository.GetAllBooks();
            }

            var baseQuery = query.OrderBy(f => f.Title);

            var totalCount = baseQuery.Count();
            var totalPages = Math.Ceiling((double)totalCount / PAGE_SIZE);

            var helper = new UrlHelper(Request);

            var links = new List <LinkModel>();

            if (page > 0)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Book", new { page = page - 1 }), "prevPage"));
            }

            if (page < totalPages - 1)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Book", new { page = page + 1 }), "nextPage"));
            }

            var results = baseQuery.Skip(PAGE_SIZE * page)
                          .Take(PAGE_SIZE)
                          .ToList()
                          .Select(f => (BookWithTagsModel)TheModelFactory.Create(f));

            return(new
            {
                TotalCount = totalCount,
                TotalPages = totalPages,
                Links = links,
                Results = results
            });
        }