using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using RMuseum.Models.Auth.Memory; using RMuseum.Models.Divan.ViewModels; using RMuseum.Services; using RSecurityBackend.Models.Generic; using System; using System.Linq; using System.Net; using System.Threading.Tasks; namespace RMuseum.Controllers { [Produces("application/json")] [Route("api/poetspecs")] public class DivanPoetSuggestedSpecLinesController : Controller { /// /// returns list of suggested spec lines for a poet /// /// poet id /// [HttpGet("poet/{id}")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(DivanPoetSuggestedSpecLineViewModel[]))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoetSuggestedSpecLinesAsync(int id) { var res = await _divanService.GetPoetSuggestedSpecLinesAsync(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// returns specific suggested line for poets /// /// /// [HttpGet("{id}")] [Authorize(Policy = RMuseumSecurableItem.DivanEntityShortName + ":" + RMuseumSecurableItem.ModeratePoetPhotos)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(DivanPoetSuggestedSpecLineViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoetSuggestedSpecLineAsync(int id) { var res = await _divanService.GetPoetSuggestedSpecLineAsync(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// return the next unpublished suggested line for poets /// /// /// [HttpGet("unpublished/next")] [Authorize(Policy = RMuseumSecurableItem.DivanEntityShortName + ":" + RMuseumSecurableItem.ModeratePoetPhotos)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(DivanPoetSuggestedSpecLineViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetNextUnmoderatedPoetSuggestedSpecLineAsync(int skip) { var res = await _divanService.GetNextUnmoderatedPoetSuggestedSpecLineAsync(skip); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); var resCount = await _divanService.GetNextUnmoderatedPoetSuggestedSpecLinesCountAsync(); if (!string.IsNullOrEmpty(resCount.ExceptionString)) return BadRequest(resCount.ExceptionString); // Paging Header HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject( new PaginationMetadata() { totalCount = resCount.Result, pageSize = -1, currentPage = -1, hasNextPage = false, hasPreviousPage = false, totalPages = -1 }) ); return Ok(res.Result); } /// /// add a suggestion for poets spec lines /// /// /// [HttpPost] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(DivanPoetSuggestedSpecLineViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task AddPoetSuggestedSpecLinesAsync([FromBody] DivanPoetSuggestedSpecLineViewModel spec) { spec.SuggestedById = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); var res = await _divanService.AddPoetSuggestedSpecLinesAsync(spec); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// modify a suggestion for poets spec lines /// /// /// [HttpPut] [Authorize(Policy = RMuseumSecurableItem.DivanEntityShortName + ":" + RMuseumSecurableItem.ModeratePoetPhotos)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task ModifyPoetSuggestedSpecLinesAsync([FromBody] DivanPoetSuggestedSpecLineViewModel spec) { var res = await _divanService.ModifyPoetSuggestedSpecLinesAsync(spec); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// reject a suggestion for poets spec lines /// /// /// /// [HttpPut("reject/{id}")] [Authorize(Policy = RMuseumSecurableItem.DivanEntityShortName + ":" + RMuseumSecurableItem.ModeratePoetPhotos)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task RejectPoetSuggestedSpecLinesAsync(int id, [FromBody] string rejectionCause) { Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); var res = await _divanService.RejectPoetSuggestedSpecLinesAsync(id, userId, rejectionCause); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// delete published suggested spec line /// /// /// [HttpDelete("{id}")] [Authorize(Policy = RMuseumSecurableItem.DivanEntityShortName + ":" + RMuseumSecurableItem.ModeratePoetPhotos)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task DeletePoetSuggestedSpecLinesAsync(int id) { var res = await _divanService.DeletePoetSuggestedSpecLinesAsync(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// Divan Service /// protected readonly IDivanService _divanService; /// /// constructor /// /// public DivanPoetSuggestedSpecLinesController(IDivanService divanService) { _divanService = divanService; } } }