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; using DevHive.Common.Jwt.Interfaces; namespace DevHive.Web.Controllers { /// /// All endpoints for interacting with the post layer /// [ApiController] [Route("/api/[controller]")] [Authorize(Roles = "User,Admin")] public class PostController { private readonly IPostService _postService; private readonly IMapper _postMapper; private readonly IJwtService _jwtService; public PostController(IPostService postService, IMapper postMapper, IJwtService jwtService) { this._postService = postService; this._postMapper = postMapper; this._jwtService = jwtService; } #region Create /// /// Create a new post /// /// The user's Id /// The new post's data /// JWT Bearer token /// New post's Id [HttpPost] public async Task Create(Guid userId, [FromForm] CreatePostWebModel createPostWebModel, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); CreatePostServiceModel createPostServiceModel = this._postMapper.Map(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 /// /// Query full post's data by it's Id /// /// The post's Id /// Full data model of the post [HttpGet] [AllowAnonymous] public async Task GetById(Guid id) { ReadPostServiceModel postServiceModel = await this._postService.GetPostById(id); ReadPostWebModel postWebModel = this._postMapper.Map(postServiceModel); return new OkObjectResult(postWebModel); } #endregion #region Update /// /// Update post's data. Creator only! /// /// The post creator's Id /// The new params of the post /// JWT Bearer token /// The post's Id [HttpPut] public async Task Update(Guid userId, [FromForm] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); if (!await this._postService.ValidateJwtForPost(updatePostWebModel.PostId, authorization)) return new UnauthorizedResult(); UpdatePostServiceModel updatePostServiceModel = this._postMapper.Map(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 /// /// Delete a post. Creator only! /// /// Post's Id /// JWT Bearer token /// Ok result [HttpDelete] public async Task Delete(Guid postId, [FromHeader] string authorization) { if (!await this._postService.ValidateJwtForPost(postId, authorization)) return new UnauthorizedResult(); return await this._postService.DeletePost(postId) ? new OkResult() : new BadRequestObjectResult("Could not delete Post"); } #endregion } }