using System.Collections.Immutable; using Connected; using Connected.Entities; using Connected.Entities.Storage; using Connected.Notifications.Events; using Connected.Security.Membership; using Connected.ServiceModel; using Connected.Services; namespace Common.Security.Membership; internal sealed class DeleteMembership : ServiceAction> { public DeleteMembership(IMembershipService membershipService, IStorageProvider storage, IMembershipCache cache, IEventService events) { MembershipService = membershipService; Storage = storage; Cache = cache; Events = events; } private IMembershipService MembershipService { get; } private IStorageProvider Storage { get; } private IMembershipCache Cache { get; } private IEventService Events { get; } protected override async Task OnInvoke() { await Storage.Open().Update(new Membership { Id = Arguments.Id, State = State.Deleted }); } protected override async Task OnCommitted() { await Cache.Refresh(Arguments.Id); await Events.Enqueue(this, MembershipService, ServiceEvents.Deleted, Arguments.Id); } } internal sealed class QueryMembership : ServiceFunction> { public QueryMembership(IMembershipCache membershipCache) { MembershipCache = membershipCache; } private IMembershipCache MembershipCache { get; } protected override async Task?> OnInvoke() { return await (from dc in MembershipCache select dc).AsEntities(); } } internal sealed class SearchMembership : ServiceFunction?> { public SearchMembership(IMembershipCache membershipCache) { MembershipCache = membershipCache; } private IMembershipCache MembershipCache { get; } protected override async Task?> OnInvoke() { return await (from dc in MembershipCache where (Arguments.User == 0 || (Arguments.User == dc.User)) && (Arguments.Role == 0 || (Arguments.Role == dc.Role)) select dc).AsEntities(); } } internal sealed class SelectMembership : ServiceFunction, IMembership?> { public SelectMembership(IMembershipCache membershipCache) { MembershipCache = membershipCache; } private IMembershipCache MembershipCache { get; } protected override async Task OnInvoke() { return await (from dc in MembershipCache where dc.Id == Arguments.Id select dc).AsEntity(); } } internal sealed class InsertMembership : ServiceFunction { public InsertMembership(IMembershipService membershipService, IMembershipCache cache, IStorageProvider storage, IEventService events) { MembershipService = membershipService; Cache = cache; Storage = storage; Events = events; } private IMembershipService MembershipService { get; } private IMembershipCache Cache { get; } private IStorageProvider Storage { get; } private IEventService Events { get; } protected override async Task OnInvoke() { var entity = Arguments.AsEntity(State.New); var result = await Storage.Open().Update(entity); return result.Id; } protected override async Task OnCommitted() { await Cache.Refresh(Result); await Events.Enqueue(this, MembershipService, ServiceEvents.Inserted, Result); } }