| | | 1 | | using Elsa.Common.Services; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Entities; |
| | | 4 | | using Elsa.Identity.Models; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Identity.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents an in-memory role store. |
| | | 10 | | /// </summary> |
| | | 11 | | public class MemoryRoleStore : IRoleStore |
| | | 12 | | { |
| | | 13 | | private readonly MemoryStore<Role> _store; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="MemoryRoleStore"/> class. |
| | | 17 | | /// </summary> |
| | 0 | 18 | | public MemoryRoleStore(MemoryStore<Role> store) |
| | | 19 | | { |
| | 0 | 20 | | _store = store; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public Task AddAsync(Role role, CancellationToken cancellationToken = default) |
| | | 25 | | { |
| | 0 | 26 | | _store.Save(role, x => x.Id); |
| | 0 | 27 | | return Task.CompletedTask; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public Task DeleteAsync(RoleFilter filter, CancellationToken cancellationToken = default) |
| | | 32 | | { |
| | 0 | 33 | | var ids = _store.Query(query => Filter(query, filter)).Select(x => x.Id).Distinct().ToList(); |
| | 0 | 34 | | _store.DeleteWhere(x => ids.Contains(x.Id)); |
| | 0 | 35 | | return Task.CompletedTask; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public Task SaveAsync(Role role, CancellationToken cancellationToken = default) |
| | | 40 | | { |
| | 0 | 41 | | _store.Save(role, x => x.Id); |
| | 0 | 42 | | return Task.CompletedTask; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public Task<Role?> FindAsync(RoleFilter filter, CancellationToken cancellationToken = default) |
| | | 47 | | { |
| | 0 | 48 | | var result = _store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 0 | 49 | | return Task.FromResult(result); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public Task<IEnumerable<Role>> FindManyAsync(RoleFilter filter, CancellationToken cancellationToken = default) |
| | | 54 | | { |
| | 0 | 55 | | var result = _store.Query(query => Filter(query, filter)).ToList().AsEnumerable(); |
| | 0 | 56 | | return Task.FromResult(result); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | private IQueryable<Role> Filter(IQueryable<Role> queryable, RoleFilter filter) => filter.Apply(queryable); |
| | | 60 | | } |