using DigitalData.Core.Abstractions.Infrastructure; using EnvelopeGenerator.Application.DTOs; using EnvelopeGenerator.Application.Exceptions; using EnvelopeGenerator.Domain.Entities; using MediatR; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update; /// /// /// public class UpdateEmailTemplateCommandHandler : IRequestHandler { private readonly IRepository _repository; /// /// /// /// public UpdateEmailTemplateCommandHandler(IRepository repository) { _repository = repository; } public async Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel) { var temp = (request.EmailTemplateQuery?.Id is int id ? await _repository.ReadOrDefaultAsync(t => t.Id == id, single: false, cancel) : request!.EmailTemplateQuery!.Type is Common.Constants.EmailTemplateType type ? await _repository.ReadOrDefaultAsync(t => t.Name == type.ToString(), single: false, cancel) : throw new InvalidOperationException("Both id and type is null. Id: " + request.EmailTemplateQuery.Id + ". Type: " + request.EmailTemplateQuery.Type.ToString())) ?? 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); } }