ajout liste des liens

This commit is contained in:
Julien LEICHER 2021-12-13 15:07:54 +01:00
parent ebfc06199e
commit 4e34679168
No known key found for this signature in database
GPG Key ID: BE0761B6A007EB96
9 changed files with 94 additions and 27 deletions

View File

@ -13,6 +13,14 @@ public class LinksController : Controller
_linkService = linkService;
}
[HttpGet]
public IActionResult Index()
{
return View(_linkService.GetPublishedLinks());
// return View(db.Links.ToArray());// retourne Link[]
// return View(_linkRepository.GetAll());// retourne Link[]
}
[HttpGet]
public IActionResult New()
{
@ -29,6 +37,6 @@ public class LinksController : Controller
_linkService.PublishLink(cmd);
return RedirectToAction("Index", "Home");
return RedirectToAction("Index");
}
}

View File

@ -5,8 +5,11 @@ using HackerNet.Infrastructure.Repositories.Memory;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddSingleton<ILinkRepository>(
new MemoryLinkRepository(new Link("https://localhost:7050/Links/New", "Youhouuu")));
var linksRepository = new MemoryLinkRepository(new Link("https://localhost:7050/", "Youhouuu"));
builder.Services.AddSingleton<ILinkRepository>(linksRepository);
builder.Services.AddSingleton<IReadStore>(linksRepository);
builder.Services.AddSingleton<LinkService>();
builder.Services.AddControllersWithViews();
@ -29,6 +32,6 @@ app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
pattern: "{controller=Links}/{action=Index}/{id?}");
app.Run();

View File

@ -0,0 +1,15 @@
@model HackerNet.Application.LinkHomePage[]
@{
ViewData["Title"] = "Derniers liens publiés";
}
@if(Model.Length == 0){
<p>Aucun lien publié pour le moment.</p>
}
else
{
foreach (var link in Model)
{
<partial name="_LinkCard" model="@link" />
}
}

View File

@ -9,7 +9,7 @@
<body>
<header class="mx-auto max-w-4xl p-4">
<nav class="flex items-center justify-between">
<a asp-controller="Home" 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>
<a asp-controller="Links" asp-action="New" class="px-4 py-2 text-indigo-500 font-semibold rounded-xl bg-indigo-100">Publier un lien</a>
</div>

View File

@ -0,0 +1,7 @@
@model HackerNet.Application.LinkHomePage
<article class="shadow-xl mb-6 p-4 rounded-sm">
<h3 class="text-indigo-500 font-semibold text-xl"><a href="@Model.Url">@Model.Url</a></h3>
<p>@Model.Description</p>
</article>

View File

@ -5,10 +5,12 @@ namespace HackerNet.Application;
public class LinkService
{
private readonly ILinkRepository _repository;
private readonly IReadStore _readStore;
public LinkService(ILinkRepository repository)
public LinkService(ILinkRepository repository, IReadStore readStore)
{
_repository = repository;
_readStore = readStore;
}
public Guid PublishLink(PublishLinkCommand cmd)
@ -19,6 +21,24 @@ public class LinkService
return link.Id;
}
public LinkHomePage[] GetPublishedLinks()
{
return _readStore.GetPublishedLinks();
}
}
public interface IReadStore
{
LinkHomePage[] GetPublishedLinks();
}
public class LinkHomePage
{
public Guid Id { get; set; }
public string Url { get; set; }
public string Description { get; set; }
public int CommentsCount { get; set; }
}
public class PublishLinkCommand

View File

@ -3,4 +3,6 @@ namespace HackerNet.Domain;
public interface ILinkRepository
{
void Add(Link link);
}
// Link[] GetAll();
}

View File

@ -0,0 +1,32 @@
using HackerNet.Application;
using HackerNet.Domain;
namespace HackerNet.Infrastructure.Repositories.Memory;
public class MemoryLinkRepository : ILinkRepository, IReadStore
{
private List<Link> _links;
public MemoryLinkRepository(params Link[] links)
{
_links = links.ToList();
}
public void Add(Link link)
{
_links.Add(link);
}
public LinkHomePage[] GetPublishedLinks()
{
return _links.Select(l => new LinkHomePage
{
Id = l.Id,
Url = l.Url,
Description = l.Description,
CommentsCount = 0,
}).ToArray();
}
}

View File

@ -1,20 +0,0 @@
using HackerNet.Domain;
namespace HackerNet.Infrastructure.Repositories.Memory;
public class MemoryLinkRepository : ILinkRepository
{
private List<Link> _links;
public MemoryLinkRepository(params Link[] links)
{
_links = links.ToList();
}
public void Add(Link link)
{
_links.Add(link);
}
}