feat: add MediatR handlers for template and channel CRUD
- ServiceResult<T>, ServiceError, ServiceErrorCode, Unit, ServiceResultHelper in Services/Core - RequestHandler<TRequest, TResponse> base class (MediatR-adapted, DoOnHandle pattern) - EmailTemplate handlers: Create, Update, Delete, Get, GetByService - EmailChannel handlers: Create, Update, Delete, Get, GetByService Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.Services.Logging;
|
||||
using MediatR;
|
||||
using Serilog;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
|
||||
where TRequest : IRequest<TResponse>
|
||||
{
|
||||
protected RequestHandler(IContextualSerilogLogger<TRequest> logger, IUnitOfWork unitOfWork)
|
||||
{
|
||||
Logger = logger.Logger;
|
||||
UnitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
protected ILogger Logger { get; }
|
||||
protected IUnitOfWork UnitOfWork { get; }
|
||||
|
||||
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken) =>
|
||||
DoOnHandle(request, cancellationToken);
|
||||
|
||||
protected abstract Task<TResponse> DoOnHandle(TRequest request, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public sealed record ServiceError
|
||||
{
|
||||
public ServiceError(string message, ServiceErrorCode? code = null)
|
||||
{
|
||||
Message = message;
|
||||
Code = code;
|
||||
}
|
||||
|
||||
public ServiceErrorCode? Code { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public enum ServiceErrorCode
|
||||
{
|
||||
NotFound = 1,
|
||||
Conflict = 2,
|
||||
InvalidRequest = 3,
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public record ServiceResult<TResult>
|
||||
{
|
||||
public ServiceError? Error { get; private set; }
|
||||
public bool IsSuccess { get; private set; }
|
||||
public TResult? Result { get; private set; }
|
||||
|
||||
public static ServiceResult<TResult> Success(TResult result) =>
|
||||
new() { IsSuccess = true, Result = result };
|
||||
|
||||
public static ServiceResult<TResult> Failure(ServiceError error) =>
|
||||
new() { IsSuccess = false, Error = error };
|
||||
|
||||
public static ServiceResult<TResult> Failure(string message, ServiceErrorCode? code = null) =>
|
||||
Failure(new ServiceError(message, code));
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public static class ServiceResultHelper
|
||||
{
|
||||
public static ServiceResult<T> Success<T>(T result) => ServiceResult<T>.Success(result);
|
||||
|
||||
public static ServiceResult<T> Failure<T>(string errorMessage, ServiceErrorCode? errorCode = null) =>
|
||||
ServiceResult<T>.Failure(errorMessage, errorCode);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace HrynCo.NotificationService.Services.Core;
|
||||
|
||||
public readonly struct Unit
|
||||
{
|
||||
public static readonly Unit Value = new();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Create;
|
||||
|
||||
public sealed record CreateEmailChannelCommand(
|
||||
string ServiceName,
|
||||
int Priority,
|
||||
EmailChannelType ChannelType,
|
||||
EmailChannelSettings Settings,
|
||||
int? DailyLimit,
|
||||
int? MonthlyLimit,
|
||||
int WarnThresholdPercent,
|
||||
bool IsActive
|
||||
) : IRequest<ServiceResult<Guid>>;
|
||||
@@ -0,0 +1,43 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
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.EmailChannels.Create;
|
||||
|
||||
internal sealed class CreateEmailChannelHandler
|
||||
: RequestHandler<CreateEmailChannelCommand, ServiceResult<Guid>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public CreateEmailChannelHandler(
|
||||
IContextualSerilogLogger<CreateEmailChannelCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Guid>> DoOnHandle(
|
||||
CreateEmailChannelCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channel = new EmailChannel
|
||||
{
|
||||
ServiceName = request.ServiceName,
|
||||
Priority = request.Priority,
|
||||
EmailChannelType = request.ChannelType,
|
||||
Settings = request.Settings,
|
||||
DailyLimit = request.DailyLimit,
|
||||
MonthlyLimit = request.MonthlyLimit,
|
||||
WarnThresholdPercent = request.WarnThresholdPercent,
|
||||
IsActive = request.IsActive
|
||||
};
|
||||
|
||||
await _channels.AddAsync(channel, cancellationToken);
|
||||
|
||||
return Success(channel.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Delete;
|
||||
|
||||
public sealed record DeleteEmailChannelCommand(Guid Id)
|
||||
: IRequest<ServiceResult<Unit>>;
|
||||
@@ -0,0 +1,35 @@
|
||||
using HrynCo.NotificationService.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.EmailChannels.Delete;
|
||||
|
||||
internal sealed class DeleteEmailChannelHandler
|
||||
: RequestHandler<DeleteEmailChannelCommand, ServiceResult<Unit>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public DeleteEmailChannelHandler(
|
||||
IContextualSerilogLogger<DeleteEmailChannelCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Unit>> DoOnHandle(
|
||||
DeleteEmailChannelCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channel = await _channels.GetByIdAsync(request.Id, cancellationToken);
|
||||
|
||||
if (channel is null)
|
||||
return Failure<Unit>("Email channel not found.", ServiceErrorCode.NotFound);
|
||||
|
||||
await _channels.DeleteAsync(channel, cancellationToken);
|
||||
|
||||
return Success(Unit.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
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.EmailChannels.Get;
|
||||
|
||||
internal sealed class GetEmailChannelHandler
|
||||
: RequestHandler<GetEmailChannelQuery, ServiceResult<EmailChannel>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public GetEmailChannelHandler(
|
||||
IContextualSerilogLogger<GetEmailChannelQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<EmailChannel>> DoOnHandle(
|
||||
GetEmailChannelQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channel = await _channels.GetByIdAsync(request.Id, cancellationToken);
|
||||
|
||||
return channel is null
|
||||
? Failure<EmailChannel>("Email channel not found.", ServiceErrorCode.NotFound)
|
||||
: Success(channel);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Get;
|
||||
|
||||
public sealed record GetEmailChannelQuery(Guid Id)
|
||||
: IRequest<ServiceResult<EmailChannel>>;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
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.EmailChannels.GetByService;
|
||||
|
||||
internal sealed class GetEmailChannelsHandler
|
||||
: RequestHandler<GetEmailChannelsQuery, ServiceResult<IReadOnlyList<EmailChannel>>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public GetEmailChannelsHandler(
|
||||
IContextualSerilogLogger<GetEmailChannelsQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<IReadOnlyList<EmailChannel>>> DoOnHandle(
|
||||
GetEmailChannelsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channels = await _channels.GetByServiceAsync(request.ServiceName, cancellationToken);
|
||||
return Success(channels);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.GetByService;
|
||||
|
||||
public sealed record GetEmailChannelsQuery(string ServiceName)
|
||||
: IRequest<ServiceResult<IReadOnlyList<EmailChannel>>>;
|
||||
@@ -0,0 +1,16 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailChannels.Update;
|
||||
|
||||
public sealed record UpdateEmailChannelCommand(
|
||||
Guid Id,
|
||||
int Priority,
|
||||
EmailChannelSettings Settings,
|
||||
int? DailyLimit,
|
||||
int? MonthlyLimit,
|
||||
int WarnThresholdPercent,
|
||||
bool IsActive
|
||||
) : IRequest<ServiceResult<Unit>>;
|
||||
@@ -0,0 +1,43 @@
|
||||
using HrynCo.NotificationService.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.EmailChannels.Update;
|
||||
|
||||
internal sealed class UpdateEmailChannelHandler
|
||||
: RequestHandler<UpdateEmailChannelCommand, ServiceResult<Unit>>
|
||||
{
|
||||
private readonly IEmailChannelRepository _channels;
|
||||
|
||||
public UpdateEmailChannelHandler(
|
||||
IContextualSerilogLogger<UpdateEmailChannelCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailChannelRepository channels)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Unit>> DoOnHandle(
|
||||
UpdateEmailChannelCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var channel = await _channels.GetByIdAsync(request.Id, cancellationToken);
|
||||
|
||||
if (channel is null)
|
||||
return Failure<Unit>("Email channel not found.", ServiceErrorCode.NotFound);
|
||||
|
||||
channel.Priority = request.Priority;
|
||||
channel.Settings = request.Settings;
|
||||
channel.DailyLimit = request.DailyLimit;
|
||||
channel.MonthlyLimit = request.MonthlyLimit;
|
||||
channel.WarnThresholdPercent = request.WarnThresholdPercent;
|
||||
channel.IsActive = request.IsActive;
|
||||
channel.Updated = DateTimeOffset.UtcNow;
|
||||
|
||||
await _channels.UpdateAsync(channel, cancellationToken);
|
||||
|
||||
return Success(Unit.Value);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Create;
|
||||
|
||||
public sealed record CreateEmailTemplateCommand(
|
||||
string ServiceName,
|
||||
string Key,
|
||||
string LanguageCode,
|
||||
string Subject,
|
||||
string HtmlBody,
|
||||
string TextBody,
|
||||
IReadOnlyList<EmailTemplateVariable> Variables
|
||||
) : IRequest<ServiceResult<Guid>>;
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
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.EmailTemplates.Create;
|
||||
|
||||
internal sealed class CreateEmailTemplateHandler
|
||||
: RequestHandler<CreateEmailTemplateCommand, ServiceResult<Guid>>
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templates;
|
||||
|
||||
public CreateEmailTemplateHandler(
|
||||
IContextualSerilogLogger<CreateEmailTemplateCommand> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_templates = templates;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<Guid>> DoOnHandle(
|
||||
CreateEmailTemplateCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var existing = await _templates.GetAsync(
|
||||
request.ServiceName, request.Key, request.LanguageCode, cancellationToken);
|
||||
|
||||
if (existing is not null)
|
||||
return Failure<Guid>("Template already exists.", ServiceErrorCode.Conflict);
|
||||
|
||||
var template = new EmailTemplate
|
||||
{
|
||||
ServiceName = request.ServiceName,
|
||||
Key = request.Key,
|
||||
LanguageCode = request.LanguageCode,
|
||||
Subject = request.Subject,
|
||||
HtmlBody = request.HtmlBody,
|
||||
TextBody = request.TextBody,
|
||||
Variables = request.Variables
|
||||
};
|
||||
|
||||
await _templates.AddAsync(template, cancellationToken);
|
||||
|
||||
return Success(template.Id);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Delete;
|
||||
|
||||
public sealed record DeleteEmailTemplateCommand(
|
||||
string ServiceName,
|
||||
string Key,
|
||||
string LanguageCode
|
||||
) : IRequest<ServiceResult<Unit>>;
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
using HrynCo.NotificationService.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,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_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);
|
||||
|
||||
await _templates.DeleteAsync(template, cancellationToken);
|
||||
|
||||
return Success(Unit.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
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.EmailTemplates.Get;
|
||||
|
||||
internal sealed class GetEmailTemplateHandler
|
||||
: RequestHandler<GetEmailTemplateQuery, ServiceResult<EmailTemplate>>
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templates;
|
||||
|
||||
public GetEmailTemplateHandler(
|
||||
IContextualSerilogLogger<GetEmailTemplateQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_templates = templates;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<EmailTemplate>> DoOnHandle(
|
||||
GetEmailTemplateQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var template = await _templates.GetAsync(
|
||||
request.ServiceName, request.Key, request.LanguageCode, cancellationToken);
|
||||
|
||||
return template is null
|
||||
? Failure<EmailTemplate>("Template not found.", ServiceErrorCode.NotFound)
|
||||
: Success(template);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Get;
|
||||
|
||||
public sealed record GetEmailTemplateQuery(string ServiceName, string Key, string LanguageCode)
|
||||
: IRequest<ServiceResult<EmailTemplate>>;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract;
|
||||
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.EmailTemplates.GetByService;
|
||||
|
||||
internal sealed class GetEmailTemplatesHandler
|
||||
: RequestHandler<GetEmailTemplatesQuery, ServiceResult<IReadOnlyList<EmailTemplate>>>
|
||||
{
|
||||
private readonly IEmailTemplateRepository _templates;
|
||||
|
||||
public GetEmailTemplatesHandler(
|
||||
IContextualSerilogLogger<GetEmailTemplatesQuery> logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_templates = templates;
|
||||
}
|
||||
|
||||
protected override async Task<ServiceResult<IReadOnlyList<EmailTemplate>>> DoOnHandle(
|
||||
GetEmailTemplatesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var templates = await _templates.GetByServiceAsync(request.ServiceName, cancellationToken);
|
||||
return Success(templates);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.GetByService;
|
||||
|
||||
public sealed record GetEmailTemplatesQuery(string ServiceName)
|
||||
: IRequest<ServiceResult<IReadOnlyList<EmailTemplate>>>;
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using MediatR;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
|
||||
namespace HrynCo.NotificationService.Services.EmailTemplates.Update;
|
||||
|
||||
public sealed record UpdateEmailTemplateCommand(
|
||||
string ServiceName,
|
||||
string Key,
|
||||
string LanguageCode,
|
||||
string Subject,
|
||||
string HtmlBody,
|
||||
string TextBody,
|
||||
IReadOnlyList<EmailTemplateVariable> Variables
|
||||
) : IRequest<ServiceResult<Unit>>;
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
using HrynCo.NotificationService.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,
|
||||
IUnitOfWork unitOfWork,
|
||||
IEmailTemplateRepository templates)
|
||||
: base(logger, unitOfWork)
|
||||
{
|
||||
_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;
|
||||
|
||||
await _templates.UpdateAsync(template, cancellationToken);
|
||||
|
||||
return Success(Unit.Value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user