Files
hrynco-notification-service/HrynCo.NotificationService.Services/EmailTemplates/Delete/DeleteEmailTemplateHandler.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

46 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.Delete;
internal sealed class DeleteEmailTemplateHandler
: RequestHandler<DeleteEmailTemplateCommand, ServiceResult<Unit>>
{
private readonly IEmailTemplateRepository _templates;
public DeleteEmailTemplateHandler(
IContextualSerilogLogger<DeleteEmailTemplateCommand> logger,
IEmailTemplateRepository templates)
: base(logger)
{
_templates = templates;
}
protected override async Task<ServiceResult<Unit>> DoOnHandle(
DeleteEmailTemplateCommand 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);
if (request.ExpectedVersionId is Guid expectedId && template.Id != expectedId)
return Failure<Unit>("Template changed after you opened it. Reload before deleting.", ServiceErrorCode.Conflict);
try
{
await _templates.DeleteAsync(template, cancellationToken);
}
catch (EmailTemplateWriteException error)
{
return Failure<Unit>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
}
return Success(Unit.Value);
}
}