aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Controllers/CommentController.cs
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-02-13 16:20:18 +0200
committertranstrike <transtrike@gmail.com>2021-02-13 16:20:18 +0200
commit98e17766b203734a1817eed94338e2d25f4395f7 (patch)
tree1266385a56cba56fd55c7faf661dd844bbdf5705 /src/Web/DevHive.Web/Controllers/CommentController.cs
parent1ab34accfda22ee3ce5c7700e3b97ff3e932d649 (diff)
downloadDevHive-98e17766b203734a1817eed94338e2d25f4395f7.tar
DevHive-98e17766b203734a1817eed94338e2d25f4395f7.tar.gz
DevHive-98e17766b203734a1817eed94338e2d25f4395f7.zip
Project Restructure P.1
Diffstat (limited to 'src/Web/DevHive.Web/Controllers/CommentController.cs')
-rw-r--r--src/Web/DevHive.Web/Controllers/CommentController.cs83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/Web/DevHive.Web/Controllers/CommentController.cs b/src/Web/DevHive.Web/Controllers/CommentController.cs
new file mode 100644
index 0000000..c38e300
--- /dev/null
+++ b/src/Web/DevHive.Web/Controllers/CommentController.cs
@@ -0,0 +1,83 @@
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Mvc;
+using AutoMapper;
+using System;
+using DevHive.Web.Models.Comment;
+using DevHive.Services.Models.Comment;
+using Microsoft.AspNetCore.Authorization;
+using DevHive.Services.Interfaces;
+
+namespace DevHive.Web.Controllers
+{
+ [ApiController]
+ [Route("/api/[controller]")]
+ [Authorize(Roles = "User,Admin")]
+ public class CommentController
+ {
+ private readonly ICommentService _commentService;
+ private readonly IMapper _commentMapper;
+
+ public CommentController(ICommentService commentService, IMapper commentMapper)
+ {
+ this._commentService = commentService;
+ this._commentMapper = commentMapper;
+ }
+
+ [HttpPost]
+ public async Task<IActionResult> AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization)
+ {
+ if (!await this._commentService.ValidateJwtForCreating(userId, authorization))
+ return new UnauthorizedResult();
+
+ CreateCommentServiceModel createCommentServiceModel =
+ this._commentMapper.Map<CreateCommentServiceModel>(createCommentWebModel);
+ createCommentServiceModel.CreatorId = userId;
+
+ Guid id = await this._commentService.AddComment(createCommentServiceModel);
+
+ return id == Guid.Empty ?
+ new BadRequestObjectResult("Could not create comment!") :
+ new OkObjectResult(new { Id = id });
+ }
+
+ [HttpGet]
+ [AllowAnonymous]
+ public async Task<IActionResult> GetCommentById(Guid id)
+ {
+ ReadCommentServiceModel readCommentServiceModel = await this._commentService.GetCommentById(id);
+ ReadCommentWebModel readCommentWebModel = this._commentMapper.Map<ReadCommentWebModel>(readCommentServiceModel);
+
+ return new OkObjectResult(readCommentWebModel);
+ }
+
+ [HttpPut]
+ public async Task<IActionResult> UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization)
+ {
+ if (!await this._commentService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization))
+ return new UnauthorizedResult();
+
+ UpdateCommentServiceModel updateCommentServiceModel =
+ this._commentMapper.Map<UpdateCommentServiceModel>(updateCommentWebModel);
+ updateCommentServiceModel.CreatorId = userId;
+
+ Guid id = await this._commentService.UpdateComment(updateCommentServiceModel);
+
+ return id == Guid.Empty ?
+ new BadRequestObjectResult("Unable to update comment!") :
+ new OkObjectResult(new { Id = id });
+ }
+
+ [HttpDelete]
+ public async Task<IActionResult> DeleteComment(Guid id, [FromHeader] string authorization)
+ {
+ if (!await this._commentService.ValidateJwtForComment(id, authorization))
+ return new UnauthorizedResult();
+
+ return await this._commentService.DeleteComment(id) ?
+ new OkResult() :
+ new BadRequestObjectResult("Could not delete Comment");
+ }
+
+ }
+}
+