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.Types.Packaging; internal sealed class PackingOps { public sealed class Delete : ServiceAction> { public Delete(IStorageProvider storage, IPackingService packingService, IEventService events, ICachingService cache) { Storage = storage; PackingService = packingService; Events = events; Cache = cache; } private IStorageProvider Storage { get; } private IPackingService PackingService { get; } private IEventService Events { get; } private ICachingService Cache { get; } protected override async Task OnInvoke() { if (await PackingService.Select(Arguments.Id) is not IPacking packing) return; SetState(packing); await Storage.Open().Update(Arguments.AsEntity(State.Deleted)); } protected override async Task OnCommitted() { await Cache.Remove(Packing.EntityKey, Arguments.Id); await Events.Enqueue(this, Events, nameof(IPackingService.Deleted), Arguments); } } public sealed class Insert : ServiceFunction { public Insert(IStorageProvider storage, IEventService events) { Storage = storage; Events = events; } private IStorageProvider Storage { get; } private IEventService Events { get; } protected override async Task OnInvoke() { return (await Storage.Open().Update(Arguments.AsEntity(State.New))).Id; } protected override async Task OnCommitted() { await Events.Enqueue(this, Events, nameof(IPackingService.Inserted), Arguments); } } public sealed class Query : ServiceFunction> { public Query(IStorageProvider storage) { Storage = storage; } private IStorageProvider Storage { get; } protected override async Task?> OnInvoke() { return await (from e in Storage.Open() select e).WithArguments(Arguments).AsEntities(); } } public sealed class Lookup : ServiceFunction, ImmutableList> { public Lookup(IStorageProvider storage) { Storage = storage; } private IStorageProvider Storage { get; } protected override async Task> OnInvoke() { return await (from e in Storage.Open() where Arguments.IdList.Any(f => f == e.Id) select e).AsEntities(); } } public sealed class Select : NullableServiceFunction, IPacking> { public Select(IStorageProvider storage, ICachingService cache) { Storage = storage; Cache = cache; } private IStorageProvider Storage { get; } private ICachingService Cache { get; } protected override async Task OnInvoke() { return await Cache.Get(Packing.EntityKey, Arguments.Id, async (f) => { return await (from e in Storage.Open() where e.Id == Arguments.Id select e).AsEntity(); }); } } public sealed class SelectByEan : NullableServiceFunction { public SelectByEan(IStorageProvider storage, ICachingService cache) { Storage = storage; Cache = cache; } private IStorageProvider Storage { get; } private ICachingService Cache { get; } protected override async Task OnInvoke() { return await Cache.Get(Packing.EntityKey, f => string.Equals(f.Ean, Arguments.Ean, StringComparison.OrdinalIgnoreCase), async (f) => { return await (from e in Storage.Open() where string.Equals(e.Ean, Arguments.Ean, StringComparison.OrdinalIgnoreCase) select e).AsEntity(); }); } } public sealed class Update : ServiceAction { public Update(IStorageProvider storage, ICachingService cache, IPackingService packingService, IEventService events) { Storage = storage; Cache = cache; PackingService = packingService; Events = events; } private IStorageProvider Storage { get; } private ICachingService Cache { get; } private IPackingService PackingService { get; } private IEventService Events { get; } protected override async Task OnInvoke() { if (SetState(await PackingService.Select(Arguments.Id)) is not Packing entity) return; await Storage.Open().Update(entity.Merge(Arguments, State.Default), Arguments, async () => { await Cache.Remove(Packing.EntityKey, Arguments.Id); return SetState(await PackingService.Select(Arguments.Id)) as Packing; }); } protected override async Task OnCommitted() { await Cache.Remove(Packing.EntityKey, Arguments.Id); await Events.Enqueue(this, PackingService, nameof(PackingService.Updated), Arguments); } } }