Ejemplo n.º 1
0
        // GET: AdminPanel
        public async Task <IActionResult> Index(
            int?contestId,
            int?nominationId,
            int page = 1,
            PostsSortState sortOrder = PostsSortState.RatingAsc)
        {
            int pageSize = 10;

            IQueryable <Post> posts = _context.Posts
                                      .Where(x => x.ContestId == contestId)
                                      .Include(x => x.Author)
                                      .Include(x => x.Likes)
                                      .Include(x => x.Nomination);

            //Фильтрация
            if (nominationId != null && nominationId != 0)
            {
                posts = posts.Where(x => x.NominationId == nominationId);
            }

            //Сортировка
            switch (sortOrder)
            {
            case PostsSortState.RatingAsc:
                posts = posts.OrderBy(x => x.Rating);
                break;

            case PostsSortState.RatingDesc:
                posts = posts.OrderByDescending(x => x.Rating);
                break;

            case PostsSortState.NameAsc:
                posts = posts.OrderBy(x => x.Title);
                break;

            case PostsSortState.NameDesc:
                posts = posts.OrderByDescending(x => x.Title);
                break;
            }

            //Пагинация
            var count = await posts.CountAsync();

            var items = await posts.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync();


            PostsIndexViewModel viewModel = new PostsIndexViewModel()
            {
                PageViewModel   = new PostsPageViewModel(count, page, pageSize),
                SortViewModel   = new PostsSortViewModel(sortOrder),
                FilterViewModel = new PostFilterViewModel(_context.Nominations.Where(x => x.ContestId == contestId).ToList(), nominationId),
                Posts           = items,
                HelpNamePost    = new Post()
            };

            ViewBag.ContestId   = contestId;
            ViewBag.ContestName = _context.Contests.FirstOrDefaultAsync(x => x.Id == contestId).Result.Name;

            return(View(viewModel));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Index(string id)
        {
            var postDb = await this.postsService.GetById(id);

            var post        = new PostViewModel(postDb, postDb.ApplicationUser, postDb.PostReplies);
            var postReplies = postDb.PostReplies.Select(reply =>
                                                        new PostReplyViewModel(reply, reply.ApplicationUser, reply.Post));
            var model = new PostsIndexViewModel {
                Post = post, PostReplies = postReplies,
            };

            return(View(model));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Index(int page = 0)
        {
            var posts = _db.Posts.Skip(page * 10).Take(10).ToList();

            Paginator paginator = new Paginator(posts.Count, page, 10);

            var paginatedPosts = posts.Skip((paginator.CurrentPage - 1) * paginator.PageLength).Take(paginator.PageLength).OrderBy(p => p.CreatedDate);

            PostsIndexViewModel model = new PostsIndexViewModel()
            {
                Posts     = paginatedPosts,
                Paginator = paginator
            };

            return(View(model));
        }
Ejemplo n.º 4
0
        public IActionResult Index(int?id)
        {
            if (id.HasValue)
            {
                return(RedirectToAction("Details", new { id = id.Value }));
            }

            var posts = _postsService.GetAllPosts();

            var model = new PostsIndexViewModel
            {
                Posts = posts
            };

            return(View(model));
        }
        // GET: /Posts/
        public async Task <ActionResult> Index(int categoryId = 1, bool?isVisible = null)
        {
            var query = db.Posts.Where(p => p.CategoryId == categoryId);

            if (isVisible.HasValue)
            {
                query = query.Where(p => p.IsVisible == isVisible.Value);
            }

            var viewModel = new PostsIndexViewModel
            {
                CategoryId     = categoryId,
                CategoriesList = new SelectList(db.Categories, "Id", "Name", selectedValue: 1),
                IsVisible      = isVisible,
                IsVisibleList  = new List <SelectListItem>
                {
                    new SelectListItem
                    {
                        Value = "",
                        Text  = "All Posts"
                    },
                    new SelectListItem
                    {
                        Value = "true",
                        Text  = "Visible Posts only"
                    },
                    new SelectListItem
                    {
                        Value = "false",
                        Text  = "Invisible Posts only"
                    }
                },
                Posts = await query
                        .Include(p => p.Category)
                        .ToListAsync()
            };

            return(View("IndexAjax", viewModel));
        }