From a9ad4a39c16e05672341e30ff20b40cf49ccdd78 Mon Sep 17 00:00:00 2001 From: Julien LEICHER Date: Tue, 14 Dec 2021 16:06:36 +0100 Subject: [PATCH] ajout gestion des commentaires dans l'API --- .../Controllers/CommentsController.cs | 17 +++++++++ .../Controllers/LinksController.cs | 37 +++++++++++++++++++ .../Properties/launchSettings.json | 1 + Apps/HackerNet.Api/api.http | 11 +++++- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 Apps/HackerNet.Api/Controllers/CommentsController.cs diff --git a/Apps/HackerNet.Api/Controllers/CommentsController.cs b/Apps/HackerNet.Api/Controllers/CommentsController.cs new file mode 100644 index 0000000..d9e6361 --- /dev/null +++ b/Apps/HackerNet.Api/Controllers/CommentsController.cs @@ -0,0 +1,17 @@ +using HackerNet.Application; +using Microsoft.AspNetCore.Mvc; + +namespace HackerNet.Api.Controllers; + +[ApiController] +[Route("/api/comments")] +public class CommentsController : ControllerBase +{ + [HttpGet("{id:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public ActionResult GetById(Guid id) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/Apps/HackerNet.Api/Controllers/LinksController.cs b/Apps/HackerNet.Api/Controllers/LinksController.cs index 9d4d2be..7ced968 100644 --- a/Apps/HackerNet.Api/Controllers/LinksController.cs +++ b/Apps/HackerNet.Api/Controllers/LinksController.cs @@ -1,3 +1,4 @@ +using System.ComponentModel.DataAnnotations; using HackerNet.Application; using HackerNet.Infrastructure.AspNet.Filters; using Microsoft.AspNetCore.Mvc; @@ -39,6 +40,36 @@ public class LinksController : ControllerBase return _linkService.GetLinkDetail(id); } + /// + /// Récupère les commentaires associés au lien. + /// + /// + /// + [HttpGet("{id:guid}/comments")] + public ActionResult GetLinkComments(Guid id) + { + return _linkService.GetLinkComments(id); + } + + [HttpPost("{id:guid}/comments")] + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public ActionResult PublishComment(Guid id, PublishCommentBody cmd) + { + var commentId = _linkService.PublishComment(new PublishCommentCommand + { + LinkId = id, + Content = cmd.Content + }); + + return CreatedAtAction( + nameof(CommentsController.GetById), + "Comments", + new { id = commentId }, + null + ); + } + /// /// Publie un nouveau lien sur la plateforme /// @@ -53,4 +84,10 @@ public class LinksController : ControllerBase return CreatedAtAction(nameof(GetLinkDetail), new { id = id }, null); } +} + +public class PublishCommentBody +{ + [Required] + public string Content { get; set; } } \ No newline at end of file diff --git a/Apps/HackerNet.Api/Properties/launchSettings.json b/Apps/HackerNet.Api/Properties/launchSettings.json index a1c2d0c..9f0b344 100644 --- a/Apps/HackerNet.Api/Properties/launchSettings.json +++ b/Apps/HackerNet.Api/Properties/launchSettings.json @@ -12,6 +12,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, + "launchUrl": "swagger", "applicationUrl": "https://localhost:7252;http://localhost:5230", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Apps/HackerNet.Api/api.http b/Apps/HackerNet.Api/api.http index 1265606..611bacd 100644 --- a/Apps/HackerNet.Api/api.http +++ b/Apps/HackerNet.Api/api.http @@ -14,4 +14,13 @@ Content-Type: application/json ### -GET {{url}}/api/links/7f92770b-e3ef-4443-ab5d-8aca6449530f \ No newline at end of file +GET {{url}}/api/links/7f92770b-e3ef-4443-ab5d-8aca6449530f + +### + +POST {{url}}/api/links/03a79c72-99e2-482e-8c58-1a099004448f/comments +Content-Type: application/json + +{ + "content": "Contenu du commentaire" +}