| | | 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 user store. |
| | | 10 | | /// </summary> |
| | | 11 | | public class MemoryUserStore : IUserStore |
| | | 12 | | { |
| | | 13 | | private readonly MemoryStore<User> _store; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="MemoryUserStore"/> class. |
| | | 17 | | /// </summary> |
| | 0 | 18 | | public MemoryUserStore(MemoryStore<User> store) |
| | | 19 | | { |
| | 0 | 20 | | _store = store; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public Task SaveAsync(User user, CancellationToken cancellationToken = default) |
| | | 25 | | { |
| | 0 | 26 | | _store.Save(user, x => x.Id); |
| | 0 | 27 | | return Task.CompletedTask; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public Task DeleteAsync(UserFilter 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<User?> FindAsync(UserFilter filter, CancellationToken cancellationToken = default) |
| | | 40 | | { |
| | 0 | 41 | | var result = _store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 0 | 42 | | return Task.FromResult(result); |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | private IQueryable<User> Filter(IQueryable<User> queryable, UserFilter filter) => filter.Apply(queryable); |
| | | 46 | | } |