示例#1
0
        public HttpResponseMessage PicturesPaged([FromUri] FilterRequestView request)
        {
            if (request == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            var items = from i in db.Pictures
                        select new PictureView
            {
                Id      = i.Id,
                Guid    = i.Guid,
                Name    = i.Name,
                Created = i.Created,
            };

            if (request.SearchList != null)
            {
                foreach (FilterSearchItemView s in request.SearchList)
                {
                    switch (s.Name)
                    {
                    case "name":
                        items = items.Where(r => r.Name != null && r.Name.Contains(s.Value));
                        break;
                    }
                }
            }
            items = items.OrderByDescending(r => r.Created);
            var pagedItems = getPage("PicturesPaged", items, request);

            return(Request.CreateResponse(HttpStatusCode.OK, pagedItems));
        }
示例#2
0
 public FilterRequestView(FilterRequestView source)
 {
     this.Page     = source.Page > 0 ? source.Page : 1;
     this.Size     = source.Size > 0 ? source.Size : 10;
     this.Status   = source.Status;
     this.DateFrom = source.DateFrom;
     this.DateTo   = source.DateTo;
     this.Search   = source.Search;
 }
示例#3
0
 public FilterResponseView(FilterRequestView request)
 {
     this.Page     = request.Page;
     this.Size     = request.Size;
     this.Status   = request.Status;
     this.Search   = request.Search;
     this.DateFrom = request.DateFrom;
     this.DateTo   = request.DateTo;
 }
示例#4
0
 public FilterRequestView nextRequest(int pages)
 {
     if (this.Page < pages)
     {
         var next = new FilterRequestView(this);
         next.Page += 1;
         return(next);
     }
     else
     {
         return(null);
     }
 }
示例#5
0
 public FilterRequestView previousRequest(int pages)
 {
     if (this.Page > 1)
     {
         var previous = new FilterRequestView(this);
         previous.Page -= 1;
         return(previous);
     }
     else
     {
         return(null);
     }
 }
示例#6
0
        public IEnumerable <dynamic> getPage(string route, IEnumerable <dynamic> items, FilterRequestView requestView)
        {
            int Count = items.Count();
            int Pages = Count > 0 ? (int)Math.Ceiling(Count / (double)requestView.Size) : 0;

            var pagination = new FilterResponseView(requestView);

            pagination.Pages = Pages;
            pagination.Count = Count;

            var previous = requestView.previousRequest(Pages);
            var next     = requestView.nextRequest(Pages);

            var urlHelper = new UrlHelper(Request);

            pagination.PrevPageLink = previous != null?urlHelper.Link(route, new HttpRouteValueDictionary(previous)) : "";

            pagination.NextPageLink = next != null?urlHelper.Link(route, new HttpRouteValueDictionary(next)) : "";

            System.Web.HttpContext.Current.Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(pagination, new BrowserJsonFormatter().SerializerSettings));

            return(items.Skip(pagination.Size * (pagination.Page - 1)).Take(pagination.Size).ToList());
        }
示例#7
0
        public async Task <HttpResponseMessage> GetDishesPaged([FromUri] FilterRequestView request)
        {
            if (request == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            ApplicationUser user = await users.FindByNameAsync(User.Identity.Name);

            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.MethodNotAllowed);
            }
            IQueryable <DishView> items = db.Dishes.Include(x => x.User).Include(x => x.Categories).Include(x => x.Pictures).Select(x => new DishView
            {
                Id              = x.Id,
                Name            = x.Name,
                Price           = x.Price,
                isVeganFriendly = x.isVeganFriendly,
                Yes             = x.Yes,
                No              = x.No,
                Created         = x.Created,
                User            = new User
                {
                    UserName   = x.User.UserName,
                    FacebookId = x.User.FacebookId,
                    Route      = x.User.Route,
                },
                Categories = x.Categories.Select(c => new CategoryView
                {
                    Id   = c.Id,
                    Name = c.Name,
                }).ToList(),
                Pictures = x.Pictures.Select(c => new PictureView
                {
                    Id      = c.Id,
                    Guid    = c.Guid,
                    Name    = c.Name,
                    Route   = c.Route,
                    Created = c.Created,
                }).ToList(),
                Vote = db.Votes.Where(v => v.DishId == x.Id && v.UserId == user.Id).Select(v => new VoteView
                {
                    DishId  = v.DishId,
                    Like    = v.Like,
                    Created = v.Created
                }).FirstOrDefault()
            });

            if (request.SearchList != null)
            {
                foreach (FilterSearchItemView s in request.SearchList)
                {
                    switch (s.Name)
                    {
                    case "UserName":
                        items = items.Where(r => r.User.UserName != null && r.User.UserName == s.Value);
                        break;

                    case "Category":
                        items = items.Where(r => r.Categories.FirstOrDefault().Name == s.Value);
                        break;
                    }
                }
            }
            items = items.OrderByDescending(r => r.Id);
            var pagedItems = getPage("GetDishesPaged", items, request);

            return(Request.CreateResponse(HttpStatusCode.OK, pagedItems));
        }