| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Common.Multitenancy; |
| | | 3 | | using Elsa.Common.Services; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Runtime.Entities; |
| | | 6 | | using Elsa.Workflows.Runtime.Filters; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Runtime.Stores; |
| | | 10 | | |
| | | 11 | | /// <inheritdoc /> |
| | | 12 | | [UsedImplicitly] |
| | 499 | 13 | | public class MemoryBookmarkStore(MemoryStore<StoredBookmark> store, ITenantAccessor? tenantAccessor = null) : IBookmarkS |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public ValueTask SaveAsync(StoredBookmark record, CancellationToken cancellationToken = default) |
| | | 17 | | { |
| | 144 | 18 | | store.Save(record, x => x.Id); |
| | 72 | 19 | | return ValueTask.CompletedTask; |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public ValueTask SaveManyAsync(IEnumerable<StoredBookmark> records, CancellationToken cancellationToken) |
| | | 24 | | { |
| | 0 | 25 | | store.SaveMany(records, x => x.Id); |
| | 0 | 26 | | return ValueTask.CompletedTask; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public ValueTask<StoredBookmark?> FindAsync(BookmarkFilter filter, CancellationToken cancellationToken = default) |
| | | 31 | | { |
| | 12 | 32 | | var entity = store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 6 | 33 | | return new(entity); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public ValueTask<IEnumerable<StoredBookmark>> FindManyAsync(BookmarkFilter filter, CancellationToken cancellationTok |
| | | 38 | | { |
| | 182 | 39 | | var entities = store.Query(query => Filter(query, filter)).AsEnumerable(); |
| | 91 | 40 | | return new(entities); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public ValueTask<Page<StoredBookmark>> FindManyAsync(BookmarkFilter filter, PageArgs pageArgs, CancellationToken can |
| | | 45 | | { |
| | 160 | 46 | | var count = store.Query(query => Filter(query, filter)).LongCount(); |
| | 160 | 47 | | var result = store.Query(query => Filter(query, filter).OrderBy(x => x.Id).Paginate(pageArgs)).ToList(); |
| | 80 | 48 | | return new(Page.Of(result, count)); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public async ValueTask<long> DeleteAsync(BookmarkFilter filter, CancellationToken cancellationToken = default) |
| | | 53 | | { |
| | 54 | 54 | | var ids = (await FindManyAsync(filter, cancellationToken)).Select(x => x.Id); |
| | 24 | 55 | | return store.DeleteMany(ids); |
| | 24 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <remarks> |
| | | 59 | | /// Ambient tenant is applied here rather than in <see cref="BookmarkFilter.Apply"/>. |
| | | 60 | | /// EF owns that via <c>SetTenantIdFilter</c> / <c>IgnoreQueryFilters</c>; Memory must compensate. |
| | | 61 | | /// </remarks> |
| | | 62 | | private IQueryable<StoredBookmark> Filter(IQueryable<StoredBookmark> query, BookmarkFilter filter) => |
| | 257 | 63 | | filter.Apply(query.WhereVisibleToTenant(CurrentTenantId, filter.TenantAgnostic)); |
| | | 64 | | |
| | 257 | 65 | | private string CurrentTenantId => tenantAccessor?.TenantId ?? Tenant.DefaultTenantId; |
| | | 66 | | } |