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.

151 lines
3.9 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 Connected.Logistics.Types.Warehouses;
2 years ago
internal sealed class WarehouseOps
{
public sealed class Delete : ServiceAction<PrimaryKeyArgs<int>>
{
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<Warehouse>().Update(Arguments.AsEntity<Warehouse>(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<InsertWarehouseArgs, int>
{
public Insert(IStorageProvider storage, IEventService events)
{
Storage = storage;
Events = events;
}
private IStorageProvider Storage { get; }
private IEventService Events { get; }
protected override async Task<int> OnInvoke()
{
return (await Storage.Open<Warehouse>().Update(Arguments.AsEntity<Warehouse>(State.New))).Id;
}
protected override async Task OnCommitted()
{
await Events.Enqueue(this, Events, nameof(IWarehouseService.Inserted), Arguments);
}
}
public sealed class Query : ServiceFunction<QueryArgs, ImmutableList<IWarehouse>>
{
public Query(IWarehouseCache warehouses)
{
Warehouses = warehouses;
}
public IWarehouseCache Warehouses { get; }
protected override async Task<ImmutableList<IWarehouse>> OnInvoke()
{
return await (from e in Warehouses
select e).WithArguments(Arguments).AsEntities<IWarehouse>();
}
}
public sealed class Lookup : ServiceFunction<PrimaryKeyListArgs<int>, ImmutableList<IWarehouse>>
{
public Lookup(IWarehouseCache warehouses)
{
Warehouses = warehouses;
}
public IWarehouseCache Warehouses { get; }
protected override async Task<ImmutableList<IWarehouse>> OnInvoke()
{
return await (from e in Warehouses
where Arguments.IdList.Any(f => f == e.Id)
select e).AsEntities<IWarehouse>();
}
}
public sealed class Select : NullableServiceFunction<PrimaryKeyArgs<int>, IWarehouse>
{
public Select(IWarehouseCache warehouses)
{
Warehouses = warehouses;
}
private IWarehouseCache Warehouses { get; }
protected override async Task<IWarehouse?> OnInvoke()
{
return await (from e in Warehouses
where e.Id == Arguments.Id
select e).AsEntity();
}
}
public sealed class Update : ServiceAction<UpdateWarehouseArgs>
{
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<Warehouse>().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);
}
}
}