aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Web/Controllers
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-01-31 12:58:44 +0200
committertranstrike <transtrike@gmail.com>2021-01-31 12:58:44 +0200
commit979a86a14cd658b5346279901ac8bca667c373d3 (patch)
tree9791cf02c0838a4d6392e3651f93eeed283acb57 /src/DevHive.Web/Controllers
parent9d5f4628a3a75871b47ac6a9f9c0419748d9dfb8 (diff)
parentb8743cfdd0515e4d07ea5c926be1d9ade5340a91 (diff)
downloadDevHive-979a86a14cd658b5346279901ac8bca667c373d3.tar
DevHive-979a86a14cd658b5346279901ac8bca667c373d3.tar.gz
DevHive-979a86a14cd658b5346279901ac8bca667c373d3.zip
Username added to JWT; Promotion to Admin fixed
Diffstat (limited to 'src/DevHive.Web/Controllers')
-rw-r--r--src/DevHive.Web/Controllers/CommentController.cs82
-rw-r--r--src/DevHive.Web/Controllers/FeedController.cs7
-rw-r--r--src/DevHive.Web/Controllers/PostController.cs67
3 files changed, 89 insertions, 67 deletions
diff --git a/src/DevHive.Web/Controllers/CommentController.cs b/src/DevHive.Web/Controllers/CommentController.cs
new file mode 100644
index 0000000..ebcb87a
--- /dev/null
+++ b/src/DevHive.Web/Controllers/CommentController.cs
@@ -0,0 +1,82 @@
+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");
+ }
+
+ }
+}
+
diff --git a/src/DevHive.Web/Controllers/FeedController.cs b/src/DevHive.Web/Controllers/FeedController.cs
index 4fd3ae9..2f14cf3 100644
--- a/src/DevHive.Web/Controllers/FeedController.cs
+++ b/src/DevHive.Web/Controllers/FeedController.cs
@@ -23,7 +23,7 @@ namespace DevHive.Web.Controllers
this._mapper = mapper;
}
- [HttpGet]
+ [HttpPost]
[Route("GetPosts")]
public async Task<IActionResult> GetPosts(Guid userId, [FromBody] GetPageWebModel getPageWebModel)
{
@@ -36,14 +36,15 @@ namespace DevHive.Web.Controllers
return new OkObjectResult(readPageWebModel);
}
- [HttpGet]
+ [HttpPost]
[Route("GetUserPosts")]
+ [AllowAnonymous]
public async Task<IActionResult> GetUserPosts(string username, [FromBody] GetPageWebModel getPageWebModel)
{
GetPageServiceModel getPageServiceModel = this._mapper.Map<GetPageServiceModel>(getPageWebModel);
getPageServiceModel.Username = username;
- ReadPageServiceModel readPageServiceModel = await this._feedService.GetPage(getPageServiceModel);
+ ReadPageServiceModel readPageServiceModel = await this._feedService.GetUserPage(getPageServiceModel);
ReadPageWebModel readPageWebModel = this._mapper.Map<ReadPageWebModel>(readPageServiceModel);
return new OkObjectResult(readPageWebModel);
diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs
index fe71519..53adfce 100644
--- a/src/DevHive.Web/Controllers/PostController.cs
+++ b/src/DevHive.Web/Controllers/PostController.cs
@@ -2,16 +2,14 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using System;
-using DevHive.Web.Models.Post.Post;
-using DevHive.Services.Models.Post.Post;
-using DevHive.Web.Models.Post.Comment;
-using DevHive.Services.Models.Post.Comment;
+using DevHive.Web.Models.Post;
+using DevHive.Services.Models.Post;
using Microsoft.AspNetCore.Authorization;
using DevHive.Services.Interfaces;
namespace DevHive.Web.Controllers
{
- [ApiController]
+ [ApiController]
[Route("/api/[controller]")]
[Authorize(Roles = "User,Admin")]
public class PostController
@@ -42,24 +40,6 @@ namespace DevHive.Web.Controllers
new BadRequestObjectResult("Could not create post!") :
new OkObjectResult(new { Id = id });
}
-
- [HttpPost]
- [Route("Comment")]
- public async Task<IActionResult> AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization)
- {
- if (!await this._postService.ValidateJwtForCreating(userId, authorization))
- return new UnauthorizedResult();
-
- CreateCommentServiceModel createCommentServiceModel =
- this._postMapper.Map<CreateCommentServiceModel>(createCommentWebModel);
- createCommentServiceModel.CreatorId = userId;
-
- Guid id = await this._postService.AddComment(createCommentServiceModel);
-
- return id == Guid.Empty ?
- new BadRequestObjectResult("Could not create comment!") :
- new OkObjectResult(new { Id = id });
- }
#endregion
#region Read
@@ -72,17 +52,6 @@ namespace DevHive.Web.Controllers
return new OkObjectResult(postWebModel);
}
-
- [HttpGet]
- [Route("Comment")]
- [AllowAnonymous]
- public async Task<IActionResult> GetCommentById(Guid id)
- {
- ReadCommentServiceModel readCommentServiceModel = await this._postService.GetCommentById(id);
- ReadCommentWebModel readCommentWebModel = this._postMapper.Map<ReadCommentWebModel>(readCommentServiceModel);
-
- return new OkObjectResult(readCommentWebModel);
- }
#endregion
#region Update
@@ -102,24 +71,6 @@ namespace DevHive.Web.Controllers
new BadRequestObjectResult("Unable to update post!") :
new OkObjectResult(new { Id = id });
}
-
- [HttpPut]
- [Route("Comment")]
- public async Task<IActionResult> UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization)
- {
- if (!await this._postService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization))
- return new UnauthorizedResult();
-
- UpdateCommentServiceModel updateCommentServiceModel =
- this._postMapper.Map<UpdateCommentServiceModel>(updateCommentWebModel);
- updateCommentServiceModel.CreatorId = userId;
-
- Guid id = await this._postService.UpdateComment(updateCommentServiceModel);
-
- return id == Guid.Empty ?
- new BadRequestObjectResult("Unable to update comment!") :
- new OkObjectResult(new { Id = id });
- }
#endregion
#region Delete
@@ -133,18 +84,6 @@ namespace DevHive.Web.Controllers
new OkResult() :
new BadRequestObjectResult("Could not delete Comment");
}
-
- [HttpDelete]
- [Route("Comment")]
- public async Task<IActionResult> DeleteComment(Guid id, [FromHeader] string authorization)
- {
- if (!await this._postService.ValidateJwtForComment(id, authorization))
- return new UnauthorizedResult();
-
- return await this._postService.DeleteComment(id) ?
- new OkResult() :
- new BadRequestObjectResult("Could not delete Comment");
- }
#endregion
}
}