publication d'un lien

This commit is contained in:
Julien LEICHER 2021-12-13 14:05:59 +01:00
parent fab4e35d73
commit ebfc06199e
No known key found for this signature in database
GPG Key ID: BE0761B6A007EB96
7 changed files with 90 additions and 31 deletions

View File

@ -0,0 +1,34 @@
using HackerNet.Application;
using HackerNet.Infrastructure.Repositories.Memory;
using Microsoft.AspNetCore.Mvc;
namespace HackerNet.Web.Controllers;
public class LinksController : Controller
{
private readonly LinkService _linkService;
public LinksController(LinkService linkService)
{
_linkService = linkService;
}
[HttpGet]
public IActionResult New()
{
return View(new PublishLinkCommand());
}
[HttpPost]
public IActionResult New(PublishLinkCommand cmd)
{
if (!ModelState.IsValid)
{
return View(cmd);
}
_linkService.PublishLink(cmd);
return RedirectToAction("Index", "Home");
}
}

View File

@ -1,6 +1,13 @@
using HackerNet.Application;
using HackerNet.Domain;
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>(
new MemoryLinkRepository(new Link("https://localhost:7050/Links/New", "Youhouuu")));
builder.Services.AddSingleton<LinkService>();
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
var app = builder.Build(); var app = builder.Build();
@ -8,9 +15,9 @@ var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts(); app.UseHsts();
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();

View File

@ -12,7 +12,7 @@
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
"applicationUrl": "https://localhost:7049;http://localhost:5245", "applicationUrl": "https://localhost:7050;http://localhost:5246",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }

View File

@ -0,0 +1,24 @@
@model HackerNet.Application.PublishLinkCommand
@{
ViewData["Title"] = "Publier un nouveau lien";
}
<div class="shadow-xl p-6">
<h1 class="font-semibold text-xl text-indigo-500">@ViewData["Title"]</h1>
<form asp-action="New" method="post">
<label asp-for="@Model.Url"></label>
<input class="mt-4 bg-gray-100 rounded-xl px-2 py-4 block w-full" asp-for="@Model.Url" />
<span class="text-red-700 text-sm block" asp-validation-for="@Model.Url"></span>
<label asp-for="@Model.Description"></label>
<textarea class="mt-4 bg-gray-100 rounded-xl px-2 py-4 block w-full" asp-for="@Model.Description">
</textarea>
<span class="text-red-700 text-sm block" asp-validation-for="@Model.Description"></span>
<div class="mt-4 flex justify-end">
<button type="submit" class="px-4 py-2 text-indigo-500 font-semibold rounded-xl bg-indigo-100">Publier un lien</button>
</div>
</form>
</div>

View File

@ -3,44 +3,27 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - HackerNet.Web</title> <title>@ViewData["Title"] - HackerNet</title>
<link href="https://unpkg.com/tailwindcss@@^2/dist/tailwind.min.css" rel="stylesheet">
</head> </head>
<body> <body>
<header> <header class="mx-auto max-w-4xl p-4">
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> <nav class="flex items-center justify-between">
<div class="container-fluid"> <a asp-controller="Home" asp-action="Index" class="font-semibold text-indigo-500">Hacker<strong>Net</strong></a>
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">HackerNet.Web</a> <div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent" <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>
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</div>
</div> </div>
</nav> </nav>
</header> </header>
<div class="container"> <div class="mx-auto max-w-4xl px-4">
<main role="main" class="pb-3"> <main role="main" class="pb-3">
@RenderBody() @RenderBody()
</main> </main>
</div> </div>
<footer class="border-top footer text-muted"> <footer class="mt-4 mx-auto max-w-4xl px-4">
<div class="container">
&copy; 2021 - HackerNet.Web - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a> &copy; 2021 - HackerNet.Web - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer> </footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@await RenderSectionAsync("Scripts", required: false) @await RenderSectionAsync("Scripts", required: false)
</body> </body>
</html> </html>

View File

@ -1,4 +1,5 @@
using HackerNet.Domain; using System.ComponentModel.DataAnnotations;
using HackerNet.Domain;
namespace HackerNet.Application; namespace HackerNet.Application;
public class LinkService public class LinkService
@ -22,6 +23,11 @@ public class LinkService
public class PublishLinkCommand public class PublishLinkCommand
{ {
[Required(ErrorMessage = "L'url est requise")]
[Display(Name = "Url du site")]
[Url]
public string Url { get; set; } public string Url { get; set; }
[Required(ErrorMessage = "La description est obligatoire")]
public string Description { get; set; } public string Description { get; set; }
} }

5
omnisharp.json Normal file
View File

@ -0,0 +1,5 @@
{
"msbuild": {
"useBundledOnly": true
}
}