Compare commits
1 Commits
3046aea2ac
...
d573536ba4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d573536ba4 |
46
Apps/Website/Controllers/CommentsController.cs
Normal file
46
Apps/Website/Controllers/CommentsController.cs
Normal 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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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!");
|
||||
|
||||
|
||||
16
Apps/Website/Models/ShowLinkViewModel.cs
Normal file
16
Apps/Website/Models/ShowLinkViewModel.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<ILinkRepository>(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<ICommentRepository>(new Infrastructure.Repositories.Memory.CommentRepository(
|
||||
link1.AddComment("my first comment"),
|
||||
link3.AddComment("another comment")
|
||||
));
|
||||
services.AddSingleton<IData>(serviceProvider =>
|
||||
{
|
||||
var memoryRepository = serviceProvider.GetRequiredService<ILinkRepository>() as Infrastructure.Repositories.Memory.LinkRepository;
|
||||
return new Infrastructure.Repositories.Memory.Data(memoryRepository);
|
||||
var memoryLinkRepository = serviceProvider.GetRequiredService<ILinkRepository>() as Infrastructure.Repositories.Memory.LinkRepository;
|
||||
var memoryCommentRepository = serviceProvider.GetRequiredService<ICommentRepository>() as Infrastructure.Repositories.Memory.CommentRepository;
|
||||
return new Infrastructure.Repositories.Memory.Data(memoryLinkRepository, memoryCommentRepository);
|
||||
});
|
||||
services.AddTransient<LinkService>();
|
||||
services.AddTransient<CommentService>();
|
||||
services.AddControllersWithViews();
|
||||
}
|
||||
|
||||
|
||||
16
Apps/Website/Views/Comments/Create.cshtml
Normal file
16
Apps/Website/Views/Comments/Create.cshtml
Normal 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>
|
||||
@ -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" /> *@
|
||||
<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>
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user