Exemplo n.º 1
0
        public ActionResult Index(int page = 1)
        {
            ForumCategoryViewModel view = new ForumCategoryViewModel
            {
                Categories = Db.ForumCategories.ToList().ToPagedList(page, PageSize),
                Posts      = Db.ForumPosts.ToList()
            };

            if (page != 1)
            {
                ViewBag.Count = PageSize - (PageSize * page - Db.ForumCategories.Count());
                ViewBag.Page  = page - 1;
            }

            return(View(view));
        }
        public async Task <IActionResult> Create(ForumCategoryViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var forumCategory = new ForumCategory
                {
                    Name = model.Name
                };

                this._context.ForumCategories.Add(forumCategory);
                await this._context.SaveChangesAsync();

                return(this.RedirectToAction("Index", "Fora"));
            }

            return(this.View(model));
        }
        public IActionResult ByName(string name, int id, string searchString, string currentFilter, int page = 1)
        {
            var normalisedName = this.categoriesService.GetNormalisedName(name);

            var forumCategory =
                this.forumCategoriesService.GetByCategoryNameAndForumId <ForumCategoryByNameViewModel>(normalisedName, id);

            if (forumCategory == null)
            {
                return(this.NotFound());
            }

            var viewModel = new ForumCategoryViewModel();

            viewModel.ForumCategory = forumCategory;

            viewModel.CurrentPage = page;

            if (searchString != null)
            {
                viewModel.CurrentPage = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            this.ViewData["CurrentFilter"] = searchString;

            viewModel.CategoryPosts = this.postsService
                                      .GetAllByCategoryNameAndForumId <PostInCategoryViewModel>(normalisedName, id, PostPerPage, (page - 1) * PostPerPage, searchString);

            var count = this.postsService.GetCountByCategoryNameAndForumId(normalisedName, id, searchString);

            viewModel.PagesCount = (int)Math.Ceiling((double)count / PostPerPage);
            if (viewModel.PagesCount == 0)
            {
                viewModel.PagesCount = 1;
            }

            // ReSharper disable once Mvc.ViewNotResolved
            // ForumCategoriesController responds to Views/Categories due ControllerName Change
            return(this.View(viewModel));
        }
        // GET: ForumCategories/Edit/5
        public async Task <IActionResult> Edit(Guid?id)
        {
            if (id == null)
            {
                return(this.NotFound());
            }

            var forumCategory = await this._context.ForumCategories.SingleOrDefaultAsync(m => m.Id == id);

            if (forumCategory == null)
            {
                return(this.NotFound());
            }

            var model = new ForumCategoryViewModel
            {
                Name = forumCategory.Name
            };

            return(this.View(model));
        }
        public async Task <IActionResult> Edit(Guid?id, ForumCategoryViewModel model)
        {
            if (id == null)
            {
                return(this.NotFound());
            }

            var forumCategory = await this._context.ForumCategories.SingleOrDefaultAsync(m => m.Id == id);

            if (forumCategory == null)
            {
                return(this.NotFound());
            }

            if (this.ModelState.IsValid)
            {
                forumCategory.Name = model.Name;
                await this._context.SaveChangesAsync();

                return(this.RedirectToAction("Index", "Fora"));
            }

            return(this.View(model));
        }