add form to comment and redirection
This commit is contained in:
parent
39a2523381
commit
1da3f513d4
@ -11,5 +11,16 @@ namespace HN.Application
|
|||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public string Content { get; set; }
|
public string Content { get; set; }
|
||||||
|
|
||||||
|
// Constructeur vide nécessaire pour le model binding
|
||||||
|
public CommentLinkCommand()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommentLinkCommand(Guid linkId)
|
||||||
|
{
|
||||||
|
LinkId = linkId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
34
Apps/Website/Controllers/CommentsController.cs
Normal file
34
Apps/Website/Controllers/CommentsController.cs
Normal 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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ using MediatR;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System;
|
using System;
|
||||||
using HN.Domain;
|
using HN.Domain;
|
||||||
|
using Website.Models;
|
||||||
|
|
||||||
namespace Website.Controllers
|
namespace Website.Controllers
|
||||||
{
|
{
|
||||||
@ -25,7 +26,7 @@ namespace Website.Controllers
|
|||||||
[HttpGet("{controller}/{id:guid}")]
|
[HttpGet("{controller}/{id:guid}")]
|
||||||
public async Task<IActionResult> Show(Guid id)
|
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()
|
public IActionResult Create()
|
||||||
@ -35,12 +36,12 @@ namespace Website.Controllers
|
|||||||
|
|
||||||
[HttpPost("{controller}/{id:guid}/vote")]
|
[HttpPost("{controller}/{id:guid}/vote")]
|
||||||
[ValidateAntiForgeryToken]
|
[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));
|
await _bus.Send(new VoteForLinkCommand(id, type));
|
||||||
|
|
||||||
SetFlash($"Successfuly {type} for {url}!");
|
SetFlash($"Successfuly {type} for {url}!");
|
||||||
return RedirectToAction(nameof(Index));
|
return Redirect(redirectTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
|
|||||||
18
Apps/Website/Models/ShowLinkViewModel.cs
Normal file
18
Apps/Website/Models/ShowLinkViewModel.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -5,9 +5,13 @@
|
|||||||
|
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<div>
|
<div>
|
||||||
@Html.LabelFor(m => m.Url)
|
@* @Html.LabelFor(m => m.Url)
|
||||||
@Html.EditorFor(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>
|
</div>
|
||||||
<button type="submit">Post!</button>
|
<button type="submit">Post!</button>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@ -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" />
|
||||||
12
Apps/Website/Views/Shared/_CommentForm.cshtml
Normal file
12
Apps/Website/Views/Shared/_CommentForm.cshtml
Normal 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>
|
||||||
@ -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>
|
<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">
|
<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="up">👍</button>
|
||||||
<input type="submit" name="type" value="down">👎</button>
|
<input type="submit" name="type" value="down">👎</button>
|
||||||
</form>
|
</form>
|
||||||
@ -9,7 +9,7 @@ namespace HN.Domain
|
|||||||
public string Url { get; }
|
public string Url { get; }
|
||||||
public DateTime CreatedAt { get; }
|
public DateTime CreatedAt { get; }
|
||||||
private List<Vote> _votes;
|
private List<Vote> _votes;
|
||||||
public IReadOnlyList<Vote> Votes { get { return _votes; } }
|
public IReadOnlyList<Vote> Votes => _votes;
|
||||||
|
|
||||||
private Link(string url)
|
private Link(string url)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user