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.Types.Warehouses; internal sealed class WarehouseOps { public sealed class Delete : ServiceAction> { public Delete(IStorageProvider storage, IWarehouseService warehouses, IEventService events, ICachingService cache) { Storage = storage; Warehouses = warehouses; Events = events; Cache = cache; } private IStorageProvider Storage { get; } private IWarehouseService Warehouses { get; } private IEventService Events { get; } private ICachingService Cache { get; } protected override async Task OnInvoke() { if (await Warehouses.Select(Arguments.Id) is not IWarehouse warehouse) return; SetState(warehouse); await Storage.Open().Update(Arguments.AsEntity(State.Deleted)); } protected override async Task OnCommitted() { await Cache.Remove(Warehouse.EntityKey, Arguments.Id); await Events.Enqueue(this, Events, nameof(IWarehouseService.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(IWarehouseService.Inserted), Arguments); } } public sealed class Query : ServiceFunction> { public Query(IWarehouseCache warehouses) { Warehouses = warehouses; } public IWarehouseCache Warehouses { get; } protected override async Task> OnInvoke() { return await (from e in Warehouses select e).WithArguments(Arguments).AsEntities(); } } public sealed class Lookup : ServiceFunction, ImmutableList> { public Lookup(IWarehouseCache warehouses) { Warehouses = warehouses; } public IWarehouseCache Warehouses { get; } protected override async Task> OnInvoke() { return await (from e in Warehouses where Arguments.IdList.Any(f => f == e.Id) select e).AsEntities(); } } public sealed class Select : NullableServiceFunction, IWarehouse> { public Select(IWarehouseCache warehouses) { Warehouses = warehouses; } private IWarehouseCache Warehouses { get; } protected override async Task OnInvoke() { return await (from e in Warehouses where e.Id == Arguments.Id select e).AsEntity(); } } public sealed class Update : ServiceAction { public Update(IStorageProvider storage, IWarehouseCache cache, IWarehouseService warehouses, IEventService events) { Storage = storage; Cache = cache; Warehouses = warehouses; Events = events; } private IStorageProvider Storage { get; } private IWarehouseCache Cache { get; } private IWarehouseService Warehouses { get; } private IEventService Events { get; } protected override async Task OnInvoke() { if (SetState(await Warehouses.Select(Arguments.Id)) is not Warehouse entity) return; await Storage.Open().Update(entity.Merge(Arguments, State.Default), Arguments, async () => { await Cache.Refresh(Arguments.Id); return SetState(await Warehouses.Select(Arguments.Id)) as Warehouse; }); } protected override async Task OnCommitted() { await Cache.Refresh(Arguments.Id); await Events.Enqueue(this, Warehouses, nameof(Warehouses.Updated), Arguments); } } }