| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Common.Services; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows.Management.Entities; |
| | | 5 | | using Elsa.Workflows.Management.Filters; |
| | | 6 | | using Elsa.Workflows.Management.Models; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Management.Stores; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// A memory implementation of <see cref="IWorkflowDefinitionStore"/>. |
| | | 12 | | /// </summary> |
| | 0 | 13 | | public class MemoryWorkflowDefinitionStore(MemoryStore<WorkflowDefinition> store) : IWorkflowDefinitionStore |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public Task<WorkflowDefinition?> FindAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = de |
| | | 17 | | { |
| | 0 | 18 | | var result = store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 0 | 19 | | return Task.FromResult(result); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public Task<WorkflowDefinition?> FindAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinitionOrder<TOrder |
| | | 24 | | { |
| | 0 | 25 | | var result = store.Query(query => Filter(query, filter).OrderBy(order)).FirstOrDefault(); |
| | 0 | 26 | | return Task.FromResult(result); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public Task<Page<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, PageArgs pageArgs, Cancellation |
| | | 31 | | { |
| | 0 | 32 | | var count = store.Query(query => Filter(query, filter)).LongCount(); |
| | 0 | 33 | | var result = store.Query(query => Filter(query, filter).Paginate(pageArgs)).ToList(); |
| | 0 | 34 | | return Task.FromResult(Page.Of(result, count)); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public Task<Page<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinitionOrd |
| | | 39 | | { |
| | 0 | 40 | | var count = store.Query(query => Filter(query, filter).OrderBy(order)).LongCount(); |
| | 0 | 41 | | var result = store.Query(query => Filter(query, filter).Paginate(pageArgs)).ToList(); |
| | 0 | 42 | | return Task.FromResult(Page.Of(result, count)); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public Task<IEnumerable<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, CancellationToken cancel |
| | | 47 | | { |
| | 0 | 48 | | var result = store.Query(query => Filter(query, filter)).ToList().AsEnumerable(); |
| | 0 | 49 | | return Task.FromResult(result); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public Task<IEnumerable<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefini |
| | | 54 | | { |
| | 0 | 55 | | var result = store.Query(query => Filter(query, filter).OrderBy(order)).ToList().AsEnumerable(); |
| | 0 | 56 | | return Task.FromResult(result); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, PageArgs pageArgs, |
| | | 61 | | { |
| | 0 | 62 | | var count = store.Query(query => Filter(query, filter)).LongCount(); |
| | 0 | 63 | | var result = store.Query(query => Filter(query, filter).Paginate(pageArgs)).Select(WorkflowDefinitionSummary.Fro |
| | 0 | 64 | | return Task.FromResult(Page.Of(result, count)); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowD |
| | | 69 | | { |
| | 0 | 70 | | var count = store.Query(query => Filter(query, filter).OrderBy(order)).LongCount(); |
| | 0 | 71 | | var result = store.Query(query => Filter(query, filter).Paginate(pageArgs)).Select(WorkflowDefinitionSummary.Fro |
| | 0 | 72 | | return Task.FromResult(Page.Of(result, count)); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, Cancellation |
| | | 77 | | { |
| | 0 | 78 | | var result = store.Query(query => Filter(query, filter)).Select(WorkflowDefinitionSummary.FromDefinition).ToList |
| | 0 | 79 | | return Task.FromResult(result); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | | 83 | | public Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filter, Wo |
| | | 84 | | { |
| | 0 | 85 | | var result = store.Query(query => Filter(query, filter).OrderBy(order)).Select(WorkflowDefinitionSummary.FromDef |
| | 0 | 86 | | return Task.FromResult(result); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | | 90 | | public Task<WorkflowDefinition?> FindLastVersionAsync(WorkflowDefinitionFilter filter, CancellationToken cancellatio |
| | | 91 | | { |
| | 0 | 92 | | var result = store.Query(query => Filter(query, filter)).MaxBy(x => x.Version); |
| | 0 | 93 | | return Task.FromResult(result); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <inheritdoc /> |
| | | 97 | | public Task SaveAsync(WorkflowDefinition definition, CancellationToken cancellationToken = default) |
| | | 98 | | { |
| | 0 | 99 | | store.Save(definition, GetId); |
| | 0 | 100 | | return Task.CompletedTask; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <inheritdoc /> |
| | | 104 | | public Task SaveManyAsync(IEnumerable<WorkflowDefinition> definitions, CancellationToken cancellationToken = default |
| | | 105 | | { |
| | 0 | 106 | | store.SaveMany(definitions, GetId); |
| | 0 | 107 | | return Task.CompletedTask; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc /> |
| | | 111 | | public Task<long> DeleteAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default) |
| | | 112 | | { |
| | 0 | 113 | | var workflowDefinitionIds = store.Query(query => Filter(query, filter)).Select(x => x.DefinitionId).Distinct().T |
| | 0 | 114 | | store.DeleteWhere(x => workflowDefinitionIds.Contains(x.DefinitionId)); |
| | 0 | 115 | | return Task.FromResult(workflowDefinitionIds.LongCount()); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <inheritdoc /> |
| | | 119 | | public Task<bool> AnyAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default) |
| | | 120 | | { |
| | 0 | 121 | | var exists = store.Query(query => Filter(query, filter)).Any(); |
| | 0 | 122 | | return Task.FromResult(exists); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <inheritdoc /> |
| | | 126 | | public Task<long> CountDistinctAsync(CancellationToken cancellationToken = default) |
| | | 127 | | { |
| | 0 | 128 | | return Task.FromResult(store.Count(x => true, x => x.DefinitionId)); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | | 132 | | public Task<bool> GetIsNameUnique(string name, string? definitionId = default, CancellationToken cancellationToken = |
| | | 133 | | { |
| | 0 | 134 | | var exists = store.Any(x => x.Name == name && x.DefinitionId != definitionId); |
| | 0 | 135 | | return Task.FromResult(!exists); |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | private IQueryable<WorkflowDefinition> Filter(IQueryable<WorkflowDefinition> queryable, WorkflowDefinitionFilter fil |
| | | 139 | | |
| | 0 | 140 | | private string GetId(WorkflowDefinition workflowDefinition) => workflowDefinition.Id; |
| | | 141 | | } |