ajout liste des liens
This commit is contained in:
parent
ebfc06199e
commit
4e34679168
@ -13,6 +13,14 @@ public class LinksController : Controller
|
|||||||
_linkService = linkService;
|
_linkService = linkService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult Index()
|
||||||
|
{
|
||||||
|
return View(_linkService.GetPublishedLinks());
|
||||||
|
// return View(db.Links.ToArray());// retourne Link[]
|
||||||
|
// return View(_linkRepository.GetAll());// retourne Link[]
|
||||||
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult New()
|
public IActionResult New()
|
||||||
{
|
{
|
||||||
@ -29,6 +37,6 @@ public class LinksController : Controller
|
|||||||
|
|
||||||
_linkService.PublishLink(cmd);
|
_linkService.PublishLink(cmd);
|
||||||
|
|
||||||
return RedirectToAction("Index", "Home");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -5,8 +5,11 @@ using HackerNet.Infrastructure.Repositories.Memory;
|
|||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddSingleton<ILinkRepository>(
|
var linksRepository = new MemoryLinkRepository(new Link("https://localhost:7050/", "Youhouuu"));
|
||||||
new MemoryLinkRepository(new Link("https://localhost:7050/Links/New", "Youhouuu")));
|
|
||||||
|
builder.Services.AddSingleton<ILinkRepository>(linksRepository);
|
||||||
|
builder.Services.AddSingleton<IReadStore>(linksRepository);
|
||||||
|
|
||||||
builder.Services.AddSingleton<LinkService>();
|
builder.Services.AddSingleton<LinkService>();
|
||||||
builder.Services.AddControllersWithViews();
|
builder.Services.AddControllersWithViews();
|
||||||
|
|
||||||
@ -29,6 +32,6 @@ app.UseAuthorization();
|
|||||||
|
|
||||||
app.MapControllerRoute(
|
app.MapControllerRoute(
|
||||||
name: "default",
|
name: "default",
|
||||||
pattern: "{controller=Home}/{action=Index}/{id?}");
|
pattern: "{controller=Links}/{action=Index}/{id?}");
|
||||||
|
|
||||||
app.Run();
|
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>
|
<body>
|
||||||
<header class="mx-auto max-w-4xl p-4">
|
<header class="mx-auto max-w-4xl p-4">
|
||||||
<nav class="flex items-center justify-between">
|
<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>
|
<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>
|
<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>
|
</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
|
public class LinkService
|
||||||
{
|
{
|
||||||
private readonly ILinkRepository _repository;
|
private readonly ILinkRepository _repository;
|
||||||
|
private readonly IReadStore _readStore;
|
||||||
|
|
||||||
public LinkService(ILinkRepository repository)
|
public LinkService(ILinkRepository repository, IReadStore readStore)
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
|
_readStore = readStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Guid PublishLink(PublishLinkCommand cmd)
|
public Guid PublishLink(PublishLinkCommand cmd)
|
||||||
@ -19,6 +21,24 @@ public class LinkService
|
|||||||
|
|
||||||
return link.Id;
|
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
|
public class PublishLinkCommand
|
||||||
|
|||||||
@ -3,4 +3,6 @@ namespace HackerNet.Domain;
|
|||||||
public interface ILinkRepository
|
public interface ILinkRepository
|
||||||
{
|
{
|
||||||
void Add(Link link);
|
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