361 lines
13 KiB
C#
361 lines
13 KiB
C#
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using AyaNova.Models;
|
|
using AyaNova.Api.ControllerHelpers;
|
|
using AyaNova.Biz;
|
|
using System;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace AyaNova.Api.Controllers
|
|
{
|
|
//DOCUMENTATING THE API
|
|
//https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/recommended-tags-for-documentation-comments
|
|
//https://github.com/domaindrivendev/Swashbuckle.AspNetCore#include-descriptions-from-xml-comments
|
|
|
|
/// <summary>
|
|
/// Translation controller
|
|
/// </summary>
|
|
[ApiController]
|
|
[ApiVersion("8.0")]
|
|
[Route("api/v{version:apiVersion}/translation")]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
public class TranslationController : ControllerBase
|
|
{
|
|
private readonly AyContext ct;
|
|
private readonly ILogger<TranslationController> log;
|
|
private readonly ApiServerState serverState;
|
|
|
|
|
|
/// <summary>
|
|
/// ctor
|
|
/// </summary>
|
|
/// <param name="dbcontext"></param>
|
|
/// <param name="logger"></param>
|
|
/// <param name="apiServerState"></param>
|
|
public TranslationController(AyContext dbcontext, ILogger<TranslationController> logger, ApiServerState apiServerState)
|
|
{
|
|
ct = dbcontext;
|
|
log = logger;
|
|
serverState = apiServerState;
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Get Translation all values
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns>A single Translation and it's values</returns>
|
|
[HttpGet("{id}")]
|
|
public async Task<IActionResult> GetTranslation([FromRoute] long id)
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
}
|
|
|
|
//Instantiate the business object handler
|
|
TranslationBiz biz = TranslationBiz.GetBiz(ct, HttpContext);
|
|
|
|
var o = await biz.GetAsync(id);
|
|
|
|
if (o == null)
|
|
{
|
|
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
}
|
|
|
|
return Ok(ApiOkResponse.Response(o));
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Put (update) Translation
|
|
///
|
|
/// </summary>
|
|
/// <param name="updatedObject"></param>
|
|
/// <returns></returns>
|
|
[HttpPut]
|
|
public async Task<IActionResult> PutTranslation([FromBody] Translation updatedObject)
|
|
{
|
|
if (!serverState.IsOpen)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
TranslationBiz biz = TranslationBiz.GetBiz(ct, HttpContext);
|
|
if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
var o = await biz.PutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token
|
|
if (o == null)
|
|
{
|
|
if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
|
|
return StatusCode(409, new ApiErrorResponse(biz.Errors));
|
|
else
|
|
return BadRequest(new ApiErrorResponse(biz.Errors));
|
|
}
|
|
return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency })); ;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Get Translations list
|
|
/// </summary>
|
|
/// <returns>List in alphabetical order of all Translations</returns>
|
|
[HttpGet("list")]
|
|
public async Task<IActionResult> TranslationList()
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
//Instantiate the business object handler
|
|
TranslationBiz biz = TranslationBiz.GetBiz(ct, HttpContext);
|
|
|
|
var l = await biz.GetTranslationListAsync();
|
|
return Ok(ApiOkResponse.Response(l));
|
|
}
|
|
|
|
|
|
#if (DEBUG)
|
|
/// <summary>
|
|
/// Get a coverage report of translation keys used versus unused
|
|
/// </summary>
|
|
/// <returns>Report of all unique translation keys requested since last server reboot</returns>
|
|
[HttpGet("translationkeycoverage")]
|
|
public async Task<IActionResult> TranslationKeyCoverage()
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
//Instantiate the business object handler
|
|
TranslationBiz biz = TranslationBiz.GetBiz(ct, HttpContext);
|
|
|
|
var l = await biz.TranslationKeyCoverageAsync();
|
|
return Ok(ApiOkResponse.Response(l));
|
|
}
|
|
#endif
|
|
|
|
|
|
/// <summary>
|
|
/// Get subset of translation values
|
|
/// </summary>
|
|
/// <param name="inObj">List of translation key strings</param>
|
|
/// <returns>A key value array of translation text values</returns>
|
|
[HttpPost("subset")]
|
|
public async Task<IActionResult> SubSet([FromBody] List<string> inObj)
|
|
{
|
|
if (serverState.IsClosed)
|
|
{
|
|
//Exception for SuperUser account to handle licensing issues
|
|
if (UserIdFromContext.Id(HttpContext.Items) != 1)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
}
|
|
|
|
//Instantiate the business object handler
|
|
|
|
//Instantiate the business object handler
|
|
TranslationBiz biz = TranslationBiz.GetBiz(ct, HttpContext);
|
|
|
|
var l = await biz.GetSubsetAsync(inObj);
|
|
return Ok(ApiOkResponse.Response(l));
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Duplicate
|
|
/// </summary>
|
|
/// <param name="id">Source object id</param>
|
|
/// <param name="apiVersion">From route path</param>
|
|
/// <returns>Duplicate</returns>
|
|
[HttpPost("duplicate/{id}")]
|
|
public async Task<IActionResult> DuplicateTranslation([FromRoute] long id, ApiVersion apiVersion)
|
|
{
|
|
if (!serverState.IsOpen)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
TranslationBiz biz = TranslationBiz.GetBiz(ct, HttpContext);
|
|
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
Translation o = await biz.DuplicateAsync(id);
|
|
if (o == null)
|
|
return BadRequest(new ApiErrorResponse(biz.Errors));
|
|
else
|
|
return CreatedAtAction(nameof(TranslationController.GetTranslation), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Delete Translation
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns>Ok</returns>
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteTranslation([FromRoute] long id)
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
}
|
|
|
|
|
|
//Fetch translation and it's children
|
|
//(fetch here so can return proper REST responses on failing basic validity)
|
|
var dbObj = await ct.Translation.Include(z => z.TranslationItems).SingleOrDefaultAsync(z => z.Id == id);
|
|
if (dbObj == null)
|
|
{
|
|
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
}
|
|
|
|
if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.Translation))
|
|
{
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
}
|
|
|
|
|
|
//Instantiate the business object handler
|
|
TranslationBiz biz = TranslationBiz.GetBiz(ct, HttpContext);
|
|
if (!await biz.DeleteAsync(dbObj))
|
|
{
|
|
return BadRequest(new ApiErrorResponse(biz.Errors));
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Get Translation all values
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="t">download token</param>
|
|
/// <returns>A single Translation and it's values</returns>
|
|
[AllowAnonymous]
|
|
[HttpGet("download/{id}")]
|
|
public async Task<IActionResult> DownloadTranslation([FromRoute] long id, [FromQuery] string t)
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
}
|
|
|
|
|
|
int nFailedAuthDelay = 3000;
|
|
|
|
if (string.IsNullOrWhiteSpace(t))
|
|
{
|
|
await Task.Delay(nFailedAuthDelay);//DOS protection
|
|
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
|
|
}
|
|
var DownloadUser = await ct.User.AsNoTracking().SingleOrDefaultAsync(z => z.DlKey == t && z.Active == true);
|
|
if (DownloadUser == null)
|
|
{
|
|
await Task.Delay(nFailedAuthDelay);//DOS protection
|
|
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
|
|
}
|
|
var utcNow = new DateTimeOffset(DateTime.Now.ToUniversalTime(), TimeSpan.Zero);
|
|
if (DownloadUser.DlKeyExpire < utcNow.DateTime)
|
|
{
|
|
await Task.Delay(nFailedAuthDelay);//DOS protection
|
|
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
|
|
}
|
|
|
|
|
|
var o = await ct.Translation.Include(z => z.TranslationItems).SingleOrDefaultAsync(z => z.Id == id);
|
|
|
|
//turn into correct format and then send as file
|
|
if (o == null)
|
|
{
|
|
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
}
|
|
var asText = Newtonsoft.Json.JsonConvert.SerializeObject(
|
|
o,
|
|
Newtonsoft.Json.Formatting.None,
|
|
new JsonSerializerSettings { ContractResolver = new ShouldSerializeContractResolver(new string[] { "Concurrency", "Id","TranslationId" }) });
|
|
var bytes = System.Text.Encoding.UTF8.GetBytes(asText);
|
|
var file = new FileContentResult(bytes, "application/octet-stream");
|
|
file.FileDownloadName = Util.FileUtil.StringToSafeFileName(o.Name) + ".json";
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
public class ShouldSerializeContractResolver : DefaultContractResolver
|
|
{
|
|
private readonly IEnumerable<string> _excludePropertyNames;
|
|
|
|
public ShouldSerializeContractResolver(IEnumerable<string> excludePropertyNames)
|
|
{
|
|
_excludePropertyNames = excludePropertyNames;
|
|
}
|
|
|
|
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
|
{
|
|
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
|
|
|
|
// only serializer properties that start with the specified character
|
|
properties =
|
|
properties.Where(p => !_excludePropertyNames.Any(p2 => p2 == p.PropertyName)).ToList();
|
|
|
|
return properties;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if (DEBUG)
|
|
public class TranslationCoverageInfo
|
|
{
|
|
public List<string> RequestedKeys { get; set; }
|
|
public int RequestedKeyCount { get; set; }
|
|
public List<string> NotRequestedKeys { get; set; }
|
|
public int NotRequestedKeyCount { get; set; }
|
|
|
|
public TranslationCoverageInfo()
|
|
{
|
|
RequestedKeys = new List<string>();
|
|
NotRequestedKeys = new List<string>();
|
|
}
|
|
|
|
}
|
|
#endif
|
|
|
|
}
|
|
} |