using PublicAPI.Models;
using Backend.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using System.Management;
namespace PublicAPI.Controllers {
[Authorize]
[ApiController]
[Route("api/[controller]")]
/**
* Misc controller (miscellaneous methods)
@module MiscController
*/
public class MiscController : Controller {
private readonly AppOptions _options;
private readonly Db _db;
private readonly IDistributedCache _cache;
private readonly ILogger<HomeController> _logger;
private readonly CurrentUserInfo _currentUserInfo;
private readonly Misc _misc;
#if DEBUG
const string imageDir = "C:/AI4ASoilHealth/uploads/";
#else
const string imageDir = "/uploads/";
#endif
/**
* Constructor
*/
public MiscController(IOptions<AppOptions> options, Db db, IDistributedCache cache, ILogger<HomeController> logger, CurrentUserInfo currentUserInfo, Misc misc) {
this._options = options.Value;
this._db = db;
this._cache = cache;
this._logger = logger;
this._currentUserInfo = currentUserInfo;
this._misc = misc;
}
[HttpPost("Translate")]
public async Task<ActionResult> Translate([FromBody]TranslateParams values) {
string taskId = Guid.NewGuid().ToString();
//int[] keys = JsonConvert.DeserializeObject<int[]>(pars["keys"].ToString());
Task.Run(() => _misc.Translate(values.keys, values.targetLang, values.tableName, values.parentKeyName, values.fieldName, taskId));
return Ok(new { taskId = taskId, count = values.keys.Length });
}
[HttpGet("TranslateSingle")]
public async Task<ActionResult> TranslateSingle(string sourceLang, string targetLang, string value) {
var ret = await _misc.TranslateString(sourceLang, targetLang, value);
return Ok(ret);
}
}
public class TranslateParams {
public int targetLang { get; set; }
public string tableName { get; set; }
public string parentKeyName { get; set; }
public string fieldName { get; set; }
public int[] keys { get; set; }
}
}