| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Alterations.Core.Contracts; |
| | | 4 | | using Elsa.Alterations.Core.Entities; |
| | | 5 | | using Elsa.Alterations.Core.Filters; |
| | | 6 | | using Elsa.Alterations.Core.Models; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Persistence.EFCore.Modules.Alterations; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// An EF Core implementation of <see cref="IAlterationPlanStore"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | public class EFCoreAlterationPlanStore : IAlterationPlanStore |
| | | 14 | | { |
| | | 15 | | private readonly EntityStore<AlterationsElsaDbContext, AlterationPlan> _store; |
| | | 16 | | private readonly IAlterationSerializer _alterationSerializer; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Constructor. |
| | | 20 | | /// </summary> |
| | 320 | 21 | | public EFCoreAlterationPlanStore(EntityStore<AlterationsElsaDbContext, AlterationPlan> store, IAlterationSerializer |
| | | 22 | | { |
| | 320 | 23 | | _store = store; |
| | 320 | 24 | | _alterationSerializer = alterationSerializer; |
| | 320 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public async Task SaveAsync(AlterationPlan record, CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | 0 | 30 | | await _store.SaveAsync(record, OnSaveAsync, cancellationToken); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" |
| | | 35 | | public async Task<AlterationPlan?> FindAsync(AlterationPlanFilter filter, CancellationToken cancellationToken = defa |
| | | 36 | | { |
| | 0 | 37 | | return await _store.FindAsync(filter.Apply, OnLoadAsync, cancellationToken); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public async Task<long> CountAsync(AlterationPlanFilter filter, CancellationToken cancellationToken = default) |
| | | 42 | | { |
| | 0 | 43 | | return await _store.CountAsync(filter.Apply, cancellationToken); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | private ValueTask OnSaveAsync(AlterationsElsaDbContext elsaDbContext, AlterationPlan entity, CancellationToken cance |
| | | 47 | | { |
| | 0 | 48 | | elsaDbContext.Entry(entity).Property("SerializedAlterations").CurrentValue = _alterationSerializer.SerializeMany |
| | 0 | 49 | | elsaDbContext.Entry(entity).Property("SerializedWorkflowInstanceFilter").CurrentValue = JsonSerializer.Serialize |
| | 0 | 50 | | return default; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" |
| | | 54 | | private ValueTask OnLoadAsync(AlterationsElsaDbContext elsaDbContext, AlterationPlan? entity, CancellationToken canc |
| | | 55 | | { |
| | 0 | 56 | | if (entity is null) |
| | 0 | 57 | | return default; |
| | | 58 | | |
| | 0 | 59 | | var alterationsJson = elsaDbContext.Entry(entity).Property<string>("SerializedAlterations").CurrentValue; |
| | 0 | 60 | | var workflowInstanceFilterJson = elsaDbContext.Entry(entity).Property<string>("SerializedWorkflowInstanceFilter" |
| | 0 | 61 | | entity.Alterations = _alterationSerializer.DeserializeMany(alterationsJson).ToList(); |
| | 0 | 62 | | entity.WorkflowInstanceFilter = JsonSerializer.Deserialize<AlterationWorkflowInstanceFilter>(workflowInstanceFil |
| | | 63 | | |
| | 0 | 64 | | return default; |
| | | 65 | | } |
| | | 66 | | } |