Browse Source

Fix for singleton contents.

pull/520/head
Sebastian 6 years ago
parent
commit
91e19f42e7
  1. 4
      backend/src/Squidex.Domain.Apps.Entities/Contents/ContentDomainObject.cs
  2. 14
      backend/src/Squidex.Domain.Apps.Entities/Contents/Guards/GuardContent.cs
  3. 30
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Guard/GuardContentTests.cs

4
backend/src/Squidex.Domain.Apps.Entities/Contents/ContentDomainObject.cs

@ -100,7 +100,7 @@ namespace Squidex.Domain.Apps.Entities.Contents
{ {
await LoadContext(Snapshot.AppId, Snapshot.SchemaId, c, () => "Failed to create draft."); await LoadContext(Snapshot.AppId, Snapshot.SchemaId, c, () => "Failed to create draft.");
GuardContent.CanCreateDraft(c, context.Schema, Snapshot); GuardContent.CanCreateDraft(c, Snapshot);
var status = await contentWorkflow.GetInitialStatusAsync(context.Schema); var status = await contentWorkflow.GetInitialStatusAsync(context.Schema);
@ -114,7 +114,7 @@ namespace Squidex.Domain.Apps.Entities.Contents
{ {
await LoadContext(Snapshot.AppId, Snapshot.SchemaId, c, () => "Failed to delete draft."); await LoadContext(Snapshot.AppId, Snapshot.SchemaId, c, () => "Failed to delete draft.");
GuardContent.CanDeleteDraft(c, context.Schema, Snapshot); GuardContent.CanDeleteDraft(c, Snapshot);
DeleteDraft(c); DeleteDraft(c);

14
backend/src/Squidex.Domain.Apps.Entities/Contents/Guards/GuardContent.cs

@ -63,30 +63,20 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guards
await ValidateCanUpdate(content, contentWorkflow, command.User); await ValidateCanUpdate(content, contentWorkflow, command.User);
} }
public static void CanDeleteDraft(DeleteContentDraft command, ISchemaEntity schema, ContentState content) public static void CanDeleteDraft(DeleteContentDraft command, ContentState content)
{ {
Guard.NotNull(command); Guard.NotNull(command);
if (schema.SchemaDef.IsSingleton)
{
throw new DomainException("Singleton content cannot be updated.");
}
if (content.NewStatus == null) if (content.NewStatus == null)
{ {
throw new DomainException("There is nothing to delete."); throw new DomainException("There is nothing to delete.");
} }
} }
public static void CanCreateDraft(CreateContentDraft command, ISchemaEntity schema, ContentState content) public static void CanCreateDraft(CreateContentDraft command, ContentState content)
{ {
Guard.NotNull(command); Guard.NotNull(command);
if (schema.SchemaDef.IsSingleton)
{
throw new DomainException("Singleton content cannot be updated.");
}
if (content.Status != Status.Published) if (content.Status != Status.Published)
{ {
throw new DomainException("You can only create a new version when the content is published."); throw new DomainException("You can only create a new version when the content is published.");

30
backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Guard/GuardContentTests.cs

@ -229,17 +229,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
await GuardContent.CanChangeStatus(schema, content, contentWorkflow, command); await GuardContent.CanChangeStatus(schema, content, contentWorkflow, command);
} }
[Fact]
public void CreateDraft_should_throw_exception_if_singleton()
{
var schema = CreateSchema(true);
var content = CreateContent(Status.Published);
var command = new CreateContentDraft();
Assert.Throws<DomainException>(() => GuardContent.CanCreateDraft(command, schema, content));
}
[Fact] [Fact]
public void CreateDraft_should_throw_exception_if_not_published() public void CreateDraft_should_throw_exception_if_not_published()
{ {
@ -248,7 +237,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
var content = CreateContent(Status.Draft); var content = CreateContent(Status.Draft);
var command = new CreateContentDraft(); var command = new CreateContentDraft();
Assert.Throws<DomainException>(() => GuardContent.CanCreateDraft(command, schema, content)); Assert.Throws<DomainException>(() => GuardContent.CanCreateDraft(command, content));
} }
[Fact] [Fact]
@ -259,7 +248,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
var content = CreateContent(Status.Published); var content = CreateContent(Status.Published);
var command = new CreateContentDraft(); var command = new CreateContentDraft();
GuardContent.CanCreateDraft(command, schema, content); GuardContent.CanCreateDraft(command, content);
} }
[Fact] [Fact]
@ -270,18 +259,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
var content = new ContentState(); var content = new ContentState();
var command = new DeleteContentDraft(); var command = new DeleteContentDraft();
Assert.Throws<DomainException>(() => GuardContent.CanDeleteDraft(command, schema, content)); Assert.Throws<DomainException>(() => GuardContent.CanDeleteDraft(command, content));
}
[Fact]
public void CanDeleteDraft_should_throw_exception_if_singleton()
{
var schema = CreateSchema(true);
var content = CreateDraftContent(Status.Draft);
var command = new DeleteContentDraft();
Assert.Throws<DomainException>(() => GuardContent.CanDeleteDraft(command, schema, content));
} }
[Fact] [Fact]
@ -292,7 +270,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
var content = CreateDraftContent(Status.Draft); var content = CreateDraftContent(Status.Draft);
var command = new DeleteContentDraft(); var command = new DeleteContentDraft();
GuardContent.CanDeleteDraft(command, schema, content); GuardContent.CanDeleteDraft(command, content);
} }
[Fact] [Fact]

Loading…
Cancel
Save