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?> { public QueryCountries(ICountryCache cache) { Cache = cache; } private ICountryCache Cache { get; } protected override async Task?> OnInvoke() { return await (from dc in Cache select dc).AsEntities(); } } internal sealed class SelectCountry : ServiceFunction, ICountry?> { public SelectCountry(ICountryCache cache) { Cache = cache; } private ICountryCache Cache { get; } protected override async Task OnInvoke() { return await (from dc in Cache where dc.Id == Arguments.Id select dc).AsEntity(); } } internal sealed class SelectCountryByName : ServiceFunction { public SelectCountryByName(ICountryCache cache) { Cache = cache; } private ICountryCache Cache { get; } protected override async Task OnInvoke() { return await (from dc in Cache where string.Equals(dc.Name, Arguments.Name, StringComparison.OrdinalIgnoreCase) select dc).AsEntity(); } } internal sealed class LookupCountries : ServiceFunction, ImmutableList?> { public LookupCountries(ICountryCache cache) { Cache = cache; } private ICountryCache Cache { get; } protected override async Task?> OnInvoke() { if (Arguments.IdList is null) return null; return await (from dc in Cache where Arguments.IdList.Contains(dc.Id) select dc).AsEntities(); } } internal sealed class DeleteCountry : ServiceAction> { 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().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 { 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 OnInvoke() { var entity = Arguments.AsEntity(State.New); var result = await Database.Open().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 { 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().Update(await Load(), Arguments, async () => { await Cache.Refresh(Arguments.Id); return await Load(); }); } private async Task 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); } }