ajout du détail d'un lien

This commit is contained in:
Julien LEICHER 2021-12-14 09:06:56 +01:00
parent 849d4e2260
commit da4179a065
No known key found for this signature in database
GPG Key ID: BE0761B6A007EB96
6 changed files with 34 additions and 9 deletions

View File

@ -1,5 +1,4 @@
using HackerNet.Application; using HackerNet.Application;
using HackerNet.Infrastructure.Repositories.Memory;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace HackerNet.Web.Controllers; namespace HackerNet.Web.Controllers;
@ -21,6 +20,14 @@ public class LinksController : Controller
// return View(_linkRepository.GetAll());// retourne Link[] // return View(_linkRepository.GetAll());// retourne Link[]
} }
// TODO: Page de détail d'un lien
[HttpGet]
public IActionResult Detail(Guid id)
{
return View(_linkService.GetLinkDetail(id));
}
[HttpGet] [HttpGet]
public IActionResult New() public IActionResult New()
{ {
@ -28,6 +35,7 @@ public class LinksController : Controller
} }
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken]
public IActionResult New(PublishLinkCommand cmd) public IActionResult New(PublishLinkCommand cmd)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)

View File

@ -0,0 +1,6 @@
@model HackerNet.Application.LinkHomePage
@{
ViewData["Title"] = $"Détail du lien {Model.Url}";
}
<partial name="_LinkCard" model="@Model" />

View File

@ -7,7 +7,7 @@
<link href="https://unpkg.com/tailwindcss@@^2/dist/tailwind.min.css" rel="stylesheet"> <link href="https://unpkg.com/tailwindcss@@^2/dist/tailwind.min.css" rel="stylesheet">
</head> </head>
<body> <body>
<header class="mx-auto max-w-4xl p-4"> <header class="mx-auto max-w-4xl px-4 py-8">
<nav class="flex items-center justify-between"> <nav class="flex items-center justify-between">
<a asp-controller="Links" asp-action="Index" class="font-semibold text-indigo-500">Hacker<strong>Net</strong></a> <a asp-controller="Links" asp-action="Index" class="font-semibold text-indigo-500">Hacker<strong>Net</strong></a>
<div> <div>

View File

@ -1,11 +1,11 @@
@model HackerNet.Application.LinkHomePage @model HackerNet.Application.LinkHomePage
<article class="shadow-xl mb-6 p-4 rounded-sm"> <article class="shadow-xl mb-6 p-6 rounded-sm">
<h3 class="text-indigo-500 font-semibold text-xl"><a href="@Model.Url">@Model.Url</a></h3> <h3 class="text-indigo-500 font-semibold text-xl"><a href="@Model.Url">@Model.Url</a></h3>
<p>@Model.Description</p> <p>@Model.Description</p>
<div> <div class="mt-4">
<a asp-controller="Links" asp-action="Detail" asp-route-id="@Model.Id">Détail du lien</a> <a class="text-indigo-500 hover:underline" asp-controller="Links" asp-action="Detail" asp-route-id="@Model.Id">Détail du lien</a>
</div> </div>
</article> </article>

View File

@ -23,14 +23,16 @@ public class LinkService
} }
public LinkHomePage[] GetPublishedLinks() public LinkHomePage[] GetPublishedLinks()
{ => _readStore.GetPublishedLinks();
return _readStore.GetPublishedLinks();
} public LinkHomePage GetLinkDetail(Guid id)
=> _readStore.GetLinkDetail(id);
} }
public interface IReadStore public interface IReadStore
{ {
LinkHomePage[] GetPublishedLinks(); LinkHomePage[] GetPublishedLinks();
LinkHomePage GetLinkDetail(Guid id);
} }
public class LinkHomePage public class LinkHomePage

View File

@ -19,6 +19,9 @@ public class MemoryLinkRepository : ILinkRepository, IReadStore
_links.Add(link); _links.Add(link);
} }
public LinkHomePage GetLinkDetail(Guid id)
=> GetLinks(id).Single();
public LinkHomePage[] GetPublishedLinks() public LinkHomePage[] GetPublishedLinks()
{ {
// return (from l in _links // return (from l in _links
@ -31,7 +34,13 @@ public class MemoryLinkRepository : ILinkRepository, IReadStore
// CommentsCount = 0, // CommentsCount = 0,
// }).ToArray(); // }).ToArray();
return GetLinks().ToArray();
}
private IEnumerable<LinkHomePage> GetLinks(Guid? id = null)
{
return _links return _links
.Where(l => !id.HasValue || l.Id == id)
.OrderByDescending(l => l.CreatedAt) .OrderByDescending(l => l.CreatedAt)
.Select(l => new LinkHomePage .Select(l => new LinkHomePage
{ {
@ -39,6 +48,6 @@ public class MemoryLinkRepository : ILinkRepository, IReadStore
Url = l.Url, Url = l.Url,
Description = l.Description, Description = l.Description,
CommentsCount = 0, CommentsCount = 0,
}).ToArray(); });
} }
} }