using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using RMuseum.Models.Auth.Memory; using RMuseum.Models.Ganjoor; using RMuseum.Models.Ganjoor.ViewModels; using RMuseum.Models.GanjoorAudio.ViewModels; using RMuseum.Models.GanjoorIntegration.ViewModels; using RMuseum.Services; using RSecurityBackend.Models.Generic; using RSecurityBackend.Services; using System; using System.Linq; using System.Net; using System.Threading.Tasks; namespace RMuseum.Controllers { [Produces("application/json")] [Route("api/ganjoor")] public class GanjoorController : Controller { /// /// get list of poets /// /// /// /// [HttpGet] [Route("poets")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetViewModel[]))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoets(bool websitePoets = true, bool includeBio = true) { RServiceResult res = await _ganjoorService.GetPoets(websitePoets, includeBio); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// poet by id /// /// /// [HttpGet] [Route("poet/{id}")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetCompleteViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetPoetById(int id) { RServiceResult res = await _ganjoorService.GetPoetById(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); return Ok(res.Result); } /// /// poet by url /// /// /// [HttpGet] [Route("poet")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetCompleteViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetPoetByUrl(string url) { RServiceResult res = await _ganjoorService.GetPoetByUrl(url); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); return Ok(res.Result); } /// /// cat by id /// /// /// /// [HttpGet] [Route("cat/{id}")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetCompleteViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetCatById(int id, bool poems = true) { RServiceResult res = await _ganjoorService.GetCatById(id, poems); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); return Ok(res.Result); } /// /// cat by url /// /// /// /// [HttpGet] [Route("cat")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetCompleteViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetCatByUrl(string url, bool poems = true) { RServiceResult res = await _ganjoorService.GetCatByUrl(url, poems); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); return Ok(res.Result); } /// /// page by url /// /// /// /// [HttpGet] [Route("page")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPageCompleteViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetPageByUrl(string url, bool catPoems = false) { RServiceResult res = await _ganjoorService.GetPageByUrl(url, catPoems); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); return Ok(res.Result); } /// /// get poem by id /// /// /// /// /// /// /// /// /// not implemented yet /// /// next/previous /// [HttpGet] [Route("poem/{id}")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemCompleteViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetPoemById(int id, bool catInfo = true, bool catPoems = false, bool rhymes = true, bool recitations = true, bool images = true, bool songs = true, bool comments = true, bool verseDetails = true, bool navigation = true) { RServiceResult res = await _ganjoorService.GetPoemById(id, catInfo, catPoems, rhymes, recitations, images, songs, comments, verseDetails, navigation); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); return Ok(res.Result); } /// /// get poem by url /// /// /// /// /// not implemented yet /// /// /// not implemented yet /// not implemented yet /// /// next/previous /// [HttpGet] [Route("poem")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemCompleteViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetPoemByUrl(string url, bool catInfo = true, bool catPoems = false, bool rhymes = true, bool recitations = true, bool images = true, bool songs = true, bool comments = true, bool verseDetails = true, bool navigation = true) { RServiceResult res = await _ganjoorService.GetPoemByUrl(url, catInfo, catPoems, rhymes, recitations, images, songs, comments, verseDetails, navigation); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); return Ok(res.Result); } /// /// get poem recitations (PlainText/HtmlText are intentionally empty) /// /// /// [HttpGet] [Route("poem/{id}/recitations")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PublicRecitationViewModel[]))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoemRecitations(int id) { RServiceResult res = await _ganjoorService.GetPoemRecitations(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// get poem images (PlainText/HtmlText are intentionally empty) /// /// /// [HttpGet] [Route("poem/{id}/images")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PoemRelatedImage[]))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoemImages(int id) { RServiceResult res = await _ganjoorService.GetPoemImages(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// get poem songs /// /// /// /// /// [HttpGet] [Route("poem/{id}/songs")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PoemMusicTrackViewModel[]))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoemSongs(int id, bool approved, PoemMusicTrackType trackType = PoemMusicTrackType.All) { RServiceResult res = await _ganjoorService.GetPoemSongs(id, approved, trackType); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// suggest song for poem /// /// /// [HttpPost] [Route("song")] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PoemMusicTrackViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Forbidden)] public async Task SuggestSong([FromBody] PoemMusicTrackViewModel song) { Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); Guid sessionId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "SessionId").Value); RServiceResult sessionCheckResult = await _appUserService.SessionExists(userId, sessionId); if (!string.IsNullOrEmpty(sessionCheckResult.ExceptionString)) { return StatusCode((int)HttpStatusCode.Forbidden); } var res = await _ganjoorService.SuggestSong(userId, song); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// get next unreviewed song /// /// /// /// [HttpGet] [Route("song")] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PoemMusicTrackViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Forbidden)] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetNextUnreviewedSong(int skip = 0, bool onlyMine = false) { Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); Guid sessionId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "SessionId").Value); RServiceResult sessionCheckResult = await _appUserService.SessionExists(userId, sessionId); if (!string.IsNullOrEmpty(sessionCheckResult.ExceptionString)) { return StatusCode((int)HttpStatusCode.Forbidden); } var res = await _ganjoorService.GetNextUnreviewedSong(skip, onlyMine ? userId : Guid.Empty); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); var resCount = await _ganjoorService.GetUnreviewedSongsCount(onlyMine ? userId : Guid.Empty); if (!string.IsNullOrEmpty(resCount.ExceptionString)) return BadRequest(resCount.ExceptionString); // Paging Header HttpContext.Response.Headers.Add("paging-headers", JsonConvert.SerializeObject( new PaginationMetadata() { totalCount = resCount.Result, pageSize = -1, currentPage = -1, hasNextPage = false, hasPreviousPage = false, totalPages = -1 }) ); return Ok(res.Result); } /// /// review song /// /// /// [HttpPut] [Route("song")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ReviewSongs)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PoemMusicTrackViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Forbidden)] public async Task ReviewSong([FromBody]PoemMusicTrackViewModel song) { var res = await _ganjoorService.ReviewSong(song); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// directly insert a poem related song /// /// /// [HttpPost] [Route("song/add")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.AddSongs)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PoemMusicTrackViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Forbidden)] public async Task DirectInsertSong([FromBody] PoemMusicTrackViewModel song) { var res = await _ganjoorService.DirectInsertSong(song); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// get a random poem from hafez /// /// [HttpGet] [Route("hafez/faal")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemCompleteViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task Faal() { RServiceResult res = await _ganjoorService.Faal(); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// imports data from ganjoor SQLite database /// /// [HttpPost] [Route("sqliteimport")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task ImportLocalSQLiteDb() { RServiceResult res = await _ganjoorService.ImportLocalSQLiteDb(); if (res.Result) return Ok(); return BadRequest(res.ExceptionString); } /// /// updates poems text and html /// /// [HttpPost] [Route("updatetext")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public IActionResult UpdatePoemsText() { RServiceResult res = _ganjoorService.UpdatePoemsText(); if (res.Result) return Ok(); return BadRequest(res.ExceptionString); } /// /// imports data from ganjoor MySql database /// /// [HttpPost] [Route("mysqlimport")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public IActionResult ImportFromMySql() { RServiceResult res = _ganjoorService.ImportFromMySql(); if (res.Result) return Ok(); return BadRequest(res.ExceptionString); } /// /// Ganjoor Service /// protected readonly IGanjoorService _ganjoorService; /// /// IAppUserService instance /// protected IAppUserService _appUserService; /// /// constructor /// /// /// public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService) { _ganjoorService = ganjoorService; _appUserService = appUserService; } } }