aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Web/Controllers/PostController.cs
diff options
context:
space:
mode:
authorKamen Mladenov <kamen.d.mladenov@protonmail.com>2021-04-09 19:51:35 +0300
committerGitHub <noreply@github.com>2021-04-09 19:51:35 +0300
commit233f38915ba0079079233eff55434ef349c05c45 (patch)
tree6c5f69017865bcab87355e910c87339453da1406 /src/DevHive.Web/Controllers/PostController.cs
parentf4a70c6430db923af9fa9958a11c2d6612cb52cc (diff)
parenta992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff)
downloadDevHive-main.tar
DevHive-main.tar.gz
DevHive-main.zip
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/DevHive.Web/Controllers/PostController.cs')
-rw-r--r--src/DevHive.Web/Controllers/PostController.cs89
1 files changed, 0 insertions, 89 deletions
diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs
deleted file mode 100644
index d3fdbf6..0000000
--- a/src/DevHive.Web/Controllers/PostController.cs
+++ /dev/null
@@ -1,89 +0,0 @@
-using System.Threading.Tasks;
-using Microsoft.AspNetCore.Mvc;
-using AutoMapper;
-using System;
-using DevHive.Web.Models.Post;
-using DevHive.Services.Models.Post;
-using Microsoft.AspNetCore.Authorization;
-using DevHive.Services.Interfaces;
-
-namespace DevHive.Web.Controllers
-{
- [ApiController]
- [Route("/api/[controller]")]
- [Authorize(Roles = "User,Admin")]
- public class PostController
- {
- private readonly IPostService _postService;
- private readonly IMapper _postMapper;
-
- public PostController(IPostService postService, IMapper postMapper)
- {
- this._postService = postService;
- this._postMapper = postMapper;
- }
-
- #region Create
- [HttpPost]
- public async Task<IActionResult> Create(Guid userId, [FromForm] CreatePostWebModel createPostWebModel, [FromHeader] string authorization)
- {
- if (!await this._postService.ValidateJwtForCreating(userId, authorization))
- return new UnauthorizedResult();
-
- CreatePostServiceModel createPostServiceModel =
- this._postMapper.Map<CreatePostServiceModel>(createPostWebModel);
- createPostServiceModel.CreatorId = userId;
-
- Guid id = await this._postService.CreatePost(createPostServiceModel);
-
- return id == Guid.Empty ?
- new BadRequestObjectResult("Could not create post!") :
- new OkObjectResult(new { Id = id });
- }
- #endregion
-
- #region Read
- [HttpGet]
- [AllowAnonymous]
- public async Task<IActionResult> GetById(Guid id)
- {
- ReadPostServiceModel postServiceModel = await this._postService.GetPostById(id);
- ReadPostWebModel postWebModel = this._postMapper.Map<ReadPostWebModel>(postServiceModel);
-
- return new OkObjectResult(postWebModel);
- }
- #endregion
-
- #region Update
- [HttpPut]
- public async Task<IActionResult> Update(Guid userId, [FromForm] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization)
- {
- if (!await this._postService.ValidateJwtForPost(updatePostWebModel.PostId, authorization))
- return new UnauthorizedResult();
-
- UpdatePostServiceModel updatePostServiceModel =
- this._postMapper.Map<UpdatePostServiceModel>(updatePostWebModel);
- updatePostServiceModel.CreatorId = userId;
-
- Guid id = await this._postService.UpdatePost(updatePostServiceModel);
-
- return id == Guid.Empty ?
- new BadRequestObjectResult("Could not update post!") :
- new OkObjectResult(new { Id = id });
- }
- #endregion
-
- #region Delete
- [HttpDelete]
- public async Task<IActionResult> Delete(Guid id, [FromHeader] string authorization)
- {
- if (!await this._postService.ValidateJwtForPost(id, authorization))
- return new UnauthorizedResult();
-
- return await this._postService.DeletePost(id) ?
- new OkResult() :
- new BadRequestObjectResult("Could not delete Post");
- }
- #endregion
- }
-}