using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using RMuseum.Models.Auth.Memory; using RMuseum.Models.Ganjoor; using RMuseum.Models.Ganjoor.ViewModels; using RMuseum.Services; using System; using System.Linq; using System.Net; using System.Threading.Tasks; namespace RMuseum.Controllers { [Produces("application/json")] [Route("api/translations")] public class TranslationController : Controller { /// /// get all languages /// /// [HttpGet("languages")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorLanguage[]))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetLanguagesAsync() { var res = await _translationService.GetLanguagesAsync(); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// get language by id /// /// /// [HttpGet("languages/{id}")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorLanguage))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetLanguageAsync(int id) { var res = await _translationService.GetLanguageAsync(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// add new language /// /// /// [HttpPost("languages")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Translations)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorLanguage))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task AddLanguageAsync([FromBody] GanjoorLanguage lang) { var res = await _translationService.AddLanguageAsync(lang); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// update an existing language /// /// /// [HttpPut("languages")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Translations)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task UpdateLangaugeAsync([FromBody] GanjoorLanguage lang) { var res = await _translationService.UpdateLangaugeAsync(lang); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } [HttpDelete("languages/{id}")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Translations)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task DeleteLangaugeAsync(int id) { var res = await _translationService.DeleteLangaugeAsync(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// get published poem translations /// /// /// [HttpGet("poem/{id}/published")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemTranslationViewModel[]))] [ProducesResponseType((int)HttpStatusCode.NotFound)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPublishedTranslationsAsync(int id) { var res = await _translationService.GetPoemTranslationsAsync(-1, id, true, false); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// get published poem translation in a specific language /// /// /// /// [HttpGet("poem/{id}/language/{lang}/published")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemTranslationViewModel))] [ProducesResponseType((int)HttpStatusCode.NotFound)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPublishedTranslationAsync(int id, int lang) { var res = await _translationService.GetPoemTranslationsAsync(lang, id, true, false); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// get all translations of a poem in a specific language /// /// /// send -1 for all languages /// [HttpGet("poem/{id}/language/{lang}")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Translations)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemTranslationViewModel[]))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetPoemTranslationsAsync(int id, int lang) { var res = await _translationService.GetPoemTranslationsAsync(lang, id, false, true); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// last language user has contributed in its translation /// /// [HttpGet("user/lastlanguage")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Translations)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorLanguage))] [ProducesResponseType((int)HttpStatusCode.NotFound)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetLastUserContributedLanguage() { Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); var res = await _translationService.GetLastUserContributedLanguage(userId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); if (res.Result == null) return NotFound(); return Ok(res.Result); } /// /// add translation for a poem (please specify verses VORder, Id is not processed) /// /// /// [HttpPost] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Translations)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task AddOrUpdatePoemTranslation([FromBody] GanjoorPoemTranslationViewModel translation) { Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); var res = await _translationService.AddPoemTranslation(userId, translation); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// translation service /// private readonly IGanjoorTranslationService _translationService; /// /// constructor /// /// public TranslationController(IGanjoorTranslationService translationService) { _translationService = translationService; } } }