You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Logistics/Connected.Logistics.Documents/Receive/ReceivePostingDocumentOps.cs

134 lines
4.1 KiB

2 years ago
using System.Collections.Immutable;
using Connected.Caching;
using Connected.Entities;
using Connected.Entities.Storage;
using Connected.Notifications.Events;
using Connected.ServiceModel;
using Connected.Services;
namespace Connected.Logistics.Documents.Receive;
2 years ago
internal sealed class ReceivePostingDocumentOps
{
public sealed class Delete : ServiceAction<PrimaryKeyArgs<int>>
{
public Delete(IStorageProvider storage, ICacheContext cache, IEventService events, IReceivePostingDocumentService documents)
{
Storage = storage;
Cache = cache;
Events = events;
Documents = documents;
}
private IStorageProvider Storage { get; }
private ICacheContext Cache { get; }
private IEventService Events { get; }
private IReceivePostingDocumentService Documents { get; }
protected override async Task OnInvoke()
{
await Storage.Open<ReceivePostingDocument>().Update(Arguments.AsEntity<ReceivePostingDocument>(State.Deleted));
await Cache.Remove(ReceivePostingDocument.EntityKey, Arguments.Id);
}
protected override async Task OnCommitted()
{
await Events.Enqueue(this, Documents, nameof(IReceivePostingDocumentService.Deleted), Arguments);
}
}
public sealed class Insert : ServiceFunction<InsertReceivePostingDocumentArgs, int>
{
public Insert(IStorageProvider storage, IEventService events, IReceivePostingDocumentService documents)
{
Storage = storage;
Events = events;
Documents = documents;
}
private IStorageProvider Storage { get; }
private IEventService Events { get; }
private IReceivePostingDocumentService Documents { get; }
protected override async Task<int> OnInvoke()
{
return (await Storage.Open<ReceivePostingDocument>().Update(Arguments.AsEntity<ReceivePostingDocument>(State.New))).Id;
}
protected override async Task OnCommitted()
{
await Events.Enqueue(this, Documents, nameof(IReceivePostingDocumentService.Inserted), new PrimaryKeyArgs<int> { Id = Result });
}
}
public sealed class Query : ServiceFunction<PrimaryKeyArgs<int>, ImmutableList<IReceivePostingDocument>>
{
public Query(IStorageProvider storage)
{
Storage = storage;
}
private IStorageProvider Storage { get; }
protected override async Task<ImmutableList<IReceivePostingDocument>> OnInvoke()
{
return await (from e in Storage.Open<ReceivePostingDocument>() where e.Document == Arguments.Id select e).AsEntities<IReceivePostingDocument>();
}
}
public sealed class Select : NullableServiceFunction<PrimaryKeyArgs<int>, IReceivePostingDocument>
{
public Select(IStorageProvider storage, ICacheContext cache)
{
Storage = storage;
Cache = cache;
}
private IStorageProvider Storage { get; }
private ICacheContext Cache { get; }
protected override async Task<IReceivePostingDocument?> OnInvoke()
{
return await Cache.Get(ReceivePostingDocument.EntityKey, Arguments.Id, async (f) =>
{
return await (from e in Storage.Open<ReceivePostingDocument>() where e.Id == Arguments.Id select e).AsEntity();
});
}
}
public sealed class Update : ServiceAction<UpdateReceivePostingDocumentArgs>
{
public Update(IStorageProvider storage, ICacheContext cache, IEventService events, IReceivePostingDocumentService documents)
{
Storage = storage;
Cache = cache;
Events = events;
Documents = documents;
}
private IStorageProvider Storage { get; }
private ICacheContext Cache { get; }
private IEventService Events { get; }
private IReceivePostingDocumentService Documents { get; }
protected override async Task OnInvoke()
{
if (await Documents.Select(Arguments.Id) is not ReceivePostingDocument entity)
return;
await Storage.Open<ReceivePostingDocument>().Update(entity, Arguments, async () =>
{
await Cache.Remove(ReceivePostingDocument.EntityKey, Arguments.Id);
return (await Documents.Select(Arguments.Id)) as ReceivePostingDocument;
});
await Cache.Remove(ReceivePostingDocument.EntityKey, Arguments.Id);
}
protected override async Task OnCommitted()
{
await Events.Enqueue(this, Documents, nameof(IReceivePostingDocumentService.Updated), Arguments);
}
}
}