Files
hrynco-notification-service/HrynCo.NotificationService.Services/TemplateDefinitions/SaveNotificationServiceHandler.cs
agrynco 8e81572386 feat(notification): add admin rest management and deletion
Add revision-safe service and template APIs, channel mapping, and validated admin controls.

Ref: IT-1601
2026-10-17 22:51:01 +03:00

31 lines
1.7 KiB
C#

using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.Abstract.Templates;
using HrynCo.NotificationService.Services.Core;
using HrynCo.NotificationService.Services.Logging;
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
internal sealed class SaveNotificationServiceHandler(
IContextualSerilogLogger<SaveNotificationServiceCommand> logger, INotificationServiceRepository services)
: RequestHandler<SaveNotificationServiceCommand, ServiceResult<NotificationServiceDto>>(logger)
{
protected override async Task<ServiceResult<NotificationServiceDto>> DoOnHandle(SaveNotificationServiceCommand request, CancellationToken cancellationToken)
{
if (!request.IsNew && request.Service.Revision == Guid.Empty)
return Failure<NotificationServiceDto>("A nonempty service revision is required for update.", ServiceErrorCode.InvalidRequest);
if (!request.IsNew && await services.GetAsync(request.Service.Id, cancellationToken) is null)
return Failure<NotificationServiceDto>("Service not found.", ServiceErrorCode.NotFound);
try
{
await services.SaveAsync(request.Service, request.IsNew, cancellationToken);
var saved = await services.GetAsync(request.Service.Id, cancellationToken);
return Success(NotificationServiceMapping.Map(saved!));
}
catch (EmailTemplateWriteException error)
{
return Failure<NotificationServiceDto>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
}
}
}