|
|
|
@ -61,65 +61,51 @@ namespace Squidex.Domain.Apps.Entities.Contents |
|
|
|
{ |
|
|
|
GuardContent.CanCreate(c); |
|
|
|
|
|
|
|
var operationContext = await CreateContext(c, () => "Failed to create content."); |
|
|
|
var operationContext = await CreateContext(c.AppId.Id, c.SchemaId.Id, () => "Failed to create content."); |
|
|
|
|
|
|
|
await operationContext.ExecuteScriptAndTransformAsync(x => x.ScriptCreate, "Create", c, c.Data, null); |
|
|
|
await operationContext.EnrichAsync(c.Data); |
|
|
|
await operationContext.ValidateAsync(c.Data); |
|
|
|
|
|
|
|
if (c.Publish) |
|
|
|
{ |
|
|
|
await operationContext.ExecuteScriptAsync(x => x.ScriptChange, "Published"); |
|
|
|
await operationContext.ExecuteScriptAsync(x => x.ScriptChange, "Published", c, c.Data, null); |
|
|
|
} |
|
|
|
|
|
|
|
await operationContext.ExecuteScriptAndTransformAsync(x => x.ScriptCreate, "Create"); |
|
|
|
await operationContext.EnrichAsync(); |
|
|
|
await operationContext.ValidateAsync(); |
|
|
|
|
|
|
|
Create(c); |
|
|
|
|
|
|
|
return EntityCreatedResult.Create(c.Data, NewVersion); |
|
|
|
}); |
|
|
|
|
|
|
|
case UpdateContent updateContent: |
|
|
|
return UpdateReturnAsync(updateContent, async c => |
|
|
|
return UpdateReturnAsync(updateContent, c => |
|
|
|
{ |
|
|
|
GuardContent.CanUpdate(c); |
|
|
|
|
|
|
|
var operationContext = await CreateContext(c, () => "Failed to update content."); |
|
|
|
|
|
|
|
await operationContext.ValidateAsync(); |
|
|
|
await operationContext.ExecuteScriptAndTransformAsync(x => x.ScriptUpdate, "Update"); |
|
|
|
|
|
|
|
Update(c); |
|
|
|
|
|
|
|
return new ContentDataChangedResult(Snapshot.Data, NewVersion); |
|
|
|
return UpdateContentAsync(c, c.Data, "Update", c.AsProposal); |
|
|
|
}); |
|
|
|
|
|
|
|
case PatchContent patchContent: |
|
|
|
return UpdateReturnAsync(patchContent, async c => |
|
|
|
return UpdateReturnAsync(patchContent, c => |
|
|
|
{ |
|
|
|
GuardContent.CanPatch(c); |
|
|
|
|
|
|
|
var operationContext = await CreateContext(c, () => "Failed to patch content."); |
|
|
|
|
|
|
|
await operationContext.ValidatePartialAsync(); |
|
|
|
await operationContext.ExecuteScriptAndTransformAsync(x => x.ScriptUpdate, "Patch"); |
|
|
|
|
|
|
|
Patch(c); |
|
|
|
|
|
|
|
return new ContentDataChangedResult(Snapshot.Data, NewVersion); |
|
|
|
return UpdateContentAsync(c, c.Data.MergeInto(Snapshot.PendingData ?? Snapshot.Data), "Patch", c.AsProposal); |
|
|
|
}); |
|
|
|
|
|
|
|
case ChangeContentStatus patchContent: |
|
|
|
return UpdateAsync(patchContent, async c => |
|
|
|
case ChangeContentStatus changeContentStatus: |
|
|
|
return UpdateReturnAsync(changeContentStatus, c => |
|
|
|
{ |
|
|
|
GuardContent.CanChangeContentStatus(Snapshot.Status, c); |
|
|
|
GuardContent.CanChangeContentStatus(Snapshot.PendingData, Snapshot.Status, c); |
|
|
|
|
|
|
|
if (!c.DueTime.HasValue) |
|
|
|
if (Snapshot.PendingData != null) |
|
|
|
{ |
|
|
|
var operationContext = await CreateContext(c, () => "Failed to patch content."); |
|
|
|
|
|
|
|
await operationContext.ExecuteScriptAsync(x => x.ScriptChange, c.Status); |
|
|
|
return UpdateContentAsync(c, Snapshot.PendingData, "Update", false); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return ChangeStatusAsync(c); |
|
|
|
} |
|
|
|
|
|
|
|
ChangeStatus(c); |
|
|
|
}); |
|
|
|
|
|
|
|
case DeleteContent deleteContent: |
|
|
|
@ -127,18 +113,63 @@ namespace Squidex.Domain.Apps.Entities.Contents |
|
|
|
{ |
|
|
|
GuardContent.CanDelete(c); |
|
|
|
|
|
|
|
var operationContext = await CreateContext(c, () => "Failed to delete content."); |
|
|
|
var operationContext = await CreateContext(Snapshot.AppId.Id, Snapshot.SchemaId.Id, () => "Failed to delete content."); |
|
|
|
|
|
|
|
await operationContext.ExecuteScriptAsync(x => x.ScriptDelete, "Delete"); |
|
|
|
await operationContext.ExecuteScriptAsync(x => x.ScriptDelete, "Delete", c, Snapshot.Data); |
|
|
|
|
|
|
|
Delete(c); |
|
|
|
}); |
|
|
|
|
|
|
|
case DiscardChanges discardChanges: |
|
|
|
return UpdateAsync(discardChanges, c => |
|
|
|
{ |
|
|
|
GuardContent.CanDiscardChanges(Snapshot.PendingData, c); |
|
|
|
|
|
|
|
DiscardChanges(c); |
|
|
|
}); |
|
|
|
|
|
|
|
default: |
|
|
|
throw new NotSupportedException(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<object> ChangeStatusAsync(ChangeContentStatus c) |
|
|
|
{ |
|
|
|
if (!c.DueTime.HasValue) |
|
|
|
{ |
|
|
|
var operationContext = await CreateContext(Snapshot.AppId.Id, Snapshot.SchemaId.Id, () => "Failed to change content."); |
|
|
|
|
|
|
|
await operationContext.ExecuteScriptAsync(x => x.ScriptChange, c.Status, c, Snapshot.Data); |
|
|
|
} |
|
|
|
|
|
|
|
ChangeStatus(c); |
|
|
|
|
|
|
|
return new EntitySavedResult(NewVersion); |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<object> UpdateContentAsync(ContentCommand command, NamedContentData data, string operation, bool asProposal) |
|
|
|
{ |
|
|
|
var operationContext = await CreateContext(Snapshot.AppId.Id, Snapshot.SchemaId.Id, () => "Failed to update content."); |
|
|
|
|
|
|
|
if (!Snapshot.Data.Equals(data)) |
|
|
|
{ |
|
|
|
await operationContext.ValidateAsync(data); |
|
|
|
|
|
|
|
if (asProposal) |
|
|
|
{ |
|
|
|
ProposeUpdate(command, data); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
await operationContext.ExecuteScriptAndTransformAsync(x => x.ScriptUpdate, "Update", command, data, Snapshot.Data); |
|
|
|
|
|
|
|
Update(command, data); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return new ContentDataChangedResult(data, NewVersion); |
|
|
|
} |
|
|
|
|
|
|
|
public void Create(CreateContent command) |
|
|
|
{ |
|
|
|
RaiseEvent(SimpleMapper.Map(command, new ContentCreated())); |
|
|
|
@ -149,12 +180,24 @@ namespace Squidex.Domain.Apps.Entities.Contents |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void Update(UpdateContent command) |
|
|
|
public void DiscardChanges(DiscardChanges command) |
|
|
|
{ |
|
|
|
if (!command.Data.Equals(Snapshot.Data)) |
|
|
|
{ |
|
|
|
RaiseEvent(SimpleMapper.Map(command, new ContentUpdated())); |
|
|
|
} |
|
|
|
RaiseEvent(SimpleMapper.Map(command, new ContentChangesDiscarded())); |
|
|
|
} |
|
|
|
|
|
|
|
public void Delete(DeleteContent command) |
|
|
|
{ |
|
|
|
RaiseEvent(SimpleMapper.Map(command, new ContentDeleted())); |
|
|
|
} |
|
|
|
|
|
|
|
public void Update(ContentCommand command, NamedContentData data) |
|
|
|
{ |
|
|
|
RaiseEvent(SimpleMapper.Map(command, new ContentUpdated { Data = data })); |
|
|
|
} |
|
|
|
|
|
|
|
public void ProposeUpdate(ContentCommand command, NamedContentData data) |
|
|
|
{ |
|
|
|
RaiseEvent(SimpleMapper.Map(command, new ContentUpdateProposed { Data = data })); |
|
|
|
} |
|
|
|
|
|
|
|
public void ChangeStatus(ChangeContentStatus command) |
|
|
|
@ -169,25 +212,6 @@ namespace Squidex.Domain.Apps.Entities.Contents |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void Patch(PatchContent command) |
|
|
|
{ |
|
|
|
var newData = command.Data.MergeInto(Snapshot.Data); |
|
|
|
|
|
|
|
if (!newData.Equals(Snapshot.Data)) |
|
|
|
{ |
|
|
|
var @event = SimpleMapper.Map(command, new ContentUpdated()); |
|
|
|
|
|
|
|
@event.Data = newData; |
|
|
|
|
|
|
|
RaiseEvent(@event); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void Delete(DeleteContent command) |
|
|
|
{ |
|
|
|
RaiseEvent(SimpleMapper.Map(command, new ContentDeleted())); |
|
|
|
} |
|
|
|
|
|
|
|
private void RaiseEvent(SchemaEvent @event) |
|
|
|
{ |
|
|
|
if (@event.AppId == null) |
|
|
|
@ -216,13 +240,13 @@ namespace Squidex.Domain.Apps.Entities.Contents |
|
|
|
ApplySnapshot(Snapshot.Apply(@event)); |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<ContentOperationContext> CreateContext(ContentCommand command, Func<string> message) |
|
|
|
private async Task<ContentOperationContext> CreateContext(Guid appId, Guid schemaId, Func<string> message) |
|
|
|
{ |
|
|
|
var operationContext = |
|
|
|
await ContentOperationContext.CreateAsync(command, Snapshot, |
|
|
|
contentRepository, |
|
|
|
await ContentOperationContext.CreateAsync(appId, schemaId, |
|
|
|
appProvider, |
|
|
|
assetRepository, |
|
|
|
contentRepository, |
|
|
|
scriptEngine, |
|
|
|
message); |
|
|
|
|
|
|
|
|