comment-a-link #24

Merged
jleicher merged 3 commits from comment-a-link into master 2020-12-11 09:46:43 +01:00
9 changed files with 90 additions and 8 deletions
Showing only changes of commit 1da3f513d4 - Show all commits

View File

@ -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;
}
}
}

View File

@ -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<IActionResult> 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 });
}
}
}

View File

@ -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<IActionResult> 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<IActionResult> Vote(Guid id, string url, VoteType type)
public async Task<IActionResult> 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]

View File

@ -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;
}
}
}

View File

@ -5,9 +5,13 @@
<form method="post">
<div>
@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) *@
<label asp-for="@Model.Url"></label>
<input asp-for="@Model.Url" />
<span asp-validation-for="@Model.Url"></span>
</div>
<button type="submit">Post!</button>
</form>

View File

@ -1,3 +1,4 @@
@model HN.Application.LinkDto
@model ShowLinkViewModel
<partial name="_LinkItem" model="@Model" />
<partial name="_LinkItem" model="@Model.Link" />
<partial name="_CommentForm" model="@Model.CommentForm" />

View File

@ -0,0 +1,12 @@
@model HN.Application.CommentLinkCommand
<div>
<h2>Add a comment</h2>
<form asp-action="Create" asp-controller="Comments" method="post">
<input type="hidden" asp-for="@Model.LinkId" />
<textarea asp-for="@Model.Content"></textarea>
<span asp-validation-for="@Model.Content"></span>
<button type="submit">Post a comment</button>
</form>
</div>

View File

@ -3,6 +3,7 @@
<a asp-action="Show" asp-controller="Links" asp-route-id="@Model.Id">@Model.Url - created at @Model.CreatedAt.ToLocalTime() (👍: @Model.UpVotes / 👎: @Model.DownVotes)</a>
<form asp-controller="Links" asp-action="Vote" asp-route-id="@Model.Id" asp-route-url="@Model.Url" method="post">
<input type="hidden" name="redirectTo" value="@Context.Request.Path" />
<input type="submit" name="type" value="up">👍</button>
<input type="submit" name="type" value="down">👎</button>
</form>

View File

@ -9,7 +9,7 @@ namespace HN.Domain
public string Url { get; }
public DateTime CreatedAt { get; }
private List<Vote> _votes;
public IReadOnlyList<Vote> Votes { get { return _votes; } }
public IReadOnlyList<Vote> Votes => _votes;
private Link(string url)
{