From 9f260f7424cb70af31fedfbe9b22103acd3be736 Mon Sep 17 00:00:00 2001 From: Julien LEICHER Date: Tue, 22 Dec 2020 11:02:10 +0100 Subject: [PATCH] add comments controller --- Apps/Api/Controllers/AccountsController.cs | 5 ++ Apps/Api/Controllers/CommentsController.cs | 48 ++++++++++++++++++++ Apps/Api/Startup.cs | 4 +- Apps/Api/swagger.json | 53 ++++++++++++++++++++++ 4 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 Apps/Api/Controllers/CommentsController.cs diff --git a/Apps/Api/Controllers/AccountsController.cs b/Apps/Api/Controllers/AccountsController.cs index 4a29fb9..4f5fba0 100644 --- a/Apps/Api/Controllers/AccountsController.cs +++ b/Apps/Api/Controllers/AccountsController.cs @@ -26,6 +26,11 @@ namespace Api.Controllers _tokenParameters = tokenParameters; } + /// + /// Récupère un jeton d'accès pour un utilisateur particulier. + /// + /// + /// [HttpPost("login")] [AllowAnonymous] public async Task Login(LoginViewModel command) diff --git a/Apps/Api/Controllers/CommentsController.cs b/Apps/Api/Controllers/CommentsController.cs new file mode 100644 index 0000000..4db8c37 --- /dev/null +++ b/Apps/Api/Controllers/CommentsController.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; +using HN.Application; +using HN.Domain; +using MediatR; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Api.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public sealed class CommentsController : ControllerBase + { + private readonly IMediator _bus; + + public CommentsController(IMediator bus) + { + _bus = bus; + } + + /// + /// Upvote un commentaire particulier. + /// + /// + /// + [HttpPut("{id}/upvote")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + public async Task Upvote(Guid id) + { + await _bus.Send(new VoteForCommentCommand(id, VoteType.Up)); + return NoContent(); + } + + /// + /// Downvote un commentaire particulier. + /// + /// + /// + [HttpPut("{id}/downvote")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + public async Task Downvote(Guid id) + { + await _bus.Send(new VoteForCommentCommand(id, VoteType.Down)); + return NoContent(); + } + } +} \ No newline at end of file diff --git a/Apps/Api/Startup.cs b/Apps/Api/Startup.cs index 06439a3..f6570a8 100644 --- a/Apps/Api/Startup.cs +++ b/Apps/Api/Startup.cs @@ -71,7 +71,7 @@ namespace Api o.Filters.Add(new AuthorizeFilter()); // Ou MapControllers().RequireAuthentication() }); - services.AddSwaggerDocument(d => + services.AddOpenApiDocument(d => { // cf. https://github.com/RicoSuter/NSwag/wiki/AspNetCore-Middleware#enable-authentication-in-generator-and-swagger-ui @@ -100,8 +100,6 @@ namespace Api { od.Info.Title = "Hacker news like API in .Net"; }; - - d.SchemaType = NJsonSchema.SchemaType.OpenApi3; }); } diff --git a/Apps/Api/swagger.json b/Apps/Api/swagger.json index d16fda3..4a56ec2 100644 --- a/Apps/Api/swagger.json +++ b/Apps/Api/swagger.json @@ -11,6 +11,7 @@ "tags": [ "Accounts" ], + "summary": "Récupère un jeton d'accès pour un utilisateur particulier.", "operationId": "Accounts_Login", "requestBody": { "x-name": "command", @@ -39,6 +40,58 @@ } } }, + "/api/comments/{id}/upvote": { + "put": { + "tags": [ + "Comments" + ], + "summary": "Upvote un commentaire particulier.", + "operationId": "Comments_Upvote", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "guid" + }, + "x-position": 1 + } + ], + "responses": { + "204": { + "description": "" + } + } + } + }, + "/api/comments/{id}/downvote": { + "put": { + "tags": [ + "Comments" + ], + "summary": "Downvote un commentaire particulier.", + "operationId": "Comments_Downvote", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "guid" + }, + "x-position": 1 + } + ], + "responses": { + "204": { + "description": "" + } + } + } + }, "/api/links": { "get": { "tags": [