112 lines
4.2 KiB
C#

using System;
using System.Threading.Tasks;
using Asp.Versioning;
using AutoMapper;
using gehGassi.Core.Interfaces;
using gehGassi.Core.Services;
using gehGassi.Domain.Common;
using gehGassi.Dto;
using gehGassi.Dto.Common;
using gehGassi.Web.Helper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace gehGassi.Web.Controllers.Api
{
/// <summary>
/// Controller für die Verwaltung vom Server-Status
/// </summary>
[ApiController]
[ApiVersion(1)]
[Route("api/status")]
[Route("api/v{v:apiVersion}/status")]
public class ApiStatusController : ApiBaseController
{
private readonly IMapper _mapper;
private readonly ILogger<ApiStatusController> _logger;
private readonly IAppVersionService _appVersionService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="mapper">Instanz eines IMapper</param>
/// <param name="logger">Instanz eines ILogger</param>
/// <param name="localizationOptions">Instanz von LocalizationOptions</param>
/// <param name="appUserService">Instanz eines IDogOwnerService</param>
/// <param name="appVersionService">Instanz eines IAppVersionService</param>
public ApiStatusController(IMapper mapper, ILogger<ApiStatusController> logger, IOptions<LocalizationOptions> localizationOptions, IAppUserService appUserService, IAppVersionService appVersionService) : base(mapper, localizationOptions, appUserService)
{
_mapper = mapper;
_logger = logger;
_appVersionService = appVersionService;
}
/// <summary>
/// Gibt zurück, dass der Server online ist...
/// </summary>
/// <returns>200 OK</returns>
[HttpGet("IsOnline")]
public IActionResult IsOnline()
{
var clientOffset = GetClientDateOffset();
return Ok(true);
}
/// <summary>
/// Gibt die Server-Zeit zurück
/// </summary>
/// <returns>Server-Zeit</returns>
[HttpGet("ServerTime")]
public IActionResult ServerTime()
{
var clientOffset = GetClientDateOffset();
return Ok(DateTimeOffset.UtcNow);
}
/// <summary>
/// Gibt die aktuelle Version der App für eine Plattform zurück
/// </summary>
/// <returns>Appversion</returns>
[HttpGet("AppVersion")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public async Task<IActionResult> AppVersion(PlatformDto platform, string language)
{
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var currentVersion = await _appVersionService.GetCurrentwithNamesAsync((Platform)platform, language, LocalizationOptions.Value.DefaultCulture);
if (currentVersion != null)
{
var currentVersionDto = _mapper.Map<AppVersionDto>(currentVersion);
return Ok(currentVersionDto);
}
}
return NotFound(CommunicationErrors.Common_NotFound);
}
/// <summary>
/// Gibt die aktuelle Version der App für eine Plattform zurück
/// </summary>
/// <returns>Appversion</returns>
[HttpGet("CheckAppVersion")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public async Task<IActionResult> CheckAppVersion(PlatformDto platform, string language)
{
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
var currentVersion = await _appVersionService.GetCurrentwithNamesAsync((Platform)platform, language, LocalizationOptions.Value.DefaultCulture);
if (currentVersion != null)
{
var currentVersionDto = _mapper.Map<AppVersionCheckDto>(currentVersion);
return Ok(currentVersionDto);
}
}
return NotFound(CommunicationErrors.Common_NotFound);
}
}
}