Ajout de la gestion des commentaires sur l'app MVC

This commit is contained in:
YuukanOO 2021-04-27 11:57:46 +02:00
parent 274e309a81
commit d573536ba4
6 changed files with 127 additions and 15 deletions

View File

@ -0,0 +1,46 @@
using System;
using Application;
using Microsoft.AspNetCore.Mvc;
namespace Website.Controllers
{
public class CommentsController : BaseController
{
private readonly CommentService _commentService;
private readonly LinkService _linkService;
public CommentsController(CommentService commentService, LinkService linkService)
{
_commentService = commentService;
_linkService = linkService;
}
[HttpGet("links/{linkId:guid}/comment")]
public IActionResult Create(Guid linkId)
{
ViewData["Url"] = _linkService.GetLinkById(linkId).Url;
return View();
}
[HttpPost("links/{linkId:guid}/comment")]
[ValidateAntiForgeryToken]
public IActionResult Create(PublishCommentCommand cmd)
{
if (!ModelState.IsValid)
{
ViewData["Url"] = _linkService.GetLinkById(cmd.LinkId).Url;
return View(cmd);
}
_commentService.PublishComment(cmd);
Success("Your comment was published!");
// return Redirect($"/links/detail/{cmd.LinkId}");
return RedirectToAction(nameof(LinksController.Show), "Links", new { linkId = cmd.LinkId });
}
}
}

View File

@ -1,28 +1,35 @@
using System; using System;
using Application; using Application;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Website.Models;
namespace Website namespace Website
{ {
public class LinksController : BaseController public class LinksController : BaseController
{ {
private readonly LinkService _service; private readonly LinkService _linkService;
private readonly CommentService _commentService;
public LinksController(LinkService service) public LinksController(LinkService linkService, CommentService commentService)
{ {
_service = service; _linkService = linkService;
_commentService = commentService;
} }
public IActionResult Index() public IActionResult Index()
{ {
// return Forbid(); // return Forbid();
return View(_service.GetAllLinks()); return View(_linkService.GetAllLinks());
} }
[HttpGet("{controller}/detail/{linkId:guid}")] [HttpGet("{controller}/detail/{linkId:guid}")]
public IActionResult Show(Guid linkId) public IActionResult Show(Guid linkId)
{ {
return View(_service.GetLinkById(linkId)); // ViewData["Comments"] = _commentService.GetAllLinkComments(linkId);
return View(new ShowLinkViewModel(
_linkService.GetLinkById(linkId),
_commentService.GetAllLinkComments(linkId)));
} }
// [HttpGet] // Implicite car HttpGet par défaut // [HttpGet] // Implicite car HttpGet par défaut
@ -41,7 +48,7 @@ namespace Website
return View(cmd); return View(cmd);
} }
_service.PublishLink(cmd); _linkService.PublishLink(cmd);
Success("Your link was posted!"); Success("Your link was posted!");

View File

@ -0,0 +1,16 @@
using Application;
namespace Website.Models
{
public class ShowLinkViewModel
{
public LinkDTO Link { get; }
public CommentDTO[] Comments { get; }
public ShowLinkViewModel(LinkDTO link, CommentDTO[] comments)
{
Link = link;
Comments = comments;
}
}
}

View File

@ -20,17 +20,27 @@ namespace Website
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
var link1 = new Domain.Link("http://default.website");
var link2 = new Domain.Link("http://another.website");
var link3 = new Domain.Link("http://a.final.website");
services.AddSingleton<ILinkRepository>(new Infrastructure.Repositories.Memory.LinkRepository( services.AddSingleton<ILinkRepository>(new Infrastructure.Repositories.Memory.LinkRepository(
new Domain.Link("http://default.website"), link1,
new Domain.Link("http://another.website"), link2,
new Domain.Link("http://a.final.website") link3
));
services.AddSingleton<ICommentRepository>(new Infrastructure.Repositories.Memory.CommentRepository(
link1.AddComment("my first comment"),
link3.AddComment("another comment")
)); ));
services.AddSingleton<IData>(serviceProvider => services.AddSingleton<IData>(serviceProvider =>
{ {
var memoryRepository = serviceProvider.GetRequiredService<ILinkRepository>() as Infrastructure.Repositories.Memory.LinkRepository; var memoryLinkRepository = serviceProvider.GetRequiredService<ILinkRepository>() as Infrastructure.Repositories.Memory.LinkRepository;
return new Infrastructure.Repositories.Memory.Data(memoryRepository); var memoryCommentRepository = serviceProvider.GetRequiredService<ICommentRepository>() as Infrastructure.Repositories.Memory.CommentRepository;
return new Infrastructure.Repositories.Memory.Data(memoryLinkRepository, memoryCommentRepository);
}); });
services.AddTransient<LinkService>(); services.AddTransient<LinkService>();
services.AddTransient<CommentService>();
services.AddControllersWithViews(); services.AddControllersWithViews();
} }

View File

@ -0,0 +1,16 @@
@model Application.PublishCommentCommand
@{
ViewData["Title"] = "Add a new comment";
}
<h1>Add a comment to "@ViewData["Url"]"</h1>
<form method="post">
<input type="hidden" asp-for="@Model.LinkId">
<label asp-for="@Model.Content">Your comment</label>
<textarea asp-for="@Model.Content"></textarea>
<span asp-validation-for="@Model.Content"></span>
<button type="submit">Post!</button>
</form>

View File

@ -1,8 +1,25 @@
@model Application.LinkDTO @model ShowLinkViewModel
@{ @{
ViewData["Title"] = $"Viewing link {Model.Url}"; ViewData["Title"] = $"Viewing link {Model.Link.Url}";
@* var comments = (Application.CommentDTO[]) ViewData["Comments"]; *@
} }
<h1>Viewing link @Model.Url</h1> <h1>Viewing link @Model.Link.Url</h1>
@* <partial name="_LinkItem" model="@Model" /> *@ @* <partial name="_LinkItem" model="@Model" /> *@
<a asp-controller="Comments" asp-action="Create" asp-route-linkId="@Model.Link.Id">Add comment</a>
@if(Model.Comments.Length == 0)
{
<p>No comments yet.</p>
}
else
{
<ul>
@foreach (var comment in Model.Comments)
{
<li>@comment.Content</li>
}
</ul>
}