Skip to content

Commit 866378d

Browse files
committed
feat: added comments section for the posts
1 parent 5e3dbf0 commit 866378d

21 files changed

+874
-29
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Blog.Models;
6+
using Blog.Models.Repositories;
7+
using Blog.Models.ViewModels;
8+
using Microsoft.AspNetCore.Mvc;
9+
10+
namespace Blog.Controllers
11+
{
12+
public class CommentsController : Controller
13+
{
14+
private readonly ICommentRepository commentRepository;
15+
16+
public CommentsController(ICommentRepository commentRepository)
17+
{
18+
this.commentRepository = commentRepository;
19+
}
20+
21+
[HttpPost]
22+
[ValidateAntiForgeryToken]
23+
public async Task<IActionResult> Create(CommentViewModel commentViewModel)
24+
{
25+
if (commentViewModel == null)
26+
return BadRequest();
27+
28+
commentViewModel.Comment.PostId = commentViewModel.PostId;
29+
commentRepository.Create(commentViewModel.Comment);
30+
await commentRepository.SaveAsync();
31+
return Redirect(commentViewModel.ReturnUrl);
32+
}
33+
34+
35+
}
36+
}

Blog/Blog/Controllers/HomeController.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ public class HomeController : Controller
1717

1818
private readonly ITagRepository tagsRepository;
1919
private readonly IPostRepository postsRepository;
20-
20+
private readonly ICommentRepository commentRepository;
2121
private readonly int ItemsPerPage = 4;
2222

23-
public HomeController(ILogger<HomeController> logger, ITagRepository tagsRepository, IPostRepository postsRepository)
23+
public HomeController(ILogger<HomeController> logger, ITagRepository tagsRepository, IPostRepository postsRepository, ICommentRepository commentRepository)
2424
{
2525
_logger = logger;
2626
this.tagsRepository = tagsRepository;
2727
this.postsRepository = postsRepository;
28+
this.commentRepository = commentRepository;
2829
}
2930

3031
public IActionResult Index(string tag = "", string title = "", int page = 1)
@@ -70,9 +71,15 @@ public IActionResult Details(int? id)
7071
if (post == null)
7172
return NotFound();
7273

74+
PostDetailsViewModel view = new PostDetailsViewModel()
75+
{
76+
Post = post,
77+
Comments = commentRepository.GetByPost(post.Id)
78+
};
79+
7380
++post.Views;
7481
postsRepository.SaveAsync();
75-
return View(post);
82+
return View(view);
7683
}
7784

7885
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

Blog/Blog/Data/ApplicationDbContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
1919
public DbSet<AboutMe> AboutMe { get; set; }
2020
public DbSet<Contacts> Contacts { get; set; }
2121
public DbSet<Websites> Websites { get; set; }
22+
public DbSet<Comment> Comments { get; set; }
2223
}
2324
}

0 commit comments

Comments
 (0)