diff --git a/Apps/Website/Controllers/CommentsController.cs b/Apps/Website/Controllers/CommentsController.cs new file mode 100644 index 0000000..bf6076d --- /dev/null +++ b/Apps/Website/Controllers/CommentsController.cs @@ -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 }); + } + } +} \ No newline at end of file diff --git a/Apps/Website/Controllers/LinksController.cs b/Apps/Website/Controllers/LinksController.cs index 75ed760..ff29da8 100644 --- a/Apps/Website/Controllers/LinksController.cs +++ b/Apps/Website/Controllers/LinksController.cs @@ -1,28 +1,35 @@ using System; using Application; using Microsoft.AspNetCore.Mvc; +using Website.Models; namespace Website { 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() { // return Forbid(); - return View(_service.GetAllLinks()); + return View(_linkService.GetAllLinks()); } [HttpGet("{controller}/detail/{linkId:guid}")] 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 @@ -41,7 +48,7 @@ namespace Website return View(cmd); } - _service.PublishLink(cmd); + _linkService.PublishLink(cmd); Success("Your link was posted!"); diff --git a/Apps/Website/Models/ShowLinkViewModel.cs b/Apps/Website/Models/ShowLinkViewModel.cs new file mode 100644 index 0000000..5b0a210 --- /dev/null +++ b/Apps/Website/Models/ShowLinkViewModel.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Apps/Website/Startup.cs b/Apps/Website/Startup.cs index 0d01397..19476a6 100644 --- a/Apps/Website/Startup.cs +++ b/Apps/Website/Startup.cs @@ -20,17 +20,27 @@ namespace Website // This method gets called by the runtime. Use this method to add services to the container. 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(new Infrastructure.Repositories.Memory.LinkRepository( - new Domain.Link("http://default.website"), - new Domain.Link("http://another.website"), - new Domain.Link("http://a.final.website") + link1, + link2, + link3 + )); + services.AddSingleton(new Infrastructure.Repositories.Memory.CommentRepository( + link1.AddComment("my first comment"), + link3.AddComment("another comment") )); services.AddSingleton(serviceProvider => { - var memoryRepository = serviceProvider.GetRequiredService() as Infrastructure.Repositories.Memory.LinkRepository; - return new Infrastructure.Repositories.Memory.Data(memoryRepository); + var memoryLinkRepository = serviceProvider.GetRequiredService() as Infrastructure.Repositories.Memory.LinkRepository; + var memoryCommentRepository = serviceProvider.GetRequiredService() as Infrastructure.Repositories.Memory.CommentRepository; + return new Infrastructure.Repositories.Memory.Data(memoryLinkRepository, memoryCommentRepository); }); services.AddTransient(); + services.AddTransient(); services.AddControllersWithViews(); } diff --git a/Apps/Website/Views/Comments/Create.cshtml b/Apps/Website/Views/Comments/Create.cshtml new file mode 100644 index 0000000..470fed5 --- /dev/null +++ b/Apps/Website/Views/Comments/Create.cshtml @@ -0,0 +1,16 @@ +@model Application.PublishCommentCommand +@{ + ViewData["Title"] = "Add a new comment"; +} + +

Add a comment to "@ViewData["Url"]"

+ +
+ + + + + + + +
\ No newline at end of file diff --git a/Apps/Website/Views/Links/Show.cshtml b/Apps/Website/Views/Links/Show.cshtml index 5ed89f9..d1ee4a8 100644 --- a/Apps/Website/Views/Links/Show.cshtml +++ b/Apps/Website/Views/Links/Show.cshtml @@ -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"]; *@ } -

Viewing link @Model.Url

+

Viewing link @Model.Link.Url

-@* *@ \ No newline at end of file +@* *@ +Add comment + +@if(Model.Comments.Length == 0) +{ +

No comments yet.

+} +else +{ +
    + @foreach (var comment in Model.Comments) + { +
  • @comment.Content
  • + } +
+} \ No newline at end of file