Updated namespaces to align with the new DigitalData.Core structure, replacing `DigitalData.Core.Abstractions` with `DigitalData.Core.Application.Interfaces` and `DigitalData.Core.Client.Interface`. Removed the `IUnique<int>` interface from several DTOs, simplifying their design and altering the handling of entity identification. Updated project files to reflect new dependency versions for improved compatibility and features. Cleaned up using directives to remove obsolete references, enhancing code maintainability.
64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using DigitalData.Core.Application.Interfaces.Repository;
|
|
using EnvelopeGenerator.Application.DTOs;
|
|
using EnvelopeGenerator.Application.Exceptions;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
|
|
namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class UpdateEmailTemplateCommandHandler : IRequestHandler<UpdateEmailTemplateCommand>
|
|
{
|
|
private readonly IRepository<EmailTemplate> _repository;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repository"></param>
|
|
public UpdateEmailTemplateCommandHandler(IRepository<EmailTemplate> repository)
|
|
{
|
|
_repository = repository;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
/// <exception cref="NotFoundException"></exception>
|
|
public async Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel)
|
|
{
|
|
EmailTemplateDto? temp;
|
|
|
|
if (request.EmailTemplateQuery?.Id is int id)
|
|
{
|
|
temp = await _repository.ReadOrDefaultAsync<EmailTemplateDto>(t => t.Id == id, single: false, cancel);
|
|
}
|
|
else if (request!.EmailTemplateQuery!.Type is Common.Constants.EmailTemplateType type)
|
|
{
|
|
temp = await _repository.ReadOrDefaultAsync<EmailTemplateDto>(t => t.Name == type.ToString(), single: false, cancel);
|
|
}
|
|
else
|
|
{
|
|
throw new InvalidOperationException("Both id and type is null. Id: " + request.EmailTemplateQuery.Id +". Type: " + request.EmailTemplateQuery.Type.ToString());
|
|
}
|
|
|
|
if (temp == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
if (request.Body is not null)
|
|
temp.Body = request.Body;
|
|
|
|
if (request.Subject is not null)
|
|
temp.Subject = request.Subject;
|
|
|
|
await _repository.UpdateAsync(temp, t => t.Id == temp.Id, cancel);
|
|
}
|
|
}
|