| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Common.Services; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Tenants; |
| | | 6 | | |
| | | 7 | | [UsedImplicitly] |
| | 0 | 8 | | public class MemoryTenantStore(MemoryStore<Tenant> store) : ITenantStore |
| | | 9 | | { |
| | | 10 | | public Task<Tenant?> FindAsync(TenantFilter filter, CancellationToken cancellationToken = default) |
| | | 11 | | { |
| | 0 | 12 | | var result = store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 0 | 13 | | return Task.FromResult(result); |
| | | 14 | | } |
| | | 15 | | |
| | | 16 | | public Task<Tenant?> FindAsync(string id, CancellationToken cancellationToken = default) |
| | | 17 | | { |
| | 0 | 18 | | var filter = TenantFilter.ById(id); |
| | 0 | 19 | | return FindAsync(filter, cancellationToken); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public Task<IEnumerable<Tenant>> FindManyAsync(TenantFilter filter, CancellationToken cancellationToken = default) |
| | | 23 | | { |
| | 0 | 24 | | var result = store.Query(query => Filter(query, filter)).ToList().AsEnumerable(); |
| | 0 | 25 | | return Task.FromResult(result); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public Task<IEnumerable<Tenant>> ListAsync(CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | 0 | 30 | | var result = store.List(); |
| | 0 | 31 | | return Task.FromResult(result); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public Task AddAsync(Tenant tenant, CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | 0 | 36 | | store.Add(tenant, GetId); |
| | 0 | 37 | | return Task.CompletedTask; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public Task UpdateAsync(Tenant tenant, CancellationToken cancellationToken = default) |
| | | 41 | | { |
| | 0 | 42 | | store.Update(tenant, GetId); |
| | 0 | 43 | | return Task.CompletedTask; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public Task<bool> DeleteAsync(string id, CancellationToken cancellationToken = default) |
| | | 47 | | { |
| | 0 | 48 | | var found = store.Delete(id); |
| | 0 | 49 | | return Task.FromResult(found); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public Task<long> DeleteAsync(TenantFilter filter, CancellationToken cancellationToken = default) |
| | | 53 | | { |
| | 0 | 54 | | var deletedCount = store.DeleteMany(filter.Apply(store.Queryable), GetId); |
| | 0 | 55 | | return Task.FromResult(deletedCount); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | private IQueryable<Tenant> Filter(IQueryable<Tenant> queryable, TenantFilter filter) => filter.Apply(queryable); |
| | | 59 | | |
| | 0 | 60 | | private string GetId(Tenant tenant) => tenant.Id; |
| | | 61 | | } |