From b20901cff6206af01d9da71c9b47c9b072c7a476 Mon Sep 17 00:00:00 2001 From: Julien LEICHER Date: Mon, 21 Dec 2020 11:10:32 +0100 Subject: [PATCH] add needed routes in LinksController --- Application/CommentLink/CommentLinkCommand.cs | 3 +- Apps/Api/Controllers/LinksController.cs | 70 +++++- Apps/Api/Models/AddCommentViewModel.cs | 10 + Apps/Api/swagger.json | 200 +++++++++++++++++- 4 files changed, 277 insertions(+), 6 deletions(-) create mode 100644 Apps/Api/Models/AddCommentViewModel.cs diff --git a/Application/CommentLink/CommentLinkCommand.cs b/Application/CommentLink/CommentLinkCommand.cs index 233be2a..a75abb6 100644 --- a/Application/CommentLink/CommentLinkCommand.cs +++ b/Application/CommentLink/CommentLinkCommand.cs @@ -18,9 +18,10 @@ namespace HN.Application } - public CommentLinkCommand(Guid linkId) + public CommentLinkCommand(Guid linkId, string content = null) { LinkId = linkId; + Content = content; } } } \ No newline at end of file diff --git a/Apps/Api/Controllers/LinksController.cs b/Apps/Api/Controllers/LinksController.cs index 9c2671b..9fe3cdb 100644 --- a/Apps/Api/Controllers/LinksController.cs +++ b/Apps/Api/Controllers/LinksController.cs @@ -1,5 +1,8 @@ +using System; using System.Threading.Tasks; +using Api.Models; using HN.Application; +using HN.Domain; using MediatR; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -18,7 +21,7 @@ namespace Api.Controllers } /// - /// Retrieve all links already posted. + /// Récupère tous les liens postés. /// /// [ProducesResponseType(typeof(LinkDto[]), StatusCodes.Status200OK)] @@ -29,7 +32,68 @@ namespace Api.Controllers } /// - /// Post a new link. + /// Récupère um lien particulier. + /// + /// + [HttpGet("{id}")] + public async Task> GetLinkById(Guid id) + { + return Ok(await _bus.Send(new GetLinkQuery(id))); + } + + /// + /// Upvote un lien particulier. + /// + /// + /// + [HttpPut("{id}/upvote")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + public async Task Upvote(Guid id) + { + await _bus.Send(new VoteForLinkCommand(id, VoteType.Up)); + return NoContent(); + } + + /// + /// Downvote un lien particulier. + /// + /// + /// + [HttpPut("{id}/downvote")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + public async Task Downvote(Guid id) + { + await _bus.Send(new VoteForLinkCommand(id, VoteType.Down)); + return NoContent(); + } + + /// + /// Récupère les commentaires d'un lien particulier. + /// + /// + /// + [HttpGet("{id}/comments")] + public async Task> Comments(Guid id) + { + return Ok(await _bus.Send(new GetLinkCommentsQuery(id))); + } + + /// + /// Poste un nouveau commentaire sur un lien. + /// + /// + /// + /// + [HttpPost("{id}/comments")] + [ProducesResponseType(StatusCodes.Status201Created)] + public async Task AddComment(Guid id, AddCommentViewModel command) + { + var commentId = await _bus.Send(new CommentLinkCommand(id, command.Content)); + return CreatedAtAction("", "", new { id = commentId }, null); + } + + /// + /// Poste un nouveau lien. /// /// /// @@ -38,7 +102,7 @@ namespace Api.Controllers { var result = await _bus.Send(command); - return Created("blabla", result); + return CreatedAtAction(nameof(GetLinkById), new { id = result }); } } } \ No newline at end of file diff --git a/Apps/Api/Models/AddCommentViewModel.cs b/Apps/Api/Models/AddCommentViewModel.cs new file mode 100644 index 0000000..3216a7e --- /dev/null +++ b/Apps/Api/Models/AddCommentViewModel.cs @@ -0,0 +1,10 @@ +using System.ComponentModel.DataAnnotations; + +namespace Api.Models +{ + public sealed class AddCommentViewModel + { + [Required] + public string Content { get; set; } + } +} \ No newline at end of file diff --git a/Apps/Api/swagger.json b/Apps/Api/swagger.json index 248b757..cdc6792 100644 --- a/Apps/Api/swagger.json +++ b/Apps/Api/swagger.json @@ -11,7 +11,7 @@ "tags": [ "Links" ], - "summary": "Retrieve all links already posted.", + "summary": "Récupère tous les liens postés.", "operationId": "Links_GetLinks", "responses": { "200": { @@ -43,7 +43,7 @@ "tags": [ "Links" ], - "summary": "Post a new link.", + "summary": "Poste un nouveau lien.", "operationId": "Links_CreateLink", "requestBody": { "x-name": "command", @@ -71,6 +71,163 @@ } } } + }, + "/api/links/{id}": { + "get": { + "tags": [ + "Links" + ], + "summary": "Récupère um lien particulier.", + "operationId": "Links_GetLinkById", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "guid" + }, + "x-position": 1 + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/LinkDto" + } + } + } + } + } + } + }, + "/api/links/{id}/upvote": { + "put": { + "tags": [ + "Links" + ], + "summary": "Upvote un lien particulier.", + "operationId": "Links_Upvote", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "guid" + }, + "x-position": 1 + } + ], + "responses": { + "204": { + "description": "" + } + } + } + }, + "/api/links/{id}/downvote": { + "put": { + "tags": [ + "Links" + ], + "summary": "Downvote un lien particulier.", + "operationId": "Links_Downvote", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "guid" + }, + "x-position": 1 + } + ], + "responses": { + "204": { + "description": "" + } + } + } + }, + "/api/links/{id}/comments": { + "get": { + "tags": [ + "Links" + ], + "summary": "Récupère les commentaires d'un lien particulier.", + "operationId": "Links_Comments", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "guid" + }, + "x-position": 1 + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/CommentDto" + } + } + } + } + } + } + }, + "post": { + "tags": [ + "Links" + ], + "summary": "Poste un nouveau commentaire sur un lien.", + "operationId": "Links_AddComment", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "guid" + }, + "x-position": 1 + } + ], + "requestBody": { + "x-name": "command", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/AddCommentViewModel" + } + } + }, + "required": true, + "x-position": 2 + }, + "responses": { + "201": { + "description": "" + } + } + } } }, "components": { @@ -139,6 +296,45 @@ } } }, + "CommentDto": { + "type": "object", + "additionalProperties": false, + "properties": { + "id": { + "type": "string", + "format": "guid" + }, + "content": { + "type": "string", + "nullable": true + }, + "createdAt": { + "type": "string", + "format": "date-time" + }, + "upVotes": { + "type": "integer", + "format": "int32" + }, + "downVotes": { + "type": "integer", + "format": "int32" + } + } + }, + "AddCommentViewModel": { + "type": "object", + "additionalProperties": false, + "required": [ + "content" + ], + "properties": { + "content": { + "type": "string", + "minLength": 1 + } + } + }, "AddLinkCommand": { "type": "object", "additionalProperties": false,