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;
using DevHive.Common.Jwt.Interfaces;
namespace DevHive.Web.Controllers
{
///
/// All endpoints for interacting with the comments layer
///
[ApiController]
[Route("/api/[controller]")]
[Authorize(Roles = "User,Admin")]
public class CommentController
{
private readonly ICommentService _commentService;
private readonly IMapper _commentMapper;
private readonly IJwtService _jwtService;
public CommentController(ICommentService commentService, IMapper commentMapper, IJwtService jwtService)
{
this._commentService = commentService;
this._commentMapper = commentMapper;
this._jwtService = jwtService;
}
///
/// Create a comment and attach it to a post
///
/// The useer's Id
/// The new comment's parametars
/// JWT Bearer token
/// The comment's Id
[HttpPost]
public async Task AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization)
{
if (!this._jwtService.ValidateToken(userId, authorization))
return new UnauthorizedResult();
if (!await this._commentService.ValidateJwtForCreating(userId, authorization))
return new UnauthorizedResult();
CreateCommentServiceModel createCommentServiceModel =
this._commentMapper.Map(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 });
}
///
/// Query comment's data by it's Id
///
/// The comment's Id
/// Full data model of the comment
[HttpGet]
[AllowAnonymous]
public async Task GetCommentById(Guid commentId)
{
ReadCommentServiceModel readCommentServiceModel = await this._commentService.GetCommentById(commentId);
ReadCommentWebModel readCommentWebModel = this._commentMapper.Map(readCommentServiceModel);
return new OkObjectResult(readCommentWebModel);
}
///
/// Update comment's parametars. Comment creator only!
///
/// The comment creator's Id
/// New comment's parametars
/// JWT Bearer token
/// Ok result
[HttpPut]
public async Task UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization)
{
if (!this._jwtService.ValidateToken(userId, authorization))
return new UnauthorizedResult();
UpdateCommentServiceModel updateCommentServiceModel =
this._commentMapper.Map(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 });
}
///
/// Delete a comment. Comment creator only!
///
/// Comment's Id
/// JWT Bearer token
/// Ok result
[HttpDelete]
public async Task DeleteComment(Guid commentId, [FromHeader] string authorization)
{
if (!await this._commentService.ValidateJwtForComment(commentId, authorization))
return new UnauthorizedResult();
return await this._commentService.DeleteComment(commentId) ?
new OkResult() :
new BadRequestObjectResult("Could not delete Comment");
}
}
}