divan/RMuseum/Controllers/TranslationController.cs
Hamid Reza Mohammadi 1f952b6ba1 removing unused codes
2025-05-04 12:08:17 +03:30

151 lines
6.1 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using RMuseum.Models.Auth.Memory;
using RMuseum.Models.Ganjoor;
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/translations")]
public class TranslationController : Controller
{
/// <summary>
/// get all languages
/// </summary>
/// <returns></returns>
[HttpGet("languages")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorLanguage[]))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> GetLanguagesAsync()
{
var res = await _translationService.GetLanguagesAsync();
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// get language by id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("languages/{id}")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorLanguage))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> GetLanguageAsync(int id)
{
var res = await _translationService.GetLanguageAsync(id);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// add new language
/// </summary>
/// <param name="lang"></param>
/// <returns></returns>
[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<IActionResult> AddLanguageAsync([FromBody] GanjoorLanguage lang)
{
if (ReadOnlyMode)
return BadRequest("سایت به دلایل فنی مثل انتقال سرور موقتاً در حالت فقط خواندنی قرار دارد. لطفاً ساعاتی دیگر مجدداً تلاش کنید.");
var res = await _translationService.AddLanguageAsync(lang);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// update an existing language
/// </summary>
/// <param name="lang"></param>
/// <returns></returns>
[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<IActionResult> UpdateLangaugeAsync([FromBody] GanjoorLanguage lang)
{
if (ReadOnlyMode)
return BadRequest("سایت به دلایل فنی مثل انتقال سرور موقتاً در حالت فقط خواندنی قرار دارد. لطفاً ساعاتی دیگر مجدداً تلاش کنید.");
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<IActionResult> DeleteLangaugeAsync(int id)
{
if (ReadOnlyMode)
return BadRequest("سایت به دلایل فنی مثل انتقال سرور موقتاً در حالت فقط خواندنی قرار دارد. لطفاً ساعاتی دیگر مجدداً تلاش کنید.");
var res = await _translationService.DeleteLangaugeAsync(id);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// readonly mode
/// </summary>
public bool ReadOnlyMode
{
get
{
try
{
return bool.Parse(Configuration["ReadOnlyMode"]);
}
catch
{
return false;
}
}
}
/// <summary>
/// translation service
/// </summary>
private readonly IGanjoorTranslationService _translationService;
/// <summary>
/// Configuration
/// </summary>
protected IConfiguration Configuration { get; }
/// <summary>
/// constructor
/// </summary>
/// <param name="translationService"></param>
/// <param name="configuration"></param>
public TranslationController(IGanjoorTranslationService translationService, IConfiguration configuration)
{
_translationService = translationService;
Configuration = configuration;
}
}
}