Refaktorisierung der Lokalisierung und DTO-Integration

- Ersetzung von ITranslateService durch IStringLocalizer<X> für verbesserte Lokalisierung.
- Aktualisierung der DTO-Klassen entsprechend der neuesten Core.DTO-Struktur.
- Integration der neuen Klassen Result und DataResult aus Core.DTO für standardisierte Serviceantworten.
This commit is contained in:
Developer 02
2024-05-02 17:36:23 +02:00
parent a7cf500858
commit cc01f57125
32 changed files with 571 additions and 149 deletions

View File

@@ -8,11 +8,10 @@ using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using DigitalData.Core.API;
using DigitalData.Core.Application;
using EnvelopeGenerator.Application;
using DigitalData.Core.Contracts.CultureServices;
using DigitalData.Core.CultureServices;
using Azure;
using Microsoft.Extensions.Localization;
using DigitalData.Core.DTO;
using EnvelopeGenerator.Application.Resources;
namespace EnvelopeGenerator.Web.Controllers
{
@@ -22,15 +21,15 @@ namespace EnvelopeGenerator.Web.Controllers
private readonly IEnvelopeReceiverService _envRcvService;
private readonly IEnvelopeService _envelopeService;
private readonly IEnvelopeHistoryService _historyService;
private readonly IKeyTranslationService _translator;
private readonly IStringLocalizer<Resource> _localizer;
public HomeController(DatabaseService databaseService, EnvelopeOldService envelopeOldService, ILogger<HomeController> logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeService envelopeService, IEnvelopeHistoryService historyService, IKeyTranslationService keyTranslationService) : base(databaseService, logger)
public HomeController(DatabaseService databaseService, EnvelopeOldService envelopeOldService, ILogger<HomeController> logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeService envelopeService, IEnvelopeHistoryService historyService, IStringLocalizer<Resource> localizer) : base(databaseService, logger)
{
this.envelopeOldService = envelopeOldService;
_envRcvService = envelopeReceiverService;
_envelopeService = envelopeService;
_historyService = historyService;
_translator = keyTranslationService;
_localizer = localizer;
}
[HttpGet("/EnvelopeKey/{envelopeReceiverId}")]
@@ -47,7 +46,7 @@ namespace EnvelopeGenerator.Web.Controllers
if (erResult is null)
{
_logger.LogError(MessageKey.ServiceOutputNullError.TranslateWith(_translator));
_logger.LogError(_localizer[MessageKey.ServiceOutputNullError.ToString()]);
return this.ViewEnvelopeNotFound();
}
else if (erResult.IsSuccess && mailAddress is not null && (envelope?.UseAccessCode ?? false))
@@ -65,13 +64,13 @@ namespace EnvelopeGenerator.Web.Controllers
}
else
{
_logger.LogServiceMessage(erResult);
_logger.LogNotice(erResult);
return this.ViewEnvelopeNotFound();
}
}
catch(Exception ex)
{
_logger.LogEnvelopeError(envelopeEeceiverId: envelopeReceiverId, exception:ex, message: MessageKey.UnexpectedError.TranslateWith(_translator));
_logger.LogEnvelopeError(envelopeEeceiverId: envelopeReceiverId, exception:ex, message: _localizer[MessageKey.UnexpectedError.ToString()]);
return this.ViewInnerServiceError();
}
@@ -107,8 +106,8 @@ namespace EnvelopeGenerator.Web.Controllers
if(uuid is null || signature is null)
{
_logger.LogEnvelopeError(uuid: uuid, signature: signature, message: MessageKey.WrongEnvelopeReceiverId.TranslateWith(_translator));
return BadRequest(_envelopeService.CreateMessage(false, MessageKey.WrongEnvelopeReceiverId.ToString()));
_logger.LogEnvelopeError(uuid: uuid, signature: signature, message: _localizer[MessageKey.WrongEnvelopeReceiverId.ToString()]);
return BadRequest(_localizer[MessageKey.WrongEnvelopeReceiverId.ToString()]);
}
_logger.LogInformation($"Envelope UUID: [{uuid}]\nReceiver Signature: [{signature}]");
@@ -120,12 +119,12 @@ namespace EnvelopeGenerator.Web.Controllers
if (!verification.IsSuccess)
{
_logger.LogServiceMessage(verification);
_logger.LogNotice(verification);
if (verification.HasFlag(Flag.SecurityBreach))
return Forbid();
return StatusCode(StatusCodes.Status500InternalServerError, verification.ClientMessages.Join());
return StatusCode(StatusCodes.Status500InternalServerError, string.Join(". ", verification.Messages).Append('.'));
}
else if (isVerified)
{

View File

@@ -1,5 +1,5 @@
using DigitalData.Core.API;
using DigitalData.Core.Application;
using DigitalData.Core.DTO;
using EnvelopeGenerator.Application.Contracts;
using EnvelopeGenerator.Application.DTOs;
using EnvelopeGenerator.Application.Services;

View File

@@ -0,0 +1,19 @@
using EnvelopeGenerator.Application;
using EnvelopeGenerator.Application.Resources;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
namespace EnvelopeGenerator.Web.Controllers.Test
{
[ApiController]
[Route("api/test/[controller]")]
public class TestLocalizerController : ControllerBase
{
private readonly IStringLocalizer _localizer;
public TestLocalizerController(IStringLocalizer<Resource> localizer) => _localizer = localizer;
[HttpGet]
public IActionResult Localize([FromQuery] string key = "Hello") => Ok(_localizer[key]);
}
}

View File

@@ -55,8 +55,8 @@
<Reference Include="DigitalData.Core.Contracts">
<HintPath>..\..\WebCoreModules\DigitalData.Core.Contracts\bin\Debug\net7.0\DigitalData.Core.Contracts.dll</HintPath>
</Reference>
<Reference Include="DigitalData.Core.CultureServices">
<HintPath>..\..\WebCoreModules\DigitalData.Core.CultureServices\bin\Debug\net7.0\DigitalData.Core.CultureServices.dll</HintPath>
<Reference Include="DigitalData.Core.DTO">
<HintPath>..\..\WebCoreModules\DigitalData.Core.API\bin\Debug\net7.0\DigitalData.Core.DTO.dll</HintPath>
</Reference>
<Reference Include="DigitalData.Core.Infrastructure">
<HintPath>..\..\WebCoreModules\DigitalData.Core.Infrastructure\bin\Debug\net7.0\DigitalData.Core.Infrastructure.dll</HintPath>

View File

@@ -1,4 +1,3 @@
using DigitalData.Core.CultureServices;
using DigitalData.UserManager.Infrastructure.Repositories;
using EnvelopeGenerator.Application.Contracts;
using EnvelopeGenerator.Application.MappingProfiles;
@@ -12,9 +11,10 @@ using Quartz;
using NLog.Web;
using DigitalData.Core.API;
using Microsoft.AspNetCore.Authentication.Cookies;
using DigitalData.Core.Application;
using DigitalData.UserManager.Application.MappingProfiles;
using EnvelopeGenerator.Web.Models;
using DigitalData.Core.DTO;
using Microsoft.Extensions.Localization;
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Info("Logging initialized!");
@@ -47,8 +47,6 @@ try
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddKeyTranslationService();
//AddEF Core dbcontext
var connStr = builder.Configuration["Config:ConnectionString"];
builder.Services.AddDbContext<EGDbContext>(options =>
@@ -137,6 +135,8 @@ try
builder.Services.AddCookieConsentSettings();
builder.Services.AddCookieBasedLocalizer();
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -159,6 +159,8 @@ try
app.UseAuthentication();
app.UseAuthorization();
app.UseCookieBasedLocalizer("de_DE", "en_US");
app.MapControllers();
app.MapFallbackToController("Error404", "Home");
app.Run();

View File

@@ -1,6 +1,6 @@
@using DigitalData.Core.Contracts.Application;
@using DigitalData.Core.DTO;
@using EnvelopeGenerator.Application.DTOs;
@model IServiceResult<EnvelopeDto>;
@model DataResult<EnvelopeDto>;
@{
ViewData["Title"] = "Dokument unterschreiben";
}

View File

@@ -1,4 +1,4 @@
@using DigitalData.Core.Application.DTO;
@using DigitalData.Core.DTO;
@using Microsoft.AspNetCore.Http.Features
@using Newtonsoft.Json.Serialization;
@using Newtonsoft.Json;