| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Common.Services; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Runtime.Entities; |
| | | 6 | | using Elsa.Workflows.Runtime.Filters; |
| | | 7 | | using Elsa.Workflows.Runtime.OrderDefinitions; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Runtime.Stores; |
| | | 11 | | |
| | | 12 | | /// <inheritdoc /> |
| | | 13 | | [UsedImplicitly] |
| | | 14 | | public class MemoryTriggerStore : ITriggerStore |
| | | 15 | | { |
| | | 16 | | private readonly MemoryStore<StoredTrigger> _store; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="MemoryTriggerStore"/> class. |
| | | 20 | | /// </summary> |
| | 158 | 21 | | public MemoryTriggerStore(MemoryStore<StoredTrigger> store) |
| | | 22 | | { |
| | 158 | 23 | | _store = store; |
| | 158 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public ValueTask SaveAsync(StoredTrigger record, CancellationToken cancellationToken = default) |
| | | 28 | | { |
| | 12 | 29 | | _store.Save(record, x => x.Id); |
| | 6 | 30 | | return new(); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public ValueTask SaveManyAsync(IEnumerable<StoredTrigger> records, CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | 0 | 36 | | _store.SaveMany(records, x => x.Id); |
| | 0 | 37 | | return new(); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public ValueTask<StoredTrigger?> FindAsync(TriggerFilter filter, CancellationToken cancellationToken = default) |
| | | 42 | | { |
| | 0 | 43 | | var entity = _store.Query(filter.Apply).FirstOrDefault(); |
| | 0 | 44 | | return new(entity); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public ValueTask<IEnumerable<StoredTrigger>> FindManyAsync(TriggerFilter filter, CancellationToken cancellationToken |
| | | 49 | | { |
| | 165 | 50 | | var entities = _store.Query(filter.Apply); |
| | 165 | 51 | | return new(entities); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public ValueTask<Page<StoredTrigger>> FindManyAsync(TriggerFilter filter, PageArgs pageArgs, CancellationToken cance |
| | | 55 | | { |
| | 0 | 56 | | return FindManyAsync(filter, pageArgs, new StoredTriggerOrder<string>(x => x.Id, OrderDirection.Ascending), canc |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public ValueTask<Page<StoredTrigger>> FindManyAsync<TOrderBy>(TriggerFilter filter, PageArgs pageArgs, StoredTrigger |
| | | 60 | | { |
| | 0 | 61 | | var count = _store.Query(filter.Apply).LongCount(); |
| | 0 | 62 | | var result = _store.Query(query => filter.Apply(query).OrderBy(order).Paginate(pageArgs)).ToList(); |
| | 0 | 63 | | return ValueTask.FromResult(Page.Of(result, count)); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public ValueTask ReplaceAsync(IEnumerable<StoredTrigger> removed, IEnumerable<StoredTrigger> added, CancellationToke |
| | | 68 | | { |
| | 155 | 69 | | _store.DeleteMany(removed, x => x.Id); |
| | 158 | 70 | | _store.SaveMany(added, x => x.Id); |
| | 152 | 71 | | return new(); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | | 75 | | public async ValueTask<long> DeleteManyAsync(TriggerFilter filter, CancellationToken cancellationToken = default) |
| | | 76 | | { |
| | 0 | 77 | | var ids = (await FindManyAsync(filter, cancellationToken)).Select(x => x.Id); |
| | 0 | 78 | | return _store.DeleteMany(ids); |
| | 0 | 79 | | } |
| | | 80 | | } |