| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Alterations.Core.Contracts; |
| | | 3 | | using Elsa.Alterations.Core.Entities; |
| | | 4 | | using Elsa.Alterations.Core.Filters; |
| | | 5 | | using Elsa.Alterations.Core.Models; |
| | | 6 | | using Open.Linq.AsyncExtensions; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Persistence.EFCore.Modules.Alterations; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// An EF Core implementation of <see cref="IAlterationPlanStore"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | public class EFCoreAlterationJobStore : IAlterationJobStore |
| | | 14 | | { |
| | | 15 | | private readonly EntityStore<AlterationsElsaDbContext, AlterationJob> _store; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Constructor. |
| | | 19 | | /// </summary> |
| | 320 | 20 | | public EFCoreAlterationJobStore(EntityStore<AlterationsElsaDbContext, AlterationJob> store) |
| | | 21 | | { |
| | 320 | 22 | | _store = store; |
| | 320 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public async Task SaveAsync(AlterationJob record, CancellationToken cancellationToken = default) |
| | | 27 | | { |
| | 0 | 28 | | await _store.SaveAsync(record, OnSaveAsync, cancellationToken); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public async Task SaveManyAsync(IEnumerable<AlterationJob> jobs, CancellationToken cancellationToken = default) |
| | | 33 | | { |
| | 0 | 34 | | await _store.SaveManyAsync(jobs, OnSaveAsync, cancellationToken); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public async Task<AlterationJob?> FindAsync(AlterationJobFilter filter, CancellationToken cancellationToken = defaul |
| | | 39 | | { |
| | 0 | 40 | | return await _store.FindAsync(filter.Apply, OnLoadAsync, cancellationToken); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public async Task<IEnumerable<AlterationJob>> FindManyAsync(AlterationJobFilter filter, CancellationToken cancellati |
| | | 45 | | { |
| | 0 | 46 | | return await _store.QueryAsync(filter.Apply, OnLoadAsync, cancellationToken).ToList(); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public async Task<IEnumerable<string>> FindManyIdsAsync(AlterationJobFilter filter, CancellationToken cancellationTo |
| | | 51 | | { |
| | 0 | 52 | | return await _store.QueryAsync(filter.Apply, x => x.Id, cancellationToken); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public async Task<long> CountAsync(AlterationJobFilter filter, CancellationToken cancellationToken = default) |
| | | 57 | | { |
| | 0 | 58 | | return await _store.CountAsync(queryable => Filter(queryable, filter), cancellationToken); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static ValueTask OnSaveAsync(AlterationsElsaDbContext elsaDbContext, AlterationJob entity, CancellationToken |
| | | 62 | | { |
| | 0 | 63 | | elsaDbContext.Entry(entity).Property("SerializedLog").CurrentValue = JsonSerializer.Serialize(entity.Log); |
| | 0 | 64 | | return default; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | private static ValueTask OnLoadAsync(AlterationsElsaDbContext elsaDbContext, AlterationJob? entity, CancellationToke |
| | | 68 | | { |
| | 0 | 69 | | if (entity is null) |
| | 0 | 70 | | return default; |
| | | 71 | | |
| | 0 | 72 | | var logJson = elsaDbContext.Entry(entity).Property<string>("SerializedLog").CurrentValue; |
| | 0 | 73 | | entity.Log = JsonSerializer.Deserialize<AlterationLogEntry[]>(logJson)!; |
| | | 74 | | |
| | 0 | 75 | | return default; |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | private static IQueryable<AlterationJob> Filter(IQueryable<AlterationJob> queryable, AlterationJobFilter filter) => |
| | | 79 | | } |