I'm trying to make a simple Blog-application, but I'm a bit stuck on the architecture of the application.
I'd like to create a details-page with the content of the blog-post and a form for adding comment to the post.
My code looks like the following:
ViewModel
public class PostDetailsViewModel
{
[ValidateNever]
public Post Post { get; set; }
[Required]
public string Comment { get; set; }
[Required]
public string Author { get; set; }
}
Service-layer
public class BlogService {
public void AddComment(int postId, PostDetailsViewModel model) {
using(var context = new ApplicationDbContext()) {
Post post = context.Posts.Include(x => x.Comments).Single(x => x.Id == postId);
Comment comment = new Comment();
comment.Content = model.Content;
comment.Author = model.Author;
comment.TimeStamp = DateTime.Now;
post.Comments.Add(comment);
context.SaveChanges();
}
}
}
Controller
class BlogController{
public IActionResult Details(int id)
{
PostDetailsViewModel model = service.GetPostDetails(id);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Details(int id, PostDetailsViewModel model)
{
if (ModelState.IsValid)
{
service.AddComment(id, model);
return RedirectToAction("Details", new { id = id });
}
return View(model);
}
}
I already did a lot of research on architectural best-practices, but I can't see the wood for the trees anymore...
Could someone provide me with some best-practices/improvements on my architecture?
Thank you in advance!