示例#1
0
        // GET: Admin/Post/Details/5
        public async Task <IActionResult> Details(int id)
        {
            // using Specification
            var spec = new PostWithSpecification(id);
            var post = await _uniofWork.Post.GetByIdAsyncWithSpec(spec);

            if (post == null)
            {
                return(NotFound());
            }
            return(View(post));
        }
示例#2
0
        public async Task <IActionResult> Index(PostSpecParams postParams)
        {
            // using Specification
            var spec = new PostWithSpecification(postParams);

            var countSpecification = new PostWithFiltersForCountSpecification(postParams);
            var totalItems         = await _uniofWork.Post.CountAsync(countSpecification);

            var pagesLast = (int)Math.Ceiling((double)totalItems / (double)postParams.PageSize);
            var pages     = Enumerable.Range(1, pagesLast).ToList();

            // запрос - если номер страница больше максимального числа - срабатывает когда гуляли по пагинации и выбрали категорию
            if (postParams.PageIndex > pagesLast)
            {
                postParams.PageIndex = pagesLast;
                return(RedirectToAction("Index", new
                {
                    categoryId = postParams.CategoryId,
                    pageIndex = postParams.PageIndex,
                    pageSize = postParams.PageSize,
                    search = postParams.Search
                }));
            }

            var obj = await _uniofWork.Post.GetListAsyncWithSpec(spec);

            // если результат поиска - ноль записей
            if (obj != null)
            {
                foreach (var item in obj)
                {
                    item.ApplicationUser = _db.ApplicationUsers.Find(item.ApplicationUserId);
                }
                var data = obj;
                return(View(new Pagination <Post>(postParams.PageIndex, postParams.PageSize, totalItems, pagesLast, pages, data)));
            }
            return(View(new Pagination <Post>(postParams.PageIndex, postParams.PageSize, totalItems, pagesLast, pages, new List <Post>())));
        }