| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Runtime; |
| | | 6 | | using Elsa.Workflows.Runtime.Entities; |
| | | 7 | | using Elsa.Workflows.Runtime.Filters; |
| | | 8 | | using Elsa.Workflows.Runtime.OrderDefinitions; |
| | | 9 | | using JetBrains.Annotations; |
| | | 10 | | using Open.Linq.AsyncExtensions; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Persistence.EFCore.Modules.Runtime; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | [UsedImplicitly] |
| | 321 | 16 | | public class EFCoreTriggerStore(EntityStore<RuntimeElsaDbContext, StoredTrigger> store, IPayloadSerializer serializer) : |
| | | 17 | | { |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public async ValueTask SaveAsync(StoredTrigger record, CancellationToken cancellationToken = default) |
| | | 20 | | { |
| | 0 | 21 | | await store.SaveAsync(record, OnSaveAsync, cancellationToken); |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public async ValueTask SaveManyAsync(IEnumerable<StoredTrigger> records, CancellationToken cancellationToken = defau |
| | | 26 | | { |
| | 0 | 27 | | await store.SaveManyAsync(records, OnSaveAsync, cancellationToken); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public async ValueTask<StoredTrigger?> FindAsync(TriggerFilter filter, CancellationToken cancellationToken = default |
| | | 32 | | { |
| | 2 | 33 | | return await store.FindAsync(filter.Apply, OnLoadAsync, filter.TenantAgnostic, cancellationToken); |
| | 2 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public async ValueTask<IEnumerable<StoredTrigger>> FindManyAsync(TriggerFilter filter, CancellationToken cancellatio |
| | | 38 | | { |
| | 524 | 39 | | return await store.QueryAsync(filter.Apply, OnLoadAsync, filter.TenantAgnostic, cancellationToken); |
| | 524 | 40 | | } |
| | | 41 | | |
| | | 42 | | public ValueTask<Page<StoredTrigger>> FindManyAsync(TriggerFilter filter, PageArgs pageArgs, CancellationToken cance |
| | | 43 | | { |
| | 0 | 44 | | return FindManyAsync(filter, pageArgs, new StoredTriggerOrder<string>(x => x.Id, OrderDirection.Ascending), canc |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public async ValueTask<Page<StoredTrigger>> FindManyAsync<TProp>(TriggerFilter filter, PageArgs pageArgs, StoredTrig |
| | | 48 | | { |
| | 0 | 49 | | var count = await store.QueryAsync(filter.Apply, OnLoadAsync, cancellationToken).LongCount(); |
| | 0 | 50 | | var results = await store.QueryAsync(queryable => filter.Apply(queryable).OrderBy(order).Paginate(pageArgs).Orde |
| | 0 | 51 | | return new(results, count); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public async ValueTask ReplaceAsync(IEnumerable<StoredTrigger> removed, IEnumerable<StoredTrigger> added, Cancellati |
| | | 56 | | { |
| | 488 | 57 | | var removedList = removed.ToList(); |
| | | 58 | | |
| | 488 | 59 | | if(removedList.Count > 0) |
| | | 60 | | { |
| | 8 | 61 | | var filter = new TriggerFilter { Ids = removedList.Select(r => r.Id).ToList() }; |
| | 4 | 62 | | await DeleteManyAsync(filter, cancellationToken); |
| | | 63 | | } |
| | | 64 | | |
| | 488 | 65 | | await store.SaveManyAsync(added, OnSaveAsync, cancellationToken); |
| | 488 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public async ValueTask<long> DeleteManyAsync(TriggerFilter filter, CancellationToken cancellationToken = default) |
| | | 70 | | { |
| | 4 | 71 | | return await store.DeleteWhereAsync(filter.Apply, cancellationToken); |
| | 4 | 72 | | } |
| | | 73 | | |
| | | 74 | | private ValueTask OnSaveAsync(RuntimeElsaDbContext dbContext, StoredTrigger entity, CancellationToken cancellationTo |
| | | 75 | | { |
| | 13 | 76 | | dbContext.Entry(entity).Property("SerializedPayload").CurrentValue = entity.Payload != null ? serializer.Seriali |
| | 13 | 77 | | return default; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | private ValueTask OnLoadAsync(RuntimeElsaDbContext dbContext, StoredTrigger? entity, CancellationToken cancellationT |
| | | 81 | | { |
| | 98 | 82 | | if (entity is null) |
| | 0 | 83 | | return ValueTask.CompletedTask; |
| | | 84 | | |
| | 98 | 85 | | var json = dbContext.Entry(entity).Property<string>("SerializedPayload").CurrentValue; |
| | 98 | 86 | | entity.Payload = !string.IsNullOrEmpty(json) ? serializer.Deserialize(json) : null; |
| | | 87 | | |
| | 98 | 88 | | return ValueTask.CompletedTask; |
| | | 89 | | } |
| | | 90 | | } |