From c9b27393a043c7c52975a5bc9704b5c6e1ed9d0a Mon Sep 17 00:00:00 2001 From: Julien LEICHER Date: Wed, 15 Dec 2021 14:24:42 +0100 Subject: [PATCH] ajout controller Accounts api --- .vscode/launch.json | 24 +++++++++++- .vscode/tasks.json | 14 ++++++- .../Controllers/AccountsController.cs | 39 +++++++++++++++++++ Apps/HackerNet.Api/HackerNet.Api.csproj | 1 + .../Models/SignupLoginViewModel.cs | 13 +++++++ Apps/HackerNet.Api/Program.cs | 19 ++++++++- Apps/HackerNet.Api/api.http | 9 +++++ .../appsettings.Development.json | 3 ++ 8 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 Apps/HackerNet.Api/Controllers/AccountsController.cs create mode 100644 Apps/HackerNet.Api/Models/SignupLoginViewModel.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index b8fe974..a19c60a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -26,10 +26,32 @@ "/Views": "${workspaceFolder}/Views" } }, + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (api)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/Apps/HackerNet.Api/bin/Debug/net6.0/HackerNet.Api.dll", + "args": [], + "cwd": "${workspaceFolder}/Apps/HackerNet.Api", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach" } ] -} \ No newline at end of file +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 67999b6..b8f1e69 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -13,6 +13,18 @@ ], "problemMatcher": "$msCompile" }, + { + "label": "buildapi", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Apps/HackerNet.Api/HackerNet.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, { "label": "publish", "command": "dotnet", @@ -39,4 +51,4 @@ "problemMatcher": "$msCompile" } ] -} \ No newline at end of file +} diff --git a/Apps/HackerNet.Api/Controllers/AccountsController.cs b/Apps/HackerNet.Api/Controllers/AccountsController.cs new file mode 100644 index 0000000..8bac125 --- /dev/null +++ b/Apps/HackerNet.Api/Controllers/AccountsController.cs @@ -0,0 +1,39 @@ +using HackerNet.Api.Models; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; + +namespace HackerNet.Api.Controllers; + +[ApiController] +[Route("/api/accounts")] +public class AccountsController : ControllerBase +{ + private readonly UserManager _userManager; + private readonly SignInManager _signInManager; + + public AccountsController(UserManager userManager, SignInManager signInManager) + { + _userManager = userManager; + _signInManager = signInManager; + } + + [HttpGet("me")] + public ActionResult Me() + { + throw new NotImplementedException(); + } + + [HttpPost] + public async Task Signup(SignupLoginViewModel cmd) + { + var user = new IdentityUser(cmd.Username); + var result = await _userManager.CreateAsync(user, cmd.Password); + + if (!result.Succeeded) + { + return BadRequest(); + } + + return CreatedAtAction(nameof(Me), null); + } +} \ No newline at end of file diff --git a/Apps/HackerNet.Api/HackerNet.Api.csproj b/Apps/HackerNet.Api/HackerNet.Api.csproj index 3ae7d7a..effe5ca 100644 --- a/Apps/HackerNet.Api/HackerNet.Api.csproj +++ b/Apps/HackerNet.Api/HackerNet.Api.csproj @@ -5,6 +5,7 @@ + diff --git a/Apps/HackerNet.Api/Models/SignupLoginViewModel.cs b/Apps/HackerNet.Api/Models/SignupLoginViewModel.cs new file mode 100644 index 0000000..b47d058 --- /dev/null +++ b/Apps/HackerNet.Api/Models/SignupLoginViewModel.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace HackerNet.Api.Models; + +public class SignupLoginViewModel +{ + [Required] + public string Username { get; set; } + + [Required] + [DataType(DataType.Password)] + public string Password { get; set; } +} \ No newline at end of file diff --git a/Apps/HackerNet.Api/Program.cs b/Apps/HackerNet.Api/Program.cs index fc9a031..e620df4 100644 --- a/Apps/HackerNet.Api/Program.cs +++ b/Apps/HackerNet.Api/Program.cs @@ -1,9 +1,23 @@ using HackerNet.Infrastructure.AspNet; +using HackerNet.Infrastructure.Repositories.EntityFramework; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.DependencyInjection.Extensions; var builder = WebApplication.CreateBuilder(args); -builder.Services.AddHackerNetServicesEntityFramework(); +builder.Services.AddHackerNetServicesEntityFramework(builder.Configuration); builder.Services.AddControllers(); +builder.Services + .AddIdentityCore() + .AddRoles() + .AddSignInManager() + .AddEntityFrameworkStores(); + +builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(); + builder.Services.AddOpenApiDocument(d => { d.Title = "HackerNet API"; @@ -16,6 +30,9 @@ app.MigrateDatabase(); app.UseOpenApi(); app.UseSwaggerUi3(); +app.UseAuthentication(); +app.UseAuthorization(); + app.MapGet("/", () => "Hello World!"); app.MapControllers(); diff --git a/Apps/HackerNet.Api/api.http b/Apps/HackerNet.Api/api.http index 611bacd..d026f37 100644 --- a/Apps/HackerNet.Api/api.http +++ b/Apps/HackerNet.Api/api.http @@ -24,3 +24,12 @@ Content-Type: application/json { "content": "Contenu du commentaire" } + +### + +POST {{url}}/api/accounts +Content-Type: application/json + +{ + +} \ No newline at end of file diff --git a/Apps/HackerNet.Api/appsettings.Development.json b/Apps/HackerNet.Api/appsettings.Development.json index ff66ba6..e3873b5 100644 --- a/Apps/HackerNet.Api/appsettings.Development.json +++ b/Apps/HackerNet.Api/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "ConnectionStrings": { + "Default": "Data Source=../HackerNet.Web/hackernet.db" } }