| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Common.Services; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Labels.Contracts; |
| | | 5 | | using Elsa.Labels.Entities; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Labels.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// An in-memory store of labels. |
| | | 11 | | /// </summary> |
| | | 12 | | public class InMemoryLabelStore : ILabelStore |
| | | 13 | | { |
| | | 14 | | private readonly MemoryStore<Label> _labelStore; |
| | | 15 | | private readonly MemoryStore<WorkflowDefinitionLabel> _workflowDefinitionLabelStore; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Constructor. |
| | | 19 | | /// </summary> |
| | 0 | 20 | | public InMemoryLabelStore(MemoryStore<Label> labelStore, MemoryStore<WorkflowDefinitionLabel> workflowDefinitionLabe |
| | | 21 | | { |
| | 0 | 22 | | _labelStore = labelStore; |
| | 0 | 23 | | _workflowDefinitionLabelStore = workflowDefinitionLabelStore; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public Task SaveAsync(Label record, CancellationToken cancellationToken = default) |
| | | 28 | | { |
| | 0 | 29 | | _labelStore.Save(record, x => x.Id); |
| | 0 | 30 | | return Task.CompletedTask; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public Task SaveManyAsync(IEnumerable<Label> records, CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | 0 | 36 | | _labelStore.SaveMany(records, x => x.Id); |
| | 0 | 37 | | return Task.CompletedTask; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public Task<bool> DeleteAsync(string id, CancellationToken cancellationToken = default) |
| | | 42 | | { |
| | 0 | 43 | | _workflowDefinitionLabelStore.DeleteWhere(x => x.LabelId == id); |
| | 0 | 44 | | var result = _labelStore.Delete(id); |
| | 0 | 45 | | return Task.FromResult(result); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public Task<long> DeleteManyAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default) |
| | | 50 | | { |
| | 0 | 51 | | var idList = ids.ToList(); |
| | 0 | 52 | | _workflowDefinitionLabelStore.DeleteWhere(x => idList.Contains(x.LabelId)); |
| | 0 | 53 | | var result = _labelStore.DeleteMany(idList); |
| | 0 | 54 | | return Task.FromResult(result); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public Task<Label?> FindByIdAsync(string id, CancellationToken cancellationToken = default) |
| | | 59 | | { |
| | 0 | 60 | | var record = _labelStore.Find(x => x.Id == id); |
| | 0 | 61 | | return Task.FromResult(record); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | public Task<Page<Label>> ListAsync(PageArgs? pageArgs = default, CancellationToken cancellationToken = default) |
| | | 66 | | { |
| | 0 | 67 | | var query = _labelStore.List().AsQueryable().OrderBy(x => x.Name); |
| | 0 | 68 | | var page = query.ToPage(pageArgs); |
| | 0 | 69 | | return Task.FromResult(page); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | | 73 | | public Task<IEnumerable<Label>> FindManyByIdAsync(IEnumerable<string> ids, CancellationToken cancellationToken) |
| | | 74 | | { |
| | 0 | 75 | | var idList = ids.ToList(); |
| | 0 | 76 | | return Task.FromResult(_labelStore.FindMany(x => idList.Contains(x.Id))); |
| | | 77 | | } |
| | | 78 | | } |