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.
Connected.Common.Types/Common.Types/Countries/CountryOps.cs

157 lines
4.3 KiB

using System.Collections.Immutable;
using Connected;
using Connected.Entities;
using Connected.Entities.Storage;
using Connected.Notifications.Events;
using Connected.ServiceModel;
using Connected.Services;
namespace Common.Types.Countries;
internal sealed class QueryCountries : ServiceFunction<IDto, ImmutableList<ICountry>?>
{
public QueryCountries(ICountryCache cache)
{
Cache = cache;
}
private ICountryCache Cache { get; }
protected override async Task<ImmutableList<ICountry>?> OnInvoke()
{
return await (from dc in Cache select dc).AsEntities<ICountry>();
}
}
internal sealed class SelectCountry : ServiceFunction<PrimaryKeyArgs<int>, ICountry?>
{
public SelectCountry(ICountryCache cache)
{
Cache = cache;
}
private ICountryCache Cache { get; }
protected override async Task<ICountry?> OnInvoke()
{
return await (from dc in Cache where dc.Id == Arguments.Id select dc).AsEntity();
}
}
internal sealed class SelectCountryByName : ServiceFunction<NameArgs, ICountry?>
{
public SelectCountryByName(ICountryCache cache)
{
Cache = cache;
}
private ICountryCache Cache { get; }
protected override async Task<ICountry?> OnInvoke()
{
return await (from dc in Cache where string.Equals(dc.Name, Arguments.Name, StringComparison.OrdinalIgnoreCase) select dc).AsEntity();
}
}
internal sealed class LookupCountries : ServiceFunction<PrimaryKeyListArgs<int>, ImmutableList<ICountry>?>
{
public LookupCountries(ICountryCache cache)
{
Cache = cache;
}
private ICountryCache Cache { get; }
protected override async Task<ImmutableList<ICountry>?> OnInvoke()
{
if (Arguments.IdList is null)
return null;
return await (from dc in Cache where Arguments.IdList.Contains(dc.Id) select dc).AsEntities<ICountry>();
}
}
internal sealed class DeleteCountry : ServiceAction<PrimaryKeyArgs<int>>
{
public DeleteCountry(ICountryService countryService, IStorageProvider storage, ICountryCache cache, IEventService events)
{
CountryService = countryService;
Storage = storage;
Cache = cache;
Events = events;
}
private ICountryService CountryService { get; }
private IStorageProvider Storage { get; }
private ICountryCache Cache { get; }
private IEventService Events { get; }
protected override async Task OnInvoke()
{
await Storage.Open<Country>().Update(new Country { Id = Arguments.Id, State = State.Deleted });
}
protected override async Task OnCommitted()
{
await Cache.Remove(Arguments.Id);
await Events.Enqueue(this, CountryService, ServiceEvents.Deleted, Arguments.Id);
}
}
internal sealed class InsertCountry : ServiceFunction<InsertCountryArgs, int>
{
public InsertCountry(ICountryService countryService, IStorageProvider database, IEventService events, ICountryCache cache)
{
CountryService = countryService;
Database = database;
Events = events;
Cache = cache;
}
private ICountryService CountryService { get; }
private IStorageProvider Database { get; }
private IEventService Events { get; }
private ICountryCache Cache { get; }
protected override async Task<int> OnInvoke()
{
var entity = Arguments.AsEntity<Country>(State.New);
var result = await Database.Open<Country>().Update(entity);
return result is null ? 0 : result.Id;
}
protected override async Task OnCommitted()
{
await Cache.Refresh(Result);
await Events.Enqueue(this, CountryService, ServiceEvents.Inserted, Result);
}
}
internal sealed class UpdateCountry : ServiceAction<UpdateCountryArgs>
{
public UpdateCountry(ICountryService countryService, IStorageProvider database, ICountryCache cache, IEventService events)
{
CountryService = countryService;
Database = database;
Cache = cache;
Events = events;
}
private IStorageProvider Database { get; }
private ICountryCache Cache { get; }
private IEventService Events { get; }
private ICountryService CountryService { get; }
protected override async Task OnInvoke()
{
await Database.Open<Country>().Update(await Load(), Arguments, async () =>
{
await Cache.Refresh(Arguments.Id);
return await Load();
});
}
private async Task<Country?> Load() => await (from dc in Cache where dc.Id == Arguments.Id select dc).AsEntity();
protected override async Task OnCommitted()
{
await Cache.Refresh(Arguments.Id);
await Events.Enqueue(this, CountryService, ServiceEvents.Updated, Arguments.Id);
}
}