Skip to content

Commit

Permalink
Merge branch 'Squidex:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaston Muijtjens authored Aug 1, 2023
2 parents 335cec2 + f16ea0b commit a0d1efb
Show file tree
Hide file tree
Showing 36 changed files with 646 additions and 595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Globalization;
using NodaTime;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Json.Objects;

#pragma warning disable SA1313 // Parameter names should begin with lower-case letter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,28 +206,26 @@ await Collection.Find(x => x.IndexedAppId == appId && x.FileHash == hash && !x.I
}
}

public async Task<IAssetEntity?> FindAssetBySlugAsync(DomainId appId, string slug,
public async Task<IAssetEntity?> FindAssetBySlugAsync(DomainId appId, string slug, bool allowDeleted,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("MongoAssetRepository/FindAssetBySlugAsync"))
{
var assetEntity =
await Collection.Find(x => x.IndexedAppId == appId && x.Slug == slug && !x.IsDeleted)
await Collection.Find(BuildFilter(appId, slug, allowDeleted))
.FirstOrDefaultAsync(ct);

return assetEntity;
}
}

public async Task<IAssetEntity?> FindAssetAsync(DomainId appId, DomainId id,
public async Task<IAssetEntity?> FindAssetAsync(DomainId appId, DomainId id, bool allowDeleted,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("MongoAssetRepository/FindAssetAsync"))
{
var documentId = DomainId.Combine(appId, id);

var assetEntity =
await Collection.Find(x => x.DocumentId == documentId && !x.IsDeleted)
await Collection.Find(BuildFilter(appId, id, allowDeleted))
.FirstOrDefaultAsync(ct);

return assetEntity;
Expand Down Expand Up @@ -256,6 +254,30 @@ private static FilterDefinition<MongoAssetEntity> BuildFilter(DomainId appId, Ha
Filter.Ne(x => x.IsDeleted, true));
}

private static FilterDefinition<MongoAssetEntity> BuildFilter(DomainId appId, string slug, bool allowDeleted)
{
var filter = Filter.And(Filter.Eq(x => x.IndexedAppId, appId), Filter.Eq(x => x.Slug, slug));

if (!allowDeleted)
{
filter = Filter.And(filter, Filter.Ne(x => x.IsDeleted, true));
}

return filter;
}

private static FilterDefinition<MongoAssetEntity> BuildFilter(DomainId appId, DomainId id, bool allowDeleted)
{
var filter = Filter.Eq(x => x.DocumentId, DomainId.Combine(appId, id));

if (!allowDeleted)
{
filter = Filter.And(filter, Filter.Ne(x => x.IsDeleted, true));
}

return filter;
}

private static FilterDefinition<MongoAssetEntity> BuildFilter(DomainId appId, DomainId parentId)
{
return Filter.And(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,29 @@ protected override bool IsDeleted(State snapshot)
return snapshot.IsDeleted;
}

protected override bool CanAcceptCreation(ICommand command)
protected override bool CanAccept(ICommand command)
{
return command is AppCommandBase;
if (Snapshot.Id == default)
{
return true;
}

return command is AppCommandBase c && c.AggregateId == Snapshot.Id;
}

protected override bool CanAccept(ICommand command)
protected override bool CanAccept(ICommand command, DomainObjectState state)
{
return command is AppCommand update && Equals(update?.AppId?.Id, Snapshot.Id);
switch (state)
{
case DomainObjectState.Undefined:
return command is CreateApp;
case DomainObjectState.Empty:
return command is CreateApp;
case DomainObjectState.Created:
return command is not CreateApp;
default:
return false;
}
}

public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
Expand All @@ -56,7 +71,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
switch (command)
{
case CreateApp create:
return CreateReturn(create, c =>
return ApplyReturn(create, c =>
{
GuardApp.CanCreate(c);
Expand All @@ -66,7 +81,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case UpdateApp update:
return UpdateReturn(update, c =>
return ApplyReturn(update, c =>
{
GuardApp.CanUpdate(c);
Expand All @@ -76,7 +91,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case TransferToTeam transfer:
return UpdateReturnAsync(transfer, async (c, ct) =>
return ApplyReturnAsync(transfer, async (c, ct) =>
{
await GuardApp.CanTransfer(c, Snapshot, AppProvider, ct);
Expand All @@ -86,7 +101,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case UpdateAppSettings updateSettings:
return UpdateReturn(updateSettings, c =>
return ApplyReturn(updateSettings, c =>
{
GuardApp.CanUpdateSettings(c);
Expand All @@ -96,7 +111,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case UploadAppImage uploadImage:
return UpdateReturn(uploadImage, c =>
return ApplyReturn(uploadImage, c =>
{
GuardApp.CanUploadImage(c);
Expand All @@ -106,7 +121,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case RemoveAppImage removeImage:
return UpdateReturn(removeImage, c =>
return ApplyReturn(removeImage, c =>
{
GuardApp.CanRemoveImage(c);
Expand All @@ -116,7 +131,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case ConfigureAssetScripts configureAssetScripts:
return UpdateReturn(configureAssetScripts, c =>
return ApplyReturn(configureAssetScripts, c =>
{
GuardApp.CanUpdateAssetScripts(c);
Expand All @@ -126,7 +141,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case AssignContributor assignContributor:
return UpdateReturnAsync(assignContributor, async (c, ct) =>
return ApplyReturnAsync(assignContributor, async (c, ct) =>
{
var (plan, _, _) = await UsageGate.GetPlanForAppAsync(Snapshot, false, ct);
Expand All @@ -138,7 +153,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case RemoveContributor removeContributor:
return UpdateReturn(removeContributor, c =>
return ApplyReturn(removeContributor, c =>
{
GuardAppContributors.CanRemove(c, Snapshot);
Expand All @@ -148,7 +163,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case AttachClient attachClient:
return UpdateReturn(attachClient, c =>
return ApplyReturn(attachClient, c =>
{
GuardAppClients.CanAttach(c, Snapshot);
Expand All @@ -158,7 +173,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case UpdateClient updateClient:
return UpdateReturn(updateClient, c =>
return ApplyReturn(updateClient, c =>
{
GuardAppClients.CanUpdate(c, Snapshot);
Expand All @@ -168,7 +183,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case RevokeClient revokeClient:
return UpdateReturn(revokeClient, c =>
return ApplyReturn(revokeClient, c =>
{
GuardAppClients.CanRevoke(c, Snapshot);
Expand All @@ -178,7 +193,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case AddWorkflow addWorkflow:
return UpdateReturn(addWorkflow, c =>
return ApplyReturn(addWorkflow, c =>
{
GuardAppWorkflows.CanAdd(c);
Expand All @@ -188,7 +203,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case UpdateWorkflow updateWorkflow:
return UpdateReturn(updateWorkflow, c =>
return ApplyReturn(updateWorkflow, c =>
{
GuardAppWorkflows.CanUpdate(c, Snapshot);
Expand All @@ -198,7 +213,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case DeleteWorkflow deleteWorkflow:
return UpdateReturn(deleteWorkflow, c =>
return ApplyReturn(deleteWorkflow, c =>
{
GuardAppWorkflows.CanDelete(c, Snapshot);
Expand All @@ -208,7 +223,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case AddLanguage addLanguage:
return UpdateReturn(addLanguage, c =>
return ApplyReturn(addLanguage, c =>
{
GuardAppLanguages.CanAdd(c, Snapshot);
Expand All @@ -218,7 +233,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case RemoveLanguage removeLanguage:
return UpdateReturn(removeLanguage, c =>
return ApplyReturn(removeLanguage, c =>
{
GuardAppLanguages.CanRemove(c, Snapshot);
Expand All @@ -228,7 +243,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case UpdateLanguage updateLanguage:
return UpdateReturn(updateLanguage, c =>
return ApplyReturn(updateLanguage, c =>
{
GuardAppLanguages.CanUpdate(c, Snapshot);
Expand All @@ -238,7 +253,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case AddRole addRole:
return UpdateReturn(addRole, c =>
return ApplyReturn(addRole, c =>
{
GuardAppRoles.CanAdd(c, Snapshot);
Expand All @@ -248,7 +263,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case DeleteRole deleteRole:
return UpdateReturn(deleteRole, c =>
return ApplyReturn(deleteRole, c =>
{
GuardAppRoles.CanDelete(c, Snapshot);
Expand All @@ -258,7 +273,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case UpdateRole updateRole:
return UpdateReturn(updateRole, c =>
return ApplyReturn(updateRole, c =>
{
GuardAppRoles.CanUpdate(c, Snapshot);
Expand All @@ -268,15 +283,15 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
}, ct);

case DeleteApp delete:
return UpdateAsync(delete, async (c, ct) =>
return ApplyAsync(delete, async (c, ct) =>
{
await BillingManager.UnsubscribeAsync(c.Actor.Identifier, Snapshot, default);
DeleteApp(c);
}, ct);

case ChangePlan changePlan:
return UpdateReturnAsync(changePlan, async (c, ct) =>
return ApplyReturnAsync(changePlan, async (c, ct) =>
{
GuardApp.CanChangePlan(c, Snapshot, BillingPlans);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ protected override async Task<object> EnrichResultAsync(CommandContext context,
}
catch (AssetAlreadyExistsException)
{
if (context.Command is not UpsertAsset)
{
throw;
}
}
}

Expand Down
Loading

0 comments on commit a0d1efb

Please sign in to comment.