| | | 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] |
| | 442 | 16 | | public class EFCoreTriggerStore( |
| | 442 | 17 | | EntityStore<RuntimeElsaDbContext, StoredTrigger> store, |
| | 442 | 18 | | IPayloadSerializer serializer) : ITriggerStore |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public async ValueTask SaveAsync(StoredTrigger record, CancellationToken cancellationToken = default) |
| | | 22 | | { |
| | 1 | 23 | | await store.SaveAsync(record, OnSaveAsync, cancellationToken); |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public async ValueTask SaveManyAsync(IEnumerable<StoredTrigger> records, CancellationToken cancellationToken = defau |
| | | 28 | | { |
| | 0 | 29 | | await store.SaveManyAsync(records, OnSaveAsync, cancellationToken); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public async ValueTask<StoredTrigger?> FindAsync(TriggerFilter filter, CancellationToken cancellationToken = default |
| | | 34 | | { |
| | 2 | 35 | | return await store.FindAsync(filter.Apply, OnLoadAsync, filter.TenantAgnostic, cancellationToken); |
| | 2 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public async ValueTask<IEnumerable<StoredTrigger>> FindManyAsync(TriggerFilter filter, CancellationToken cancellatio |
| | | 40 | | { |
| | 1243 | 41 | | return await store.QueryAsync(filter.Apply, OnLoadAsync, filter.TenantAgnostic, cancellationToken); |
| | 1243 | 42 | | } |
| | | 43 | | |
| | | 44 | | public ValueTask<Page<StoredTrigger>> FindManyAsync(TriggerFilter filter, PageArgs pageArgs, CancellationToken cance |
| | | 45 | | { |
| | 0 | 46 | | return FindManyAsync(filter, pageArgs, new StoredTriggerOrder<string>(x => x.Id, OrderDirection.Ascending), canc |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public async ValueTask<Page<StoredTrigger>> FindManyAsync<TProp>(TriggerFilter filter, PageArgs pageArgs, StoredTrig |
| | | 50 | | { |
| | 0 | 51 | | var count = await store.QueryAsync(filter.Apply, OnLoadAsync, cancellationToken).LongCount(); |
| | 0 | 52 | | var results = await store.QueryAsync(queryable => filter.Apply(queryable).OrderBy(order).Paginate(pageArgs).Orde |
| | 0 | 53 | | return new(results, count); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public async ValueTask ReplaceAsync(IEnumerable<StoredTrigger> removed, IEnumerable<StoredTrigger> added, Cancellati |
| | | 58 | | { |
| | 1117 | 59 | | var removedList = removed.ToList(); |
| | | 60 | | |
| | 1117 | 61 | | if(removedList.Count > 0) |
| | | 62 | | { |
| | 32 | 63 | | var filter = new TriggerFilter { Ids = removedList.Select(r => r.Id).ToList() }; |
| | 16 | 64 | | await DeleteManyAsync(filter, cancellationToken); |
| | | 65 | | } |
| | | 66 | | |
| | 1117 | 67 | | await store.SaveManyAsync(added, OnSaveAsync, cancellationToken); |
| | 1117 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | public async ValueTask<long> DeleteManyAsync(TriggerFilter filter, CancellationToken cancellationToken = default) |
| | | 72 | | { |
| | 16 | 73 | | return await store.DeleteWhereAsync(filter.Apply, cancellationToken); |
| | 16 | 74 | | } |
| | | 75 | | |
| | | 76 | | private ValueTask OnSaveAsync(RuntimeElsaDbContext dbContext, StoredTrigger entity, CancellationToken cancellationTo |
| | | 77 | | { |
| | 43 | 78 | | dbContext.Entry(entity).Property("SerializedPayload").CurrentValue = entity.Payload != null ? serializer.Seriali |
| | 43 | 79 | | return default; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | private ValueTask OnLoadAsync(RuntimeElsaDbContext dbContext, StoredTrigger? entity, CancellationToken cancellationT |
| | | 83 | | { |
| | 558 | 84 | | if (entity is null) |
| | 0 | 85 | | return ValueTask.CompletedTask; |
| | | 86 | | |
| | 558 | 87 | | var json = dbContext.Entry(entity).Property<string>("SerializedPayload").CurrentValue; |
| | 558 | 88 | | entity.Payload = !string.IsNullOrEmpty(json) ? serializer.Deserialize(json) : null; |
| | | 89 | | |
| | 558 | 90 | | return ValueTask.CompletedTask; |
| | | 91 | | } |
| | | 92 | | } |