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.
163 lines
4.8 KiB
163 lines
4.8 KiB
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;
|
|
internal sealed class ReceiveDocumentItemOps
|
|
{
|
|
public sealed class Insert : ServiceFunction<InsertReceiveItemArgs, long>
|
|
{
|
|
public Insert(IStorageProvider storage, IEventService events)
|
|
{
|
|
Storage = storage;
|
|
Events = events;
|
|
}
|
|
|
|
private IStorageProvider Storage { get; }
|
|
private IEventService Events { get; }
|
|
|
|
protected override async Task<long> OnInvoke()
|
|
{
|
|
var result = await Storage.Open<ReceiveItem>().Update(Arguments.AsEntity<ReceiveItem>(State.New));
|
|
|
|
return result.Id;
|
|
}
|
|
|
|
protected override async Task OnCommitted()
|
|
{
|
|
await Events.Enqueue(this, typeof(ReceiveDocumentService), nameof(IReceiveDocumentService.ItemInserted), new PrimaryKeyArgs<long> { Id = Result });
|
|
}
|
|
}
|
|
|
|
public sealed class Delete : ServiceAction<PrimaryKeyArgs<long>>
|
|
{
|
|
public Delete(IStorageProvider storage, IEventService events, ICacheContext cache, IReceiveDocumentService documents)
|
|
{
|
|
Storage = storage;
|
|
Events = events;
|
|
Cache = cache;
|
|
Documents = documents;
|
|
}
|
|
|
|
private IStorageProvider Storage { get; }
|
|
private IEventService Events { get; }
|
|
private ICacheContext Cache { get; }
|
|
private IReceiveDocumentService Documents { get; }
|
|
|
|
protected override async Task OnInvoke()
|
|
{
|
|
if (SetState(await Documents.SelectItem(Arguments)) is not IReceiveItem entity)
|
|
return;
|
|
|
|
await Storage.Open<ReceiveItem>().Update(Arguments.AsEntity<ReceiveItem>(State.Deleted));
|
|
}
|
|
|
|
protected override async Task OnCommitted()
|
|
{
|
|
await Cache.Remove(ReceiveItem.EntityKey, Arguments.Id);
|
|
await Events.Enqueue(this, Documents, nameof(IReceiveDocumentService.ItemDeleted), Arguments);
|
|
}
|
|
}
|
|
|
|
public sealed class Query : ServiceFunction<PrimaryKeyArgs<int>, ImmutableList<IReceiveItem>>
|
|
{
|
|
public Query(IStorageProvider storage)
|
|
{
|
|
Storage = storage;
|
|
}
|
|
|
|
private IStorageProvider Storage { get; }
|
|
|
|
protected override async Task<ImmutableList<IReceiveItem>> OnInvoke()
|
|
{
|
|
return await (from e in Storage.Open<ReceiveItem>() where e.Document == Arguments.Id select e).AsEntities<IReceiveItem>();
|
|
}
|
|
}
|
|
|
|
public sealed class Select : NullableServiceFunction<PrimaryKeyArgs<long>, IReceiveItem?>
|
|
{
|
|
public Select(IStorageProvider storage, ICacheContext cache)
|
|
{
|
|
Storage = storage;
|
|
Cache = cache;
|
|
}
|
|
|
|
private IStorageProvider Storage { get; }
|
|
private ICacheContext Cache { get; }
|
|
|
|
protected override async Task<IReceiveItem?> OnInvoke()
|
|
{
|
|
return await Cache.Get(ReceiveItem.EntityKey, Arguments.Id, async (f) =>
|
|
{
|
|
return await (from e in Storage.Open<ReceiveItem>() where e.Id == Arguments.Id select e).AsEntity();
|
|
});
|
|
}
|
|
}
|
|
|
|
public sealed class SelectByEntity : NullableServiceFunction<SelectReceiveItemArgs, IReceiveItem?>
|
|
{
|
|
public SelectByEntity(IStorageProvider storage, ICacheContext cache)
|
|
{
|
|
Storage = storage;
|
|
Cache = cache;
|
|
}
|
|
|
|
private IStorageProvider Storage { get; }
|
|
private ICacheContext Cache { get; }
|
|
|
|
protected override async Task<IReceiveItem?> OnInvoke()
|
|
{
|
|
return await Cache.Get(ReceiveItem.EntityKey,
|
|
f => f.Document == Arguments.Document
|
|
&& string.Equals(f.Entity, Arguments.Entity, StringComparison.OrdinalIgnoreCase)
|
|
&& string.Equals(f.EntityId, Arguments.EntityId, StringComparison.OrdinalIgnoreCase), async (f) =>
|
|
{
|
|
return await (from e in Storage.Open<ReceiveItem>()
|
|
where e.Document == Arguments.Document
|
|
&& string.Equals(e.Entity, Arguments.Entity, StringComparison.OrdinalIgnoreCase)
|
|
&& string.Equals(e.EntityId, Arguments.EntityId, StringComparison.OrdinalIgnoreCase)
|
|
select e).AsEntity();
|
|
});
|
|
}
|
|
}
|
|
|
|
public sealed class Update : ServiceAction<UpdateReceiveItemArgs>
|
|
{
|
|
public Update(IStorageProvider storage, ICacheContext cache, IEventService events, IReceiveDocumentService documents)
|
|
{
|
|
Storage = storage;
|
|
Cache = cache;
|
|
Events = events;
|
|
Documents = documents;
|
|
}
|
|
|
|
private IStorageProvider Storage { get; }
|
|
private ICacheContext Cache { get; }
|
|
private IEventService Events { get; }
|
|
private IReceiveDocumentService Documents { get; }
|
|
|
|
protected override async Task OnInvoke()
|
|
{
|
|
if (await Documents.SelectItem(Arguments.Id) is not ReceiveItem entity)
|
|
return;
|
|
|
|
await Storage.Open<ReceiveItem>().Update(entity, Arguments, async () =>
|
|
{
|
|
await Cache.Remove(ReceiveItem.EntityKey, Arguments.Id);
|
|
|
|
return (await Documents.SelectItem(Arguments.Id)) as ReceiveItem;
|
|
});
|
|
}
|
|
|
|
protected override async Task OnCommitted()
|
|
{
|
|
await Cache.Remove(ReceiveItem.EntityKey, Arguments.Id);
|
|
await Events.Enqueue(this, Documents, nameof(IReceiveDocumentService.ItemUpdated), Arguments);
|
|
}
|
|
}
|
|
}
|