Skip to content

Commit 6f31970

Browse files
committed
feat: index and details views for the comments
1 parent 148a61b commit 6f31970

File tree

11 files changed

+192
-6
lines changed

11 files changed

+192
-6
lines changed

Blog/Blog/Controllers/CommentsController.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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();

Blog/Blog/Models/Repositories/CommentRepository.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Blog.Data;
2+
using Microsoft.EntityFrameworkCore;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -26,9 +27,14 @@ public IEnumerable<Comment> GetByPost(int id)
2627
return context.Comments.Where(c => c.PostId == id).OrderByDescending(x => x.Created);
2728
}
2829

30+
public IEnumerable<Comment> GetByUserName(string userName)
31+
{
32+
return context.Comments.Where(c => c.UserName.Contains(userName)).OrderByDescending(x => x.Created);
33+
}
34+
2935
public Comment Get(int id)
3036
{
31-
return context.Comments.FirstOrDefault(x => x.Id == id);
37+
return context.Comments.Include(c => c.Post).FirstOrDefault(x => x.Id == id);
3238
}
3339

3440
public void Create(Comment entity)

Blog/Blog/Models/Repositories/ICommentRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ namespace Blog.Models.Repositories
1010
public interface ICommentRepository : IRepository<Comment>
1111
{
1212
IEnumerable<Comment> GetByPost(int id);
13+
IEnumerable<Comment> GetByUserName(string userName);
1314
}
1415
}

Blog/Blog/Models/ViewModels/CommentViewModel.cs renamed to Blog/Blog/Models/ViewModels/CommentPostViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Blog.Models.ViewModels
88
{
99

10-
public class CommentViewModel
10+
public class CommentPostViewModel
1111
{
1212
public int PostId { get; set; }
1313
public Comment Comment { get; set; }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace Blog.Models.ViewModels
8+
{
9+
10+
public class CommentsViewModel
11+
{
12+
public IEnumerable<Comment> Comments { get; set; }
13+
public PagingInformation PagingInformation { get; set; }
14+
}
15+
}

Blog/Blog/Models/ViewModels/PostsViewModel.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ public class PostsViewModel
1111
public IEnumerable<Post> Posts { get; set; }
1212
public PagingInformation PagingInformation { get; set; }
1313
}
14-
1514
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
@model Blog.Models.Comment
2+
3+
@{
4+
ViewData["Title"] = "Comment Details";
5+
}
6+
7+
<h1 class="text-center mb-3">Comment Details</h1>
8+
9+
<div>
10+
<h4>Comment</h4>
11+
<hr />
12+
<dl class="row">
13+
<dt class="col-sm-2">
14+
@Html.DisplayNameFor(model => model.UserName)
15+
</dt>
16+
<dd class="col-sm-10">
17+
@Html.DisplayFor(model => model.UserName)
18+
</dd>
19+
<dt class="col-sm-2">
20+
@Html.DisplayNameFor(model => model.Message)
21+
</dt>
22+
<dd class="col-sm-10">
23+
@Html.DisplayFor(model => model.Message)
24+
</dd>
25+
26+
@if (Model.Post != null)
27+
{
28+
<dt class="col-sm-2">
29+
@Html.DisplayNameFor(model => model.Post.Title)
30+
</dt>
31+
<dd class="col-sm-10">
32+
@Html.DisplayFor(model => model.Post.Title)
33+
</dd>
34+
}
35+
36+
<dt class="col-sm-2">
37+
@Html.DisplayNameFor(model => model.Created)
38+
</dt>
39+
<dd class="col-sm-10">
40+
@Html.DisplayFor(model => model.Created)
41+
</dd>
42+
<dt class="col-sm-2">
43+
@Html.DisplayNameFor(model => model.Updated)
44+
</dt>
45+
<dd class="col-sm-10">
46+
@Html.DisplayFor(model => model.Updated)
47+
</dd>
48+
</dl>
49+
</div>
50+
<div>
51+
<a asp-action="Index" class="btn btn-primary"><i class="fas fa-arrow-left"></i> Back to List</a>
52+
<a asp-action="Delete" asp-route-id="@Model.Id" asp-route-returnUrl="/Comments/Index" class="btn btn-danger"
53+
onclick="return confirm('Are you sure to delete this?');">
54+
<i class="fas fa-edit"></i> Delete
55+
</a>
56+
</div>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
@model Blog.Models.ViewModels.CommentsViewModel
2+
3+
@{
4+
ViewData["Title"] = "Posts Table";
5+
}
6+
7+
<h1>Comments</h1>
8+
9+
<!-- SEARCH -->
10+
<section id="search" class="mt-3 mb-3">
11+
<div class="container px-4">
12+
<div class="row">
13+
<form class="form-inline w-100" asp-controller="Comments" asp-action="Index" method="get" role="form" enctype="multipart/form-data">
14+
<div class="col-md-10 p-0">
15+
<input type="text" class="form-control rounded-0 w-100" id="userName" name="userName" placeholder="Search by user name...">
16+
</div>
17+
<div class="col-md-2 p-0">
18+
<button type="submit" class="btn btn-dark w-100" value="Search"><i class="fas fa-search"></i></button>
19+
</div>
20+
</form>
21+
</div>
22+
</div>
23+
</section>
24+
25+
26+
<!-- COMMENT TABLE -->
27+
<section id="post-table">
28+
<table class="table table-striped">
29+
<thead class="thead-dark">
30+
<tr>
31+
<th>User Name</th>
32+
<th>Created</th>
33+
<th>Actions</th>
34+
</tr>
35+
</thead>
36+
<tbody>
37+
@if (Model.Comments.Any())
38+
{
39+
@foreach (var item in Model.Comments)
40+
{
41+
<tr>
42+
<td>
43+
@Html.DisplayFor(modelItem => item.UserName)
44+
</td>
45+
<td>
46+
@Html.DisplayFor(modelItem => item.Created)
47+
</td>
48+
<td>
49+
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-primary my-1"><i class="fas fa-eye"></i> Details</a>
50+
<a asp-action="Delete" asp-route-id="@item.Id" asp-route-returnUrl="@Context.Request.Path" class="btn btn-danger my-1"
51+
onclick="return confirm('Are you sure to delete this?');">
52+
<i class="fas fa-trash"></i> Delete
53+
</a>
54+
</td>
55+
</tr>
56+
}
57+
}
58+
</tbody>
59+
</table>
60+
61+
<!-- PAGINATION -->
62+
<div paging-information="@Model.PagingInformation" page-action="Index" class="mx-auto"></div>
63+
</section>

Blog/Blog/Views/Home/Details.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<h4>New Comment</h4>
4545
<hr />
4646
<div class="col-md-6 mb-4">
47-
@await Html.PartialAsync("_CommentCreateForm", new Blog.Models.ViewModels.CommentViewModel { PostId = Model.Post.Id, Comment = new Comment(), ReturnUrl = Context.Request.Path })
47+
@await Html.PartialAsync("_CommentCreateForm", new Blog.Models.ViewModels.CommentPostViewModel { PostId = Model.Post.Id, Comment = new Comment(), ReturnUrl = Context.Request.Path })
4848
</div>
4949

5050
<h4>Comments</h4>

Blog/Blog/Views/Shared/_CommentCreateForm.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@model Blog.Models.ViewModels.CommentViewModel
1+
@model Blog.Models.ViewModels.CommentPostViewModel
22

33
<div class="row">
44
<div class="col-md-12">

0 commit comments

Comments
 (0)