using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using RMuseum.Models.Auth.Memory; using RMuseum.Models.Ganjoor.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 GanjoorPoetSuggestedSpecLinesController : Controller { /// /// returns list of suggested spec lines for a poet /// /// poet id /// [HttpGet("poet/{id}")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetSuggestedSpecLineViewModel[]))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoetSuggestedSpecLinesAsync(int id) { var res = await _ganjoorService.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.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ModeratePoetPhotos)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetSuggestedSpecLineViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoetSuggestedSpecLineAsync(int id) { var res = await _ganjoorService.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.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ModeratePoetPhotos)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetSuggestedSpecLineViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetNextUnmoderatedPoetSuggestedSpecLineAsync(int skip) { var res = await _ganjoorService.GetNextUnmoderatedPoetSuggestedSpecLineAsync(skip); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); var resCount = await _ganjoorService.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(GanjoorPoetSuggestedSpecLineViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task AddPoetSuggestedSpecLinesAsync([FromBody] GanjoorPoetSuggestedSpecLineViewModel spec) { spec.SuggestedById = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); var res = await _ganjoorService.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.GanjoorEntityShortName + ":" + 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] GanjoorPoetSuggestedSpecLineViewModel spec) { var res = await _ganjoorService.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.GanjoorEntityShortName + ":" + 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 _ganjoorService.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.GanjoorEntityShortName + ":" + 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 _ganjoorService.DeletePoetSuggestedSpecLinesAsync(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// Ganjoor Service /// protected readonly IGanjoorService _ganjoorService; /// /// constructor /// /// public GanjoorPoetSuggestedSpecLinesController(IGanjoorService ganjoorService) { _ganjoorService = ganjoorService; } } }