mirror of https://github.com/Squidex/squidex.git
17 changed files with 417 additions and 514 deletions
@ -1,200 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
||||
// All rights reserved. Licensed under the MIT license.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System; |
|
||||
using System.Threading.Tasks; |
|
||||
using Squidex.Domain.Apps.Entities.Apps.Commands; |
|
||||
using Squidex.Domain.Apps.Entities.Apps.Guards; |
|
||||
using Squidex.Domain.Apps.Entities.Apps.Services; |
|
||||
using Squidex.Infrastructure; |
|
||||
using Squidex.Infrastructure.Commands; |
|
||||
using Squidex.Infrastructure.Dispatching; |
|
||||
using Squidex.Shared.Users; |
|
||||
|
|
||||
namespace Squidex.Domain.Apps.Entities.Apps |
|
||||
{ |
|
||||
public class AppCommandMiddleware : ICommandMiddleware |
|
||||
{ |
|
||||
private readonly IAggregateHandler handler; |
|
||||
private readonly IAppProvider appProvider; |
|
||||
private readonly IAppPlansProvider appPlansProvider; |
|
||||
private readonly IAppPlanBillingManager appPlansBillingManager; |
|
||||
private readonly IUserResolver userResolver; |
|
||||
|
|
||||
public AppCommandMiddleware( |
|
||||
IAggregateHandler handler, |
|
||||
IAppProvider appProvider, |
|
||||
IAppPlansProvider appPlansProvider, |
|
||||
IAppPlanBillingManager appPlansBillingManager, |
|
||||
IUserResolver userResolver) |
|
||||
{ |
|
||||
Guard.NotNull(handler, nameof(handler)); |
|
||||
Guard.NotNull(appProvider, nameof(appProvider)); |
|
||||
Guard.NotNull(userResolver, nameof(userResolver)); |
|
||||
Guard.NotNull(appPlansProvider, nameof(appPlansProvider)); |
|
||||
Guard.NotNull(appPlansBillingManager, nameof(appPlansBillingManager)); |
|
||||
|
|
||||
this.handler = handler; |
|
||||
this.userResolver = userResolver; |
|
||||
this.appProvider = appProvider; |
|
||||
this.appPlansProvider = appPlansProvider; |
|
||||
this.appPlansBillingManager = appPlansBillingManager; |
|
||||
} |
|
||||
|
|
||||
protected async Task On(CreateApp command, CommandContext context) |
|
||||
{ |
|
||||
var app = await handler.CreateSyncedAsync<AppDomainObject>(context, async a => |
|
||||
{ |
|
||||
await GuardApp.CanCreate(command, appProvider); |
|
||||
|
|
||||
a.Create(command); |
|
||||
|
|
||||
context.Complete(EntityCreatedResult.Create(command.AppId, a.Version)); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(AssignContributor command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, async a => |
|
||||
{ |
|
||||
await GuardAppContributors.CanAssign(a.Snapshot.Contributors, command, userResolver, appPlansProvider.GetPlan(a.Snapshot.Plan?.PlanId)); |
|
||||
|
|
||||
a.AssignContributor(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(RemoveContributor command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppContributors.CanRemove(a.Snapshot.Contributors, command); |
|
||||
|
|
||||
a.RemoveContributor(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(AttachClient command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppClients.CanAttach(a.Snapshot.Clients, command); |
|
||||
|
|
||||
a.AttachClient(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(UpdateClient command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppClients.CanUpdate(a.Snapshot.Clients, command); |
|
||||
|
|
||||
a.UpdateClient(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(RevokeClient command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppClients.CanRevoke(a.Snapshot.Clients, command); |
|
||||
|
|
||||
a.RevokeClient(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(AddLanguage command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppLanguages.CanAdd(a.Snapshot.LanguagesConfig, command); |
|
||||
|
|
||||
a.AddLanguage(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(RemoveLanguage command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppLanguages.CanRemove(a.Snapshot.LanguagesConfig, command); |
|
||||
|
|
||||
a.RemoveLanguage(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(UpdateLanguage command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppLanguages.CanUpdate(a.Snapshot.LanguagesConfig, command); |
|
||||
|
|
||||
a.UpdateLanguage(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(AddPattern command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppPattern.CanAdd(a.Snapshot.Patterns, command); |
|
||||
|
|
||||
a.AddPattern(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(DeletePattern command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppPattern.CanDelete(a.Snapshot.Patterns, command); |
|
||||
|
|
||||
a.DeletePattern(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected async Task On(UpdatePattern command, CommandContext context) |
|
||||
{ |
|
||||
await handler.UpdateSyncedAsync<AppDomainObject>(context, a => |
|
||||
{ |
|
||||
GuardAppPattern.CanUpdate(a.Snapshot.Patterns, command); |
|
||||
|
|
||||
a.UpdatePattern(command); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
protected Task On(ChangePlan command, CommandContext context) |
|
||||
{ |
|
||||
return handler.UpdateSyncedAsync<AppDomainObject>(context, async a => |
|
||||
{ |
|
||||
GuardApp.CanChangePlan(command, a.Snapshot.Plan, appPlansProvider); |
|
||||
|
|
||||
if (command.FromCallback) |
|
||||
{ |
|
||||
a.ChangePlan(command); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
var result = await appPlansBillingManager.ChangePlanAsync(command.Actor.Identifier, a.Snapshot.Id, a.Snapshot.Name, command.PlanId); |
|
||||
|
|
||||
if (result is PlanChangedResult) |
|
||||
{ |
|
||||
a.ChangePlan(command); |
|
||||
} |
|
||||
|
|
||||
context.Complete(result); |
|
||||
} |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
public async Task HandleAsync(CommandContext context, Func<Task> next) |
|
||||
{ |
|
||||
await this.DispatchActionAsync(context.Command, context); |
|
||||
await next(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,228 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
||||
// All rights reserved. Licensed under the MIT license.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
using Squidex.Domain.Apps.Core.Apps; |
|
||||
using Squidex.Domain.Apps.Entities.Apps.Commands; |
|
||||
using Squidex.Domain.Apps.Entities.Apps.State; |
|
||||
using Squidex.Domain.Apps.Events; |
|
||||
using Squidex.Domain.Apps.Events.Apps; |
|
||||
using Squidex.Infrastructure; |
|
||||
using Squidex.Infrastructure.EventSourcing; |
|
||||
using Squidex.Infrastructure.Reflection; |
|
||||
|
|
||||
namespace Squidex.Domain.Apps.Entities.Apps |
|
||||
{ |
|
||||
public sealed class AppDomainObject : SquidexDomainObjectBase<AppState> |
|
||||
{ |
|
||||
private readonly InitialPatterns initialPatterns; |
|
||||
|
|
||||
public AppDomainObject(InitialPatterns initialPatterns) |
|
||||
{ |
|
||||
Guard.NotNull(initialPatterns, nameof(initialPatterns)); |
|
||||
|
|
||||
this.initialPatterns = initialPatterns; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject Create(CreateApp command) |
|
||||
{ |
|
||||
ThrowIfCreated(); |
|
||||
|
|
||||
var appId = new NamedId<Guid>(command.AppId, command.Name); |
|
||||
|
|
||||
var events = new List<AppEvent> |
|
||||
{ |
|
||||
CreateInitalEvent(command.Name), |
|
||||
CreateInitialOwner(command.Actor), |
|
||||
CreateInitialLanguage() |
|
||||
}; |
|
||||
|
|
||||
foreach (var pattern in initialPatterns) |
|
||||
{ |
|
||||
events.Add(CreateInitialPattern(pattern.Key, pattern.Value)); |
|
||||
} |
|
||||
|
|
||||
foreach (var @event in events) |
|
||||
{ |
|
||||
@event.Actor = command.Actor; |
|
||||
@event.AppId = appId; |
|
||||
|
|
||||
RaiseEvent(@event); |
|
||||
} |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject UpdateLanguage(UpdateLanguage command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppLanguageUpdated())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject UpdateClient(UpdateClient command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
if (!string.IsNullOrWhiteSpace(command.Name)) |
|
||||
{ |
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppClientRenamed())); |
|
||||
} |
|
||||
|
|
||||
if (command.Permission.HasValue) |
|
||||
{ |
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppClientUpdated { Permission = command.Permission.Value })); |
|
||||
} |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject AssignContributor(AssignContributor command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppContributorAssigned())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject RemoveContributor(RemoveContributor command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppContributorRemoved())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject AttachClient(AttachClient command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppClientAttached())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject RevokeClient(RevokeClient command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppClientRevoked())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject AddLanguage(AddLanguage command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppLanguageAdded())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject RemoveLanguage(RemoveLanguage command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppLanguageRemoved())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject ChangePlan(ChangePlan command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppPlanChanged())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject AddPattern(AddPattern command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppPatternAdded())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject DeletePattern(DeletePattern command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppPatternDeleted())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
public AppDomainObject UpdatePattern(UpdatePattern command) |
|
||||
{ |
|
||||
ThrowIfNotCreated(); |
|
||||
|
|
||||
RaiseEvent(SimpleMapper.Map(command, new AppPatternUpdated())); |
|
||||
|
|
||||
return this; |
|
||||
} |
|
||||
|
|
||||
private void RaiseEvent(AppEvent @event) |
|
||||
{ |
|
||||
if (@event.AppId == null) |
|
||||
{ |
|
||||
@event.AppId = new NamedId<Guid>(Snapshot.Id, Snapshot.Name); |
|
||||
} |
|
||||
|
|
||||
RaiseEvent(Envelope.Create(@event)); |
|
||||
} |
|
||||
|
|
||||
private static AppCreated CreateInitalEvent(string name) |
|
||||
{ |
|
||||
return new AppCreated { Name = name }; |
|
||||
} |
|
||||
|
|
||||
private static AppPatternAdded CreateInitialPattern(Guid id, AppPattern pattern) |
|
||||
{ |
|
||||
return new AppPatternAdded { PatternId = id, Name = pattern.Name, Pattern = pattern.Pattern, Message = pattern.Message }; |
|
||||
} |
|
||||
|
|
||||
private static AppLanguageAdded CreateInitialLanguage() |
|
||||
{ |
|
||||
return new AppLanguageAdded { Language = Language.EN }; |
|
||||
} |
|
||||
|
|
||||
private static AppContributorAssigned CreateInitialOwner(RefToken actor) |
|
||||
{ |
|
||||
return new AppContributorAssigned { ContributorId = actor.Identifier, Permission = AppContributorPermission.Owner }; |
|
||||
} |
|
||||
|
|
||||
private void ThrowIfNotCreated() |
|
||||
{ |
|
||||
if (string.IsNullOrWhiteSpace(Snapshot.Name)) |
|
||||
{ |
|
||||
throw new DomainException("App has not been created."); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private void ThrowIfCreated() |
|
||||
{ |
|
||||
if (!string.IsNullOrWhiteSpace(Snapshot.Name)) |
|
||||
{ |
|
||||
throw new DomainException("App has already been created."); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public override void ApplyEvent(Envelope<IEvent> @event) |
|
||||
{ |
|
||||
ApplySnapshot(Snapshot.Apply(@event)); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,320 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Squidex.Domain.Apps.Core.Apps; |
||||
|
using Squidex.Domain.Apps.Entities.Apps.Commands; |
||||
|
using Squidex.Domain.Apps.Entities.Apps.Guards; |
||||
|
using Squidex.Domain.Apps.Entities.Apps.Services; |
||||
|
using Squidex.Domain.Apps.Entities.Apps.State; |
||||
|
using Squidex.Domain.Apps.Events; |
||||
|
using Squidex.Domain.Apps.Events.Apps; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.Commands; |
||||
|
using Squidex.Infrastructure.EventSourcing; |
||||
|
using Squidex.Infrastructure.Orleans; |
||||
|
using Squidex.Infrastructure.Reflection; |
||||
|
using Squidex.Infrastructure.States; |
||||
|
using Squidex.Shared.Users; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Apps |
||||
|
{ |
||||
|
public sealed class AppGrain : DomainObjectGrain<AppState>, IAppGrain |
||||
|
{ |
||||
|
private readonly InitialPatterns initialPatterns; |
||||
|
private readonly IAppProvider appProvider; |
||||
|
private readonly IAppPlansProvider appPlansProvider; |
||||
|
private readonly IAppPlanBillingManager appPlansBillingManager; |
||||
|
private readonly IUserResolver userResolver; |
||||
|
|
||||
|
public AppGrain( |
||||
|
IStore<Guid> store, |
||||
|
IAppProvider appProvider, |
||||
|
IAppPlansProvider appPlansProvider, |
||||
|
IAppPlanBillingManager appPlansBillingManager, |
||||
|
IUserResolver userResolver, |
||||
|
InitialPatterns initialPatterns) |
||||
|
: base(store) |
||||
|
{ |
||||
|
Guard.NotNull(initialPatterns, nameof(initialPatterns)); |
||||
|
Guard.NotNull(appProvider, nameof(appProvider)); |
||||
|
Guard.NotNull(userResolver, nameof(userResolver)); |
||||
|
Guard.NotNull(appPlansProvider, nameof(appPlansProvider)); |
||||
|
Guard.NotNull(appPlansBillingManager, nameof(appPlansBillingManager)); |
||||
|
|
||||
|
this.userResolver = userResolver; |
||||
|
this.appProvider = appProvider; |
||||
|
this.appPlansProvider = appPlansProvider; |
||||
|
this.appPlansBillingManager = appPlansBillingManager; |
||||
|
this.initialPatterns = initialPatterns; |
||||
|
} |
||||
|
|
||||
|
public override Task<object> ExecuteAsync(IAggregateCommand command) |
||||
|
{ |
||||
|
switch (command) |
||||
|
{ |
||||
|
case CreateApp createApp: |
||||
|
return CreateAsync(createApp, async c => |
||||
|
{ |
||||
|
await GuardApp.CanCreate(c, appProvider); |
||||
|
|
||||
|
Create(c); |
||||
|
}); |
||||
|
|
||||
|
case AssignContributor assigneContributor: |
||||
|
return UpdateAsync(assigneContributor, async c => |
||||
|
{ |
||||
|
await GuardAppContributors.CanAssign(Snapshot.Contributors, c, userResolver, appPlansProvider.GetPlan(Snapshot.Plan?.PlanId)); |
||||
|
|
||||
|
AssignContributor(c); |
||||
|
}); |
||||
|
|
||||
|
case RemoveContributor removeContributor: |
||||
|
return UpdateAsync(removeContributor, c => |
||||
|
{ |
||||
|
GuardAppContributors.CanRemove(Snapshot.Contributors, c); |
||||
|
|
||||
|
RemoveContributor(c); |
||||
|
}); |
||||
|
|
||||
|
case AttachClient attachClient: |
||||
|
return UpdateAsync(attachClient, c => |
||||
|
{ |
||||
|
GuardAppClients.CanAttach(Snapshot.Clients, c); |
||||
|
|
||||
|
AttachClient(c); |
||||
|
}); |
||||
|
|
||||
|
case UpdateClient updateClient: |
||||
|
return UpdateAsync(updateClient, c => |
||||
|
{ |
||||
|
GuardAppClients.CanUpdate(Snapshot.Clients, c); |
||||
|
|
||||
|
UpdateClient(c); |
||||
|
}); |
||||
|
|
||||
|
case RevokeClient revokeClient: |
||||
|
return UpdateAsync(revokeClient, c => |
||||
|
{ |
||||
|
GuardAppClients.CanRevoke(Snapshot.Clients, c); |
||||
|
|
||||
|
RevokeClient(c); |
||||
|
}); |
||||
|
|
||||
|
case AddLanguage addLanguage: |
||||
|
return UpdateAsync(addLanguage, c => |
||||
|
{ |
||||
|
GuardAppLanguages.CanAdd(Snapshot.LanguagesConfig, c); |
||||
|
|
||||
|
AddLanguage(c); |
||||
|
}); |
||||
|
|
||||
|
case RemoveLanguage removeLanguage: |
||||
|
return UpdateAsync(removeLanguage, c => |
||||
|
{ |
||||
|
GuardAppLanguages.CanRemove(Snapshot.LanguagesConfig, c); |
||||
|
|
||||
|
RemoveLanguage(c); |
||||
|
}); |
||||
|
|
||||
|
case UpdateLanguage updateLanguage: |
||||
|
return UpdateAsync(updateLanguage, c => |
||||
|
{ |
||||
|
GuardAppLanguages.CanUpdate(Snapshot.LanguagesConfig, c); |
||||
|
|
||||
|
UpdateLanguage(c); |
||||
|
}); |
||||
|
|
||||
|
case AddPattern addPattern: |
||||
|
return UpdateAsync(addPattern, c => |
||||
|
{ |
||||
|
GuardAppPattern.CanAdd(Snapshot.Patterns, c); |
||||
|
|
||||
|
AddPattern(c); |
||||
|
}); |
||||
|
|
||||
|
case DeletePattern deletePattern: |
||||
|
return UpdateAsync(deletePattern, c => |
||||
|
{ |
||||
|
GuardAppPattern.CanDelete(Snapshot.Patterns, c); |
||||
|
|
||||
|
DeletePattern(c); |
||||
|
}); |
||||
|
|
||||
|
case UpdatePattern updatePattern: |
||||
|
return UpdateAsync(updatePattern, c => |
||||
|
{ |
||||
|
GuardAppPattern.CanUpdate(Snapshot.Patterns, c); |
||||
|
|
||||
|
UpdatePattern(c); |
||||
|
}); |
||||
|
|
||||
|
case ChangePlan changePlan: |
||||
|
return UpdateReturnAsync(changePlan, async c => |
||||
|
{ |
||||
|
GuardApp.CanChangePlan(c, Snapshot.Plan, appPlansProvider); |
||||
|
|
||||
|
if (c.FromCallback) |
||||
|
{ |
||||
|
ChangePlan(c); |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
var result = await appPlansBillingManager.ChangePlanAsync(c.Actor.Identifier, Snapshot.Id, Snapshot.Name, c.PlanId); |
||||
|
|
||||
|
if (result is PlanChangedResult) |
||||
|
{ |
||||
|
ChangePlan(c); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
default: |
||||
|
throw new NotSupportedException(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Create(CreateApp command) |
||||
|
{ |
||||
|
var appId = new NamedId<Guid>(command.AppId, command.Name); |
||||
|
|
||||
|
var events = new List<AppEvent> |
||||
|
{ |
||||
|
CreateInitalEvent(command.Name), |
||||
|
CreateInitialOwner(command.Actor), |
||||
|
CreateInitialLanguage() |
||||
|
}; |
||||
|
|
||||
|
foreach (var pattern in initialPatterns) |
||||
|
{ |
||||
|
events.Add(CreateInitialPattern(pattern.Key, pattern.Value)); |
||||
|
} |
||||
|
|
||||
|
foreach (var @event in events) |
||||
|
{ |
||||
|
@event.Actor = command.Actor; |
||||
|
@event.AppId = appId; |
||||
|
|
||||
|
RaiseEvent(@event); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void UpdateClient(UpdateClient command) |
||||
|
{ |
||||
|
if (!string.IsNullOrWhiteSpace(command.Name)) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppClientRenamed())); |
||||
|
} |
||||
|
|
||||
|
if (command.Permission.HasValue) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppClientUpdated { Permission = command.Permission.Value })); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void UpdateLanguage(UpdateLanguage command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppLanguageUpdated())); |
||||
|
} |
||||
|
|
||||
|
public void AssignContributor(AssignContributor command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppContributorAssigned())); |
||||
|
} |
||||
|
|
||||
|
public void RemoveContributor(RemoveContributor command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppContributorRemoved())); |
||||
|
} |
||||
|
|
||||
|
public void AttachClient(AttachClient command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppClientAttached())); |
||||
|
} |
||||
|
|
||||
|
public void RevokeClient(RevokeClient command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppClientRevoked())); |
||||
|
} |
||||
|
|
||||
|
public void AddLanguage(AddLanguage command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppLanguageAdded())); |
||||
|
} |
||||
|
|
||||
|
public void RemoveLanguage(RemoveLanguage command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppLanguageRemoved())); |
||||
|
} |
||||
|
|
||||
|
public void ChangePlan(ChangePlan command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppPlanChanged())); |
||||
|
} |
||||
|
|
||||
|
public void AddPattern(AddPattern command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppPatternAdded())); |
||||
|
} |
||||
|
|
||||
|
public void DeletePattern(DeletePattern command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppPatternDeleted())); |
||||
|
} |
||||
|
|
||||
|
public void UpdatePattern(UpdatePattern command) |
||||
|
{ |
||||
|
RaiseEvent(SimpleMapper.Map(command, new AppPatternUpdated())); |
||||
|
} |
||||
|
|
||||
|
private void RaiseEvent(AppEvent @event) |
||||
|
{ |
||||
|
if (@event.AppId == null) |
||||
|
{ |
||||
|
@event.AppId = new NamedId<Guid>(Snapshot.Id, Snapshot.Name); |
||||
|
} |
||||
|
|
||||
|
RaiseEvent(Envelope.Create(@event)); |
||||
|
} |
||||
|
|
||||
|
private static AppCreated CreateInitalEvent(string name) |
||||
|
{ |
||||
|
return new AppCreated { Name = name }; |
||||
|
} |
||||
|
|
||||
|
private static AppPatternAdded CreateInitialPattern(Guid id, AppPattern pattern) |
||||
|
{ |
||||
|
return new AppPatternAdded { PatternId = id, Name = pattern.Name, Pattern = pattern.Pattern, Message = pattern.Message }; |
||||
|
} |
||||
|
|
||||
|
private static AppLanguageAdded CreateInitialLanguage() |
||||
|
{ |
||||
|
return new AppLanguageAdded { Language = Language.EN }; |
||||
|
} |
||||
|
|
||||
|
private static AppContributorAssigned CreateInitialOwner(RefToken actor) |
||||
|
{ |
||||
|
return new AppContributorAssigned { ContributorId = actor.Identifier, Permission = AppContributorPermission.Owner }; |
||||
|
} |
||||
|
|
||||
|
public override void ApplyEvent(Envelope<IEvent> @event) |
||||
|
{ |
||||
|
ApplySnapshot(Snapshot.Apply(@event)); |
||||
|
} |
||||
|
|
||||
|
public Task<J<IAppEntity>> GetStateAsync() |
||||
|
{ |
||||
|
return Task.FromResult(new J<IAppEntity>(Snapshot)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Threading.Tasks; |
||||
|
using Squidex.Infrastructure.Commands; |
||||
|
using Squidex.Infrastructure.Orleans; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Apps |
||||
|
{ |
||||
|
public interface IAppGrain : IDomainObjectGrain |
||||
|
{ |
||||
|
Task<J<IAppEntity>> GetStateAsync(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue