SocialPub/PrivaPub/Controllers/ClientToServer/DataController.cs
2023-02-19 00:43:43 +01:00

55 lines
1.5 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PrivaPub.ClientModels;
using PrivaPub.ClientModels.Data;
using PrivaPub.Services.ClientToServer.Public;
namespace PrivaPub.Controllers.ClientToServer
{
[ApiController,
Route("clientapi/data")]
public class DataController : ControllerBase
{
readonly IDataService DataService;
readonly ILogger<DataController> Logger;
public DataController(IDataService dataService,
ILogger<DataController> logger)
{
DataService = dataService;
Logger = logger;
}
[HttpGet, Route("/clientapi/data/ping"), AllowAnonymous]
public async ValueTask<IActionResult> Ping() => NoContent();
[HttpGet, Route("/clientapi/data/current-version"), AllowAnonymous]
public IActionResult CurrentVersion() =>
Ok(DataService.GetCurrentVersion());
[HttpGet, Route("/clientapi/data/languages"), AllowAnonymous]
public async Task<IActionResult> Languages(CancellationToken cancellationToken)
{
var result = new WebResult();
try
{
var languages = await DataService.GetLanguages(cancellationToken);
var viewLanguages = new List<ViewLanguage>();
viewLanguages.AddRange(languages.Select(l => new ViewLanguage
{
Name = l.NativeName,
International2Code = l.International2Code
}).ToArray());
return Ok(viewLanguages);
}
catch (Exception ex)
{
Logger.LogError(ex, $"{nameof(DataController)}.{nameof(Languages)}()");
return BadRequest(result.Invalidate(ex.Message, exception: ex));
}
}
}
}