Ajout de l'API

This commit is contained in:
YuukanOO 2021-04-27 15:19:57 +02:00
parent c82e8d7f26
commit f4ebf153e2
12 changed files with 271 additions and 1 deletions

24
Apps/Api/Api.csproj Normal file
View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NSwag.AspNetCore" Version="13.10.9" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarm);1591</NoWarn>
</PropertyGroup>
<!-- <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>C:\Users\Julien\Documents\Sources\cesi\hn-20-2\Apps\Api\Api.xml</DocumentationFile>
</PropertyGroup> -->
</Project>

28
Apps/Api/Api.xml Normal file
View File

@ -0,0 +1,28 @@
<?xml version="1.0"?>
<doc>
<assembly>
<name>Api</name>
</assembly>
<members>
<member name="M:Api.Controllers.LinksController.Index">
<summary>
Récupère la liste liste des derniers liens publiés.
</summary>
<returns></returns>
</member>
<member name="M:Api.Controllers.LinksController.Show(System.Guid)">
<summary>
Récupère les détails d'un lien.
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:Api.Controllers.LinksController.Create(Application.PublishLinkCommand)">
<summary>
Permet de publier un nouveau lien sur la plateforme.
</summary>
<param name="cmd"></param>
<returns></returns>
</member>
</members>
</doc>

View File

@ -0,0 +1,57 @@
using System;
using Application;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Api.Controllers
{
[Route("api/links")]
[ApiController]
public class LinksController : ControllerBase
{
private readonly LinkService _linkService;
public LinksController(LinkService linkService)
{
_linkService = linkService;
}
/// <summary>
/// Récupère la liste liste des derniers liens publiés.
/// </summary>
/// <returns></returns>
[HttpGet]
public LinkDTO[] Index()
{
return _linkService.GetAllLinks();
}
/// <summary>
/// Récupère les détails d'un lien.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id:guid}")]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status200OK)]
public LinkDTO Show(Guid id)
{
return _linkService.GetLinkById(id);
}
/// <summary>
/// Permet de publier un nouveau lien sur la plateforme.
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status201Created)]
public IActionResult Create(PublishLinkCommand cmd)
{
var linkId = _linkService.PublishLink(cmd);
return CreatedAtAction(nameof(Show), new { id = linkId }, null);
}
}
}

26
Apps/Api/Program.cs Normal file
View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Api
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}

View File

@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:11516",
"sslPort": 44313
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Api": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

60
Apps/Api/Startup.cs Normal file
View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Infrastructure;
using Infrastructure.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Api
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddHNServices();
services.AddControllers(options =>
{
options.Filters.Add<CustomExceptionFilter>();
});
services.AddOpenApiDocument(doc =>
{
doc.PostProcess = od =>
{
od.Info.Title = "Hacker News Clone API";
};
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseOpenApi();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwaggerUi3();
// app.UseReDoc();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
endpoints.MapControllers();
});
}
}
}

12
Apps/Api/api.http Normal file
View File

@ -0,0 +1,12 @@
@url = http://localhost:5000
GET {{url}}/api/links
###
POST {{url}}/api/links
Content-Type: application/json
{
"url": "http://google.com"
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

10
Apps/Api/appsettings.json Normal file
View File

@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}

View File

@ -1,6 +1,7 @@
using Application; using Application;
using Domain; using Domain;
using Infrastructure; using Infrastructure;
using Infrastructure.Filters;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;

View File

@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
namespace Website namespace Infrastructure.Filters
{ {
public class CustomExceptionFilter : IExceptionFilter public class CustomExceptionFilter : IExceptionFilter
{ {

View File

@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLI", "Apps\CLI\CLI.csproj"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Website", "Apps\Website\Website.csproj", "{50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Website", "Apps\Website\Website.csproj", "{50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "Apps\Api\Api.csproj", "{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -88,9 +90,22 @@ Global
{50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4}.Release|x64.Build.0 = Release|Any CPU {50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4}.Release|x64.Build.0 = Release|Any CPU
{50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4}.Release|x86.ActiveCfg = Release|Any CPU {50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4}.Release|x86.ActiveCfg = Release|Any CPU
{50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4}.Release|x86.Build.0 = Release|Any CPU {50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4}.Release|x86.Build.0 = Release|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Debug|x64.ActiveCfg = Debug|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Debug|x64.Build.0 = Debug|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Debug|x86.ActiveCfg = Debug|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Debug|x86.Build.0 = Debug|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Release|Any CPU.Build.0 = Release|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Release|x64.ActiveCfg = Release|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Release|x64.Build.0 = Release|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Release|x86.ActiveCfg = Release|Any CPU
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{F76DEBDB-E281-41B6-B476-15871F6BD8E1} = {04765FD6-F289-4A9F-8FBE-9FCB817513DD} {F76DEBDB-E281-41B6-B476-15871F6BD8E1} = {04765FD6-F289-4A9F-8FBE-9FCB817513DD}
{50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4} = {04765FD6-F289-4A9F-8FBE-9FCB817513DD} {50B328CF-C94B-4CF6-BD4B-A3C5D122EDB4} = {04765FD6-F289-4A9F-8FBE-9FCB817513DD}
{3514C7EF-B0C1-483A-BB9D-1CDADFC2C104} = {04765FD6-F289-4A9F-8FBE-9FCB817513DD}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal