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/Common/Security/Membership/MembershipOps.cs

117 lines
3.3 KiB

2 years ago
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<PrimaryKeyArgs<int>>
{
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<Membership>().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<IDto, ImmutableList<IMembership>>
{
public QueryMembership(IMembershipCache membershipCache)
{
MembershipCache = membershipCache;
}
private IMembershipCache MembershipCache { get; }
protected override async Task<ImmutableList<IMembership>?> OnInvoke()
{
return await (from dc in MembershipCache
select dc).AsEntities<IMembership>();
}
}
internal sealed class SearchMembership : ServiceFunction<MembershipQueryArgs, ImmutableList<IMembership>?>
{
public SearchMembership(IMembershipCache membershipCache)
{
MembershipCache = membershipCache;
}
private IMembershipCache MembershipCache { get; }
protected override async Task<ImmutableList<IMembership>?> 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<IMembership>();
}
}
internal sealed class SelectMembership : ServiceFunction<PrimaryKeyArgs<int>, IMembership?>
{
public SelectMembership(IMembershipCache membershipCache)
{
MembershipCache = membershipCache;
}
private IMembershipCache MembershipCache { get; }
protected override async Task<IMembership?> OnInvoke()
{
return await (from dc in MembershipCache
where dc.Id == Arguments.Id
select dc).AsEntity();
}
}
internal sealed class InsertMembership : ServiceFunction<MembershipArgs, int>
{
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<int> OnInvoke()
{
var entity = Arguments.AsEntity<Membership>(State.New);
var result = await Storage.Open<Membership>().Update(entity);
return result.Id;
}
protected override async Task OnCommitted()
{
await Cache.Refresh(Result);
await Events.Enqueue(this, MembershipService, ServiceEvents.Inserted, Result);
}
}