public async Task <FilteredModels <Article> > GetArticlesByPage(int page, int count = 20) { var filteredModels = new FilteredModels <Article>(); using (var db = new SqlConnection(_connectionString)) { int allAmount = (await db.QueryAsync <int>("EXEC GetArticlesCount")).Single(); filteredModels.MaxPage = (allAmount % count == 0) ? allAmount / count : allAmount / count + 1; if (filteredModels.MaxPage == 0) { throw new AdapterException(StatusCodes.Status404NotFound, "На данной странице нет статей"); } if (page > filteredModels.MaxPage) { page = filteredModels.MaxPage; } filteredModels.Items = (await db.QueryAsync <Article>($"EXEC GetArticlesByPage {page - 1}, {count}")).ToList(); } filteredModels.Page = page; return(filteredModels); }
private async Task <IActionResult> ShowArticlePage(int id, int page) { Article article; var comments = new FilteredModels <CommentViewModel> { Items = new List <CommentViewModel>() }; // Get article try { article = await _articleAdapter.GetArticleById(id); } catch (AdapterException ex) { return(View(new ArticleViewModel { Error = new ErrorViewModel { Text = ex.Message } })); } // Get comments try { comments = await _commentAdapter.GetCommentsByPage(id, page, 10); } catch (AdapterException ex) { // Comment list can't be empty if there are comments in article if (article.CommentsCount != 0 && comments.Items.Count == 0) { return(View(new ArticleViewModel { Error = new ErrorViewModel { Text = ex.Message } })); } } return(View(new ArticleViewModel { Article = article, Comments = comments })); }