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/Logistics.Documents/Receive/ReceivePostingItemOps.cs

71 lines
2.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 Logistics.Documents.Receive;
internal sealed class ReceivePostingItemOps
{
public sealed class Insert : ServiceFunction<InsertReceivePostingItemArgs, long>
{
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<long> OnInvoke()
{
return (await Storage.Open<ReceivePostingItem>().Update(Arguments.AsEntity<ReceivePostingItem>(State.New))).Id;
}
protected override async Task OnCommitted()
{
await Events.Enqueue(this, Documents, nameof(IReceivePostingDocumentService.ItemInserted), new PrimaryKeyArgs<long> { Id = Result });
}
}
public sealed class Query : ServiceFunction<PrimaryKeyArgs<int>, ImmutableList<IReceivePostingItem>>
{
public Query(IStorageProvider storage)
{
Storage = storage;
}
private IStorageProvider Storage { get; }
protected override async Task<ImmutableList<IReceivePostingItem>> OnInvoke()
{
return await (from e in Storage.Open<ReceivePostingItem>() where e.Document == Arguments.Id select e).AsEntities<IReceivePostingItem>();
}
}
public sealed class Select : NullableServiceFunction<PrimaryKeyArgs<long>, IReceivePostingItem>
{
public Select(IStorageProvider storage, ICacheContext cache)
{
Storage = storage;
Cache = cache;
}
private IStorageProvider Storage { get; }
private ICacheContext Cache { get; }
protected override async Task<IReceivePostingItem?> OnInvoke()
{
return await Cache.Get(ReceivePostingItem.EntityKey, Arguments.Id, async (f) =>
{
return await (from e in Storage.Open<ReceivePostingItem>() where e.Id == Arguments.Id select e).AsEntity();
});
}
}
}