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.

178 lines
4.8 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.Packaging;
2 years ago
internal sealed class PackingOps
{
public sealed class Delete : ServiceAction<PrimaryKeyArgs<int>>
{
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<Packing>().Update(Arguments.AsEntity<Packing>(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<InsertPackingArgs, 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<Packing>().Update(Arguments.AsEntity<Packing>(State.New))).Id;
}
protected override async Task OnCommitted()
{
await Events.Enqueue(this, Events, nameof(IPackingService.Inserted), Arguments);
}
}
public sealed class Query : ServiceFunction<QueryArgs, ImmutableList<IPacking>>
{
public Query(IStorageProvider storage)
{
Storage = storage;
}
private IStorageProvider Storage { get; }
protected override async Task<ImmutableList<IPacking>?> OnInvoke()
{
return await (from e in Storage.Open<Packing>()
select e).WithArguments(Arguments).AsEntities<IPacking>();
2 years ago
}
}
public sealed class Lookup : ServiceFunction<PrimaryKeyListArgs<int>, ImmutableList<IPacking>>
{
public Lookup(IStorageProvider storage)
{
Storage = storage;
}
private IStorageProvider Storage { get; }
protected override async Task<ImmutableList<IPacking>> OnInvoke()
{
return await (from e in Storage.Open<Packing>()
where Arguments.IdList.Any(f => f == e.Id)
select e).AsEntities<IPacking>();
2 years ago
}
}
public sealed class Select : NullableServiceFunction<PrimaryKeyArgs<int>, IPacking>
{
public Select(IStorageProvider storage, ICachingService cache)
{
Storage = storage;
Cache = cache;
}
private IStorageProvider Storage { get; }
private ICachingService Cache { get; }
protected override async Task<IPacking?> OnInvoke()
{
return await Cache.Get<IPacking>(Packing.EntityKey, Arguments.Id, async (f) =>
{
return await (from e in Storage.Open<Packing>()
where e.Id == Arguments.Id
select e).AsEntity();
2 years ago
});
}
}
public sealed class SelectByEan : NullableServiceFunction<SelectPackingArgs, IPacking>
{
public SelectByEan(IStorageProvider storage, ICachingService cache)
{
Storage = storage;
Cache = cache;
}
private IStorageProvider Storage { get; }
private ICachingService Cache { get; }
protected override async Task<IPacking?> OnInvoke()
{
return await Cache.Get<IPacking>(Packing.EntityKey, f => string.Equals(f.Ean, Arguments.Ean, StringComparison.OrdinalIgnoreCase), async (f) =>
{
return await (from e in Storage.Open<Packing>()
where string.Equals(e.Ean, Arguments.Ean, StringComparison.OrdinalIgnoreCase)
select e).AsEntity();
2 years ago
});
}
}
public sealed class Update : ServiceAction<UpdatePackingArgs>
{
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<Packing>().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);
}
}
}