diff --git a/Application/CommentLink/CommentLinkCommand.cs b/Application/CommentLink/CommentLinkCommand.cs index 1edf25e..233be2a 100644 --- a/Application/CommentLink/CommentLinkCommand.cs +++ b/Application/CommentLink/CommentLinkCommand.cs @@ -11,5 +11,16 @@ namespace HN.Application [Required] public string Content { get; set; } + + // Constructeur vide nรฉcessaire pour le model binding + public CommentLinkCommand() + { + + } + + public CommentLinkCommand(Guid linkId) + { + LinkId = linkId; + } } } \ No newline at end of file diff --git a/Apps/Website/Controllers/CommentsController.cs b/Apps/Website/Controllers/CommentsController.cs new file mode 100644 index 0000000..1f73833 --- /dev/null +++ b/Apps/Website/Controllers/CommentsController.cs @@ -0,0 +1,34 @@ +using HN.Application; +using Website.Models; +using Microsoft.AspNetCore.Mvc; +using MediatR; +using System.Threading.Tasks; + +namespace Website.Controllers +{ + public sealed class CommentsController : BaseController + { + private readonly IMediator _bus; + + public CommentsController(IMediator bus) + { + _bus = bus; + } + + [ValidateAntiForgeryToken] + [HttpPost] + public async Task Create(CommentLinkCommand command) + { + if (!ModelState.IsValid) + { + return View("../Links/Show", new ShowLinkViewModel(await _bus.Send(new GetLinkQuery(command.LinkId)), command)); + } + + await _bus.Send(command); + + SetFlash("Comment added!"); + + return RedirectToAction(nameof(LinksController.Show), "Links", new { id = command.LinkId }); + } + } +} \ No newline at end of file diff --git a/Apps/Website/Controllers/LinksController.cs b/Apps/Website/Controllers/LinksController.cs index aa7e811..47b7d49 100644 --- a/Apps/Website/Controllers/LinksController.cs +++ b/Apps/Website/Controllers/LinksController.cs @@ -4,6 +4,7 @@ using MediatR; using System.Threading.Tasks; using System; using HN.Domain; +using Website.Models; namespace Website.Controllers { @@ -25,7 +26,7 @@ namespace Website.Controllers [HttpGet("{controller}/{id:guid}")] public async Task Show(Guid id) { - return View(await _bus.Send(new GetLinkQuery(id))); + return View(new ShowLinkViewModel(await _bus.Send(new GetLinkQuery(id)), new CommentLinkCommand(id))); } public IActionResult Create() @@ -35,12 +36,12 @@ namespace Website.Controllers [HttpPost("{controller}/{id:guid}/vote")] [ValidateAntiForgeryToken] - public async Task Vote(Guid id, string url, VoteType type) + public async Task Vote(Guid id, string url, VoteType type, string redirectTo) { await _bus.Send(new VoteForLinkCommand(id, type)); SetFlash($"Successfuly {type} for {url}!"); - return RedirectToAction(nameof(Index)); + return Redirect(redirectTo); } [HttpPost] diff --git a/Apps/Website/Models/ShowLinkViewModel.cs b/Apps/Website/Models/ShowLinkViewModel.cs new file mode 100644 index 0000000..739fc09 --- /dev/null +++ b/Apps/Website/Models/ShowLinkViewModel.cs @@ -0,0 +1,18 @@ +using HN.Application; + +namespace Website.Models +{ + public class ShowLinkViewModel + { + public LinkDto Link { get; set; } + + public CommentLinkCommand CommentForm { get; set; } + + public ShowLinkViewModel(LinkDto link, CommentLinkCommand commentForm) + { + Link = link; + CommentForm = commentForm; + } + } + +} \ No newline at end of file diff --git a/Apps/Website/Views/Links/Create.cshtml b/Apps/Website/Views/Links/Create.cshtml index 0de01d2..2161b0d 100644 --- a/Apps/Website/Views/Links/Create.cshtml +++ b/Apps/Website/Views/Links/Create.cshtml @@ -5,9 +5,13 @@
- @Html.LabelFor(m => m.Url) + @* @Html.LabelFor(m => m.Url) @Html.EditorFor(m => m.Url) - @Html.ValidationMessageFor(m => m.Url) + @Html.ValidationMessageFor(m => m.Url) *@ + + + +
diff --git a/Apps/Website/Views/Links/Show.cshtml b/Apps/Website/Views/Links/Show.cshtml index fced2aa..656418b 100644 --- a/Apps/Website/Views/Links/Show.cshtml +++ b/Apps/Website/Views/Links/Show.cshtml @@ -1,3 +1,4 @@ -@model HN.Application.LinkDto +@model ShowLinkViewModel - \ No newline at end of file + + \ No newline at end of file diff --git a/Apps/Website/Views/Shared/_CommentForm.cshtml b/Apps/Website/Views/Shared/_CommentForm.cshtml new file mode 100644 index 0000000..57fd06c --- /dev/null +++ b/Apps/Website/Views/Shared/_CommentForm.cshtml @@ -0,0 +1,12 @@ +@model HN.Application.CommentLinkCommand + +
+

Add a comment

+
+ + + + + +
+
\ No newline at end of file diff --git a/Apps/Website/Views/Links/_LinkItem.cshtml b/Apps/Website/Views/Shared/_LinkItem.cshtml similarity index 84% rename from Apps/Website/Views/Links/_LinkItem.cshtml rename to Apps/Website/Views/Shared/_LinkItem.cshtml index 7a6e43f..ecc19fc 100644 --- a/Apps/Website/Views/Links/_LinkItem.cshtml +++ b/Apps/Website/Views/Shared/_LinkItem.cshtml @@ -3,6 +3,7 @@ @Model.Url - created at @Model.CreatedAt.ToLocalTime() (๐Ÿ‘: @Model.UpVotes / ๐Ÿ‘Ž: @Model.DownVotes)
+ ๐Ÿ‘ ๐Ÿ‘Ž
\ No newline at end of file diff --git a/Domain/Link.cs b/Domain/Link.cs index dec8f08..363d111 100644 --- a/Domain/Link.cs +++ b/Domain/Link.cs @@ -9,7 +9,7 @@ namespace HN.Domain public string Url { get; } public DateTime CreatedAt { get; } private List _votes; - public IReadOnlyList Votes { get { return _votes; } } + public IReadOnlyList Votes => _votes; private Link(string url) {