- Introduced Lazy<dynamic> field for deferred deserialization of PSPDFKitInstantJSON - Added ExpandoObject property (PSPDFKitInstant) for dynamic access - Updated handler to use ParsePSPDFKitInstant instead of ParsePSPDFKitInstantJSON - Improved JsonSerializerOptions for case-insensitive property handling
131 lines
3.6 KiB
C#
131 lines
3.6 KiB
C#
using AngleSharp.Dom;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using EnvelopeGenerator.Application.Common.Extensions;
|
|
using EnvelopeGenerator.Application.Common.Query;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Dynamic;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace EnvelopeGenerator.Application.Annotations.Commands;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record CreateAnnotationCommand : EnvelopeReceiverQueryBase, IRequest<IEnumerable<Signature>>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public CreateAnnotationCommand()
|
|
{
|
|
_lazyPSPDFKitInstant = new(() =>
|
|
{
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
PropertyNamingPolicy = null
|
|
};
|
|
|
|
return JsonSerializer.Deserialize<dynamic>(PSPDFKitInstantJSON, options) ?? new ExpandoObject();
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public string PSPDFKitInstantJSON { get; set; } = null!;
|
|
|
|
private readonly Lazy<dynamic> _lazyPSPDFKitInstant;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ExpandoObject PSPDFKitInstant => _lazyPSPDFKitInstant.Value;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class CreateAnnotationCommandHandler : IRequestHandler<CreateAnnotationCommand, IEnumerable<Signature>>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private readonly IRepository<Signature> _signRepo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private readonly IRepository<Annotation> _annotRepo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="signRepo"></param>
|
|
/// <param name="annotRepo"></param>
|
|
public CreateAnnotationCommandHandler(IRepository<Signature> signRepo, IRepository<Annotation> annotRepo)
|
|
{
|
|
_signRepo = signRepo;
|
|
_annotRepo = annotRepo;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<Signature>> Handle(CreateAnnotationCommand request, CancellationToken cancel)
|
|
{
|
|
var query = _signRepo.Query;
|
|
|
|
#region Envelope Query
|
|
if (request.Envelope.Id is int envelopeId)
|
|
query = query.Where(annot => annot.Document.EnvelopeId == envelopeId);
|
|
|
|
if (request.Envelope.Uuid is string envelopeUuid)
|
|
query = query.Where(annot => annot.Document.Envelope!.Uuid == envelopeUuid);
|
|
#endregion
|
|
|
|
// Receiver Query
|
|
query = query.Where(request.Receiver);
|
|
|
|
var elements = await query.ToListAsync(cancel);
|
|
|
|
foreach (var element in elements)
|
|
{
|
|
var annots = ParsePSPDFKitInstant(request.PSPDFKitInstant, element.Id)
|
|
.Select(annot => new Annotation()
|
|
{
|
|
ElementId = element.Id,
|
|
Name = annot.Key,
|
|
Value = annot.Value,
|
|
AddedWhen = DateTime.UtcNow
|
|
});
|
|
|
|
element.Annotations = await _annotRepo.CreateAsync(annots, cancel);
|
|
}
|
|
|
|
return elements;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="instant"></param>
|
|
/// <param name="elementId"></param>
|
|
/// <returns></returns>
|
|
public static Dictionary<string, string> ParsePSPDFKitInstant(ExpandoObject instant, int elementId)
|
|
{
|
|
Dictionary<string, string> annots = new();
|
|
|
|
// parse json here
|
|
|
|
return annots;
|
|
}
|
|
} |