|
|
|
|
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 ReceiveDocumentOps
|
|
|
|
|
{
|
|
|
|
|
public sealed class Delete : ServiceAction<PrimaryKeyArgs<int>>
|
|
|
|
|
{
|
|
|
|
|
public Delete(IReceiveDocumentService documents, IStorageProvider storage, ICacheContext cache, IEventService events)
|
|
|
|
|
{
|
|
|
|
|
Documents = documents;
|
|
|
|
|
Storage = storage;
|
|
|
|
|
Cache = cache;
|
|
|
|
|
Events = events;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IReceiveDocumentService Documents { get; }
|
|
|
|
|
private IStorageProvider Storage { get; }
|
|
|
|
|
private ICacheContext Cache { get; }
|
|
|
|
|
private IEventService Events { get; }
|
|
|
|
|
|
|
|
|
|
protected override async Task OnInvoke()
|
|
|
|
|
{
|
|
|
|
|
if (SetState(await Documents.Select(Arguments)) is not IReceiveDocument document)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Delete all items
|
|
|
|
|
*/
|
|
|
|
|
foreach (var item in await Documents.QueryItems(document.Id))
|
|
|
|
|
await Documents.DeleteItem(item.Id);
|
|
|
|
|
/*
|
|
|
|
|
* Delete document
|
|
|
|
|
*/
|
|
|
|
|
await Storage.Open<ReceiveDocument>().Update(Arguments.AsEntity<ReceiveDocument>(State.Deleted));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnCommitted()
|
|
|
|
|
{
|
|
|
|
|
await Cache.Remove(ReceiveDocument.EntityKey, Arguments.Id);
|
|
|
|
|
await Events.Enqueue(this, Documents, nameof(IReceiveDocumentService.Deleted), Arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class Insert : ServiceFunction<InsertReceiveDocumentArgs, int>
|
|
|
|
|
{
|
|
|
|
|
public Insert(IStorageProvider storage, IEventService events, IReceiveDocumentService documents)
|
|
|
|
|
{
|
|
|
|
|
Storage = storage;
|
|
|
|
|
Events = events;
|
|
|
|
|
Documents = documents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IStorageProvider Storage { get; }
|
|
|
|
|
private IEventService Events { get; }
|
|
|
|
|
private IReceiveDocumentService Documents { get; }
|
|
|
|
|
|
|
|
|
|
protected override async Task<int> OnInvoke()
|
|
|
|
|
{
|
|
|
|
|
return (await Storage.Open<ReceiveDocument>().Update(Arguments.AsEntity<ReceiveDocument>(State.New))).Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnCommitted()
|
|
|
|
|
{
|
|
|
|
|
await Events.Enqueue(this, Documents, nameof(IReceiveDocumentService.Inserted), new PrimaryKeyArgs<int> { Id = Result });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class Query : ServiceFunction<QueryArgs, ImmutableList<IReceiveDocument>>
|
|
|
|
|
{
|
|
|
|
|
public Query(IStorageProvider storage)
|
|
|
|
|
{
|
|
|
|
|
Storage = storage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IStorageProvider Storage { get; }
|
|
|
|
|
|
|
|
|
|
protected override async Task<ImmutableList<IReceiveDocument>> OnInvoke()
|
|
|
|
|
{
|
|
|
|
|
return await (from e in Storage.Open<ReceiveDocument>() select e).WithArguments(Arguments).AsEntities<IReceiveDocument>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class Select : NullableServiceFunction<PrimaryKeyArgs<int>, IReceiveDocument?>
|
|
|
|
|
{
|
|
|
|
|
public Select(IStorageProvider storage, ICacheContext cache)
|
|
|
|
|
{
|
|
|
|
|
Storage = storage;
|
|
|
|
|
Cache = cache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IStorageProvider Storage { get; }
|
|
|
|
|
private ICacheContext Cache { get; }
|
|
|
|
|
|
|
|
|
|
protected override async Task<IReceiveDocument?> OnInvoke()
|
|
|
|
|
{
|
|
|
|
|
return await Cache.Get<ReceiveDocument>(ReceiveDocument.EntityKey, Arguments.Id, async (f) =>
|
|
|
|
|
{
|
|
|
|
|
return await (from e in Storage.Open<ReceiveDocument>() where e.Id == Arguments.Id select e).AsEntity();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class Update : ServiceAction<UpdateReceiveDocumentArgs>
|
|
|
|
|
{
|
|
|
|
|
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.Select(Arguments.Id) is not ReceiveDocument entity)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await Storage.Open<ReceiveDocument>().Update(entity, Arguments, async () =>
|
|
|
|
|
{
|
|
|
|
|
await Cache.Remove(ReceiveDocument.EntityKey, Arguments.Id);
|
|
|
|
|
|
|
|
|
|
return (await Documents.Select(Arguments.Id)) as ReceiveDocument;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnCommitted()
|
|
|
|
|
{
|
|
|
|
|
await Cache.Remove(ReceiveDocument.EntityKey, Arguments.Id);
|
|
|
|
|
await Events.Enqueue(this, Documents, nameof(IReceiveDocumentService.Updated), Arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|