Skip to content

Commit

Permalink
Enrich defaults (#1057)
Browse files Browse the repository at this point in the history
* Initial

* Simplify tests.

* Enrich defaults.
  • Loading branch information
SebastianStehle authored Dec 22, 2023
1 parent 3e5e6c8 commit 28ee481
Show file tree
Hide file tree
Showing 164 changed files with 27,967 additions and 1,356 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@
<data name="ContentPartitionField" xml:space="preserve">
<value>{0} field ({1}).</value>
</data>
<data name="ContentRequestApplyDefaults" xml:space="preserve">
<value>Enrich the content with defaults.</value>
</data>
<data name="ContentRequestData" xml:space="preserve">
<value>The data for the content.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public enum BulkUpdateContentType
Delete,
Patch,
Update,
Validate
Validate,
EnrichDefaults
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public sealed class BulkUpdateJob

public bool Permanent { get; set; }

public bool EnrichDefaults { get; set; }

public long ExpectedCount { get; set; } = 1;

public long ExpectedVersion { get; set; } = EtagVersion.Any;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

namespace Squidex.Domain.Apps.Entities.Contents.Commands;

public sealed class EnrichContentDefaults : ContentCommand
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ namespace Squidex.Domain.Apps.Entities.Contents.Commands;

public class UpdateContent : ContentDataCommand
{
public bool EnrichDefaults { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public sealed class UpsertContent : ContentDataCommand, ISchemaCommand

public bool Patch { get; set; }

public bool EnrichDefaults { get; set; }

public UpsertContent()
{
ContentId = DomainId.NewGuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
return Snapshot;
}, ct);

case EnrichContentDefaults enrichContentDefaults:
return ApplyReturnAsync(enrichContentDefaults, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);

var newData = operation.GenerateDefaultValues(Snapshot.EditingData.Clone());

if (!newData.Equals(Snapshot.EditingData))
{
Update(c, newData);
}

return Snapshot;
}, ct);

case DeleteContent { Permanent: true } deleteContent:
return DeletePermanentAsync(deleteContent, async (c, ct) =>
{
Expand Down Expand Up @@ -319,6 +334,11 @@ private async Task UpdateCore(UpdateContent c, ContentOperation operation,

var newData = c.Data;

if (c.EnrichDefaults)
{
newData = operation.GenerateDefaultValues(newData);
}

if (newData.Equals(Snapshot.EditingData))
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ private BulkTask CreateTask(
return CreateTask<ValidateContent>(id, schemaId, bulkJob, bulk, jobIndex,
PermissionIds.AppContentsReadOwn);

case BulkUpdateContentType.EnrichDefaults:
return CreateTask<EnrichContentDefaults>(id, schemaId, bulkJob, bulk, jobIndex,
PermissionIds.AppContentsUpdateOwn);

case BulkUpdateContentType.ChangeStatus:
return CreateTask<ChangeContentStatus>(id, schemaId, bulkJob, bulk, jobIndex,
PermissionIds.AppContentsChangeStatusOwn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using LibGit2Sharp;
using Squidex.Domain.Apps.Core.Contents;
using Squidex.Domain.Apps.Entities.Contents.Text.State;
using Squidex.Domain.Apps.Events.Contents;
using Squidex.Infrastructure;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.Json;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
{
await Trigger(triggerRule);

return true;
return None.Value;
}, ct);

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,36 @@ public async Task<IActionResult> PostUpsertContent(string app, string schema, Do
[AcceptHeader.Unpublished]
[AcceptHeader.Languages]
[ApiCosts(1)]
public async Task<IActionResult> PutContent(string app, string schema, DomainId id, [FromBody] ContentData request)
public async Task<IActionResult> PutContent(string app, string schema, DomainId id, UpdateContentDto request)
{
var command = new UpdateContent { ContentId = id, Data = request };
var command = request.ToCommand(id);

var response = await InvokeCommandAsync(command);

return Ok(response);
}

/// <summary>
/// Enrich a content item with defaults.
/// </summary>
/// <param name="app">The name of the app.</param>
/// <param name="schema">The name of the schema.</param>
/// <param name="id">The ID of the content item to update.</param>
/// <response code="200">Content updated.</response>
/// <response code="404">Content references, schema or app not found.</response>
/// <remarks>
/// You can read the generated documentation for your app at /api/content/{appName}/docs.
/// </remarks>
[HttpPut]
[Route("content/{app}/{schema}/{id}/defaults")]
[ProducesResponseType(typeof(ContentDto), StatusCodes.Status200OK)]
[ApiPermissionOrAnonymous(PermissionIds.AppContentsUpdateOwn)]
[AcceptHeader.Unpublished]
[AcceptHeader.Languages]
[ApiCosts(1)]
public async Task<IActionResult> PutContentDefaults(string app, string schema, DomainId id)
{
var command = new EnrichContentDefaults { ContentId = id };

var response = await InvokeCommandAsync(command);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ private static void GenerateSchemaOperations(OperationsBuilder builder)
.OperationSummary("Upsert a [schema] content item.")
.HasQuery("patch", JsonObjectType.Boolean, FieldDescriptions.ContentRequestPatch)
.HasQuery("publish", JsonObjectType.Boolean, FieldDescriptions.ContentRequestPublish)
.HasQuery("enrichDefaults", JsonObjectType.String, FieldDescriptions.ContentRequestApplyDefaults)
.HasId()
.HasBody("data", builder.DataSchema, Resources.OpenApiSchemaBody)
.Responds(200, "Content item created or updated.", builder.ContentSchema)
Expand All @@ -184,11 +185,19 @@ private static void GenerateSchemaOperations(OperationsBuilder builder)
.RequirePermission(PermissionIds.AppContentsUpdateOwn)
.Operation("Update")
.OperationSummary("Update a [schema] content item.")
.HasQuery("enrichDefaults", JsonObjectType.String, FieldDescriptions.ContentRequestApplyDefaults)
.HasId()
.HasBody("data", builder.DataSchema, Resources.OpenApiSchemaBody)
.Responds(200, "Content item updated.", builder.ContentSchema)
.Responds(400, "Content data not valid.");

builder.AddOperation(OpenApiOperationMethod.Put, "/{id}/defaults")
.RequirePermission(PermissionIds.AppContentsUpdateOwn)
.Operation("UpdateDefaults")
.OperationSummary("Apply a [schema] content item. defaults")
.HasId()
.Responds(200, "Content item updated.", builder.ContentSchema);

builder.AddOperation(OpenApiOperationMethod.Patch, "/{id}")
.RequirePermission(PermissionIds.AppContentsUpdateOwn)
.Operation("Patch")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public class BulkUpdateContentsJobDto
/// </summary>
public bool Permanent { get; set; }

/// <summary>
/// Enrich the data with the default values when updating a content item.
/// </summary>
public bool EnrichDefaults { get; set; }

/// <summary>
/// The number of expected items. Set it to a higher number to update multiple items when a query is defined.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using Microsoft.AspNetCore.Mvc;
using Squidex.Domain.Apps.Core.Contents;
using Squidex.Domain.Apps.Entities.Contents.Commands;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Reflection;
using Squidex.Web;

namespace Squidex.Areas.Api.Controllers.Contents.Models;

[OpenApiRequest]
public class UpdateContentDto
{
/// <summary>
/// The full data for the content item.
/// </summary>
[FromBody]
public ContentData Data { get; set; }

/// <summary>
/// Enrich the content with defaults.
/// </summary>
[FromQuery(Name = "enrichDefaults")]
public bool EnrichDefaults { get; set; }

public UpdateContent ToCommand(DomainId id)
{
return SimpleMapper.Map(this, new UpdateContent { ContentId = id });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public class UpsertContentDto
[FromQuery(Name = "patch")]
public bool Patch { get; set; }

/// <summary>
/// Enrich the content with defaults.
/// </summary>
[FromQuery(Name = "enrichDefaults")]
public bool EnrichDefaults { get; set; }

/// <summary>
/// True to automatically publish the content.
/// </summary>
Expand Down
Loading

0 comments on commit 28ee481

Please sign in to comment.