@@ -12,15 +12,58 @@ namespace Blog.Controllers
1212 public class CommentsController : Controller
1313 {
1414 private readonly ICommentRepository commentRepository ;
15+ private readonly int ItemsPerPage = 50 ;
1516
1617 public CommentsController ( ICommentRepository commentRepository )
1718 {
1819 this . commentRepository = commentRepository ;
1920 }
2021
22+ public IActionResult Index ( string userName = "" , int page = 1 )
23+ {
24+ var comments = GetComments ( userName ) ;
25+ int totalPosts = comments . Count ( ) ;
26+
27+ if ( totalPosts > ItemsPerPage )
28+ comments = comments . Skip ( ( page - 1 ) * ItemsPerPage ) . Take ( ItemsPerPage ) ;
29+
30+ CommentsViewModel commentsViewModel = new CommentsViewModel ( )
31+ {
32+ Comments = comments ,
33+ PagingInformation = new PagingInformation ( )
34+ {
35+ CurrentPage = page ,
36+ ItemsPerPage = ItemsPerPage ,
37+ TotalItems = totalPosts
38+ }
39+ } ;
40+
41+ return View ( commentsViewModel ) ;
42+ }
43+
44+ private IEnumerable < Comment > GetComments ( string title = "" )
45+ {
46+ if ( ! string . IsNullOrEmpty ( title ) )
47+ return commentRepository . GetByUserName ( title ) ;
48+
49+ return commentRepository . Get ( ) ;
50+ }
51+
52+ public IActionResult Details ( int ? id )
53+ {
54+ if ( id == null )
55+ return NotFound ( ) ;
56+
57+ var comment = commentRepository . Get ( id . Value ) ;
58+ if ( comment == null )
59+ return NotFound ( ) ;
60+
61+ return View ( comment ) ;
62+ }
63+
2164 [ HttpPost ]
2265 [ ValidateAntiForgeryToken ]
23- public async Task < IActionResult > Create ( CommentViewModel commentViewModel )
66+ public async Task < IActionResult > Create ( CommentPostViewModel commentViewModel )
2467 {
2568 if ( commentViewModel == null )
2669 return BadRequest ( ) ;
0 commit comments