ajout liste des liens
This commit is contained in:
parent
ebfc06199e
commit
4e34679168
@ -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");
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
|
||||
15
Apps/HackerNet.Web/Views/Links/Index.cshtml
Normal file
15
Apps/HackerNet.Web/Views/Links/Index.cshtml
Normal 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" />
|
||||
}
|
||||
}
|
||||
@ -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>
|
||||
|
||||
7
Apps/HackerNet.Web/Views/Shared/_LinkCard.cshtml
Normal file
7
Apps/HackerNet.Web/Views/Shared/_LinkCard.cshtml
Normal 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>
|
||||
@ -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
|
||||
|
||||
@ -3,4 +3,6 @@ namespace HackerNet.Domain;
|
||||
public interface ILinkRepository
|
||||
{
|
||||
void Add(Link link);
|
||||
}
|
||||
|
||||
// Link[] GetAll();
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user