Files
hrynco-notification-service/HrynCo.NotificationService.Services/EmailTemplates/Update/UpdateEmailTemplateHandler.cs
T
agrynco 81654c0d33 feat(templates): add service language template management
Add normalized service and template catalogs, scoped administration, migration, validation, and regression coverage.

Ref: IT-1601
2026-10-17 22:02:43 +03:00

50 lines
1.7 KiB
C#

using HrynCo.NotificationService.DAL.Abstract.Templates;
using HrynCo.DAL.Abstract;
using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.Services.Core;
using HrynCo.NotificationService.Services.Logging;
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
namespace HrynCo.NotificationService.Services.EmailTemplates.Update;
internal sealed class UpdateEmailTemplateHandler
: RequestHandler<UpdateEmailTemplateCommand, ServiceResult<Unit>>
{
private readonly IEmailTemplateRepository _templates;
public UpdateEmailTemplateHandler(
IContextualSerilogLogger<UpdateEmailTemplateCommand> logger,
IEmailTemplateRepository templates)
: base(logger)
{
_templates = templates;
}
protected override async Task<ServiceResult<Unit>> DoOnHandle(
UpdateEmailTemplateCommand request, CancellationToken cancellationToken)
{
var template = await _templates.GetAsync(
request.ServiceName, request.Key, request.LanguageCode, cancellationToken);
if (template is null)
return Failure<Unit>("Template not found.", ServiceErrorCode.NotFound);
template.Subject = request.Subject;
template.HtmlBody = request.HtmlBody;
template.TextBody = request.TextBody;
template.Variables = request.Variables;
template.Updated = DateTimeOffset.UtcNow;
try
{
await _templates.UpdateAsync(template, cancellationToken);
}
catch (EmailTemplateWriteException error)
{
return Failure<Unit>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
}
return Success(Unit.Value);
}
}