-
Notifications
You must be signed in to change notification settings - Fork 434
/
ContentDomainObject.cs
517 lines (412 loc) · 16.2 KB
/
ContentDomainObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Microsoft.Extensions.Logging;
using NodaTime;
using Squidex.Domain.Apps.Core.Contents;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Entities.Contents.Commands;
using Squidex.Domain.Apps.Entities.Contents.DomainObject.Guards;
using Squidex.Domain.Apps.Events;
using Squidex.Domain.Apps.Events.Contents;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.Reflection;
using Squidex.Infrastructure.States;
using Squidex.Shared;
#pragma warning disable MA0022 // Return Task.FromResult instead of returning null
namespace Squidex.Domain.Apps.Entities.Contents.DomainObject;
public partial class ContentDomainObject : DomainObject<WriteContent>
{
private readonly IServiceProvider serviceProvider;
public ContentDomainObject(DomainId id, IPersistenceFactory<WriteContent> persistence, ILogger<ContentDomainObject> log,
IServiceProvider serviceProvider)
: base(id, persistence, log)
{
this.serviceProvider = serviceProvider;
}
protected override bool IsDeleted(WriteContent snapshot)
{
return snapshot.IsDeleted;
}
protected override bool IsRecreation(IEvent @event)
{
return @event is ContentCreated;
}
protected override bool CanAccept(ICommand command)
{
return command is ContentCommand c && c.AppId == Snapshot.AppId && c.SchemaId == Snapshot.SchemaId && c.ContentId == Snapshot.Id;
}
protected override bool CanAccept(ICommand command, DomainObjectState state)
{
switch (state)
{
case DomainObjectState.Undefined:
return command is CreateContent;
case DomainObjectState.Deleted:
return command is CreateContent or UpsertContent or DeleteContent { Permanent: true };
case DomainObjectState.Empty:
return command is CreateContent or UpsertContent;
default:
return command is not CreateContent;
}
}
public override Task<CommandResult> ExecuteAsync(IAggregateCommand command,
CancellationToken ct)
{
switch (command)
{
case UpsertContent upsertContent:
return ApplyReturnAsync(upsertContent, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
if (Version <= EtagVersion.Empty || IsDeleted(Snapshot))
{
await CreateCore(c.AsCreate(), operation, ct);
}
else if (c.Patch)
{
await PatchCore(c.AsUpdate(), operation, ct);
}
else
{
await UpdateCore(c.AsUpdate(), operation, ct);
}
if (Is.OptionalChange(operation.Snapshot.EditingStatus, c.Status))
{
await ChangeCore(c.AsChange(c.Status.Value), operation, ct);
}
return Snapshot;
}, ct);
case CreateContent createContent:
return ApplyReturnAsync(createContent, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
await CreateCore(c, operation, ct);
if (operation.Schema.Type == SchemaType.Singleton)
{
ChangeStatus(c.AsChange(Status.Published));
}
else if (c.Status != null && c.Status != Snapshot.EditingStatus)
{
await ChangeCore(c.AsChange(c.Status.Value), operation, ct);
}
return Snapshot;
}, ct);
case ValidateContent validate:
return ApplyReturnAsync(validate, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
await ValidateCore(operation, ct);
return true;
}, ct);
case CreateContentDraft createDraft:
return ApplyReturnAsync(createDraft, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
await CreateDraftCore(c, operation);
return Snapshot;
}, ct);
case DeleteContentDraft deleteDraft:
return ApplyReturnAsync(deleteDraft, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
DeleteDraftCore(c, operation);
return Snapshot;
}, ct);
case PatchContent patchContent:
return ApplyReturnAsync(patchContent, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
await PatchCore(c, operation, ct);
return Snapshot;
}, ct);
case UpdateContent updateContent:
return ApplyReturnAsync(updateContent, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
await UpdateCore(c, operation, ct);
return Snapshot;
}, ct);
case CancelContentSchedule cancelContentSchedule:
return ApplyReturnAsync(cancelContentSchedule, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
CancelChangeCore(c, operation);
return Snapshot;
}, ct);
case ChangeContentStatus changeContentStatus:
return ApplyReturnAsync(changeContentStatus, async (c, ct) =>
{
try
{
if (c.DueTime > SystemClock.Instance.GetCurrentInstant())
{
ChangeStatusScheduled(c, c.DueTime.Value);
}
else
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
await ChangeCore(c, operation, ct);
}
}
catch (Exception)
{
if (Snapshot.ScheduleJob != null && Snapshot.ScheduleJob.Id == c.StatusJobId)
{
CancelChangeStatus(c);
}
else
{
throw;
}
}
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) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
await DeleteCore(c, operation, ct);
}, ct);
case DeleteContent deleteContent:
return ApplyAsync(deleteContent, async (c, ct) =>
{
var operation = await ContentOperation.CreateAsync(serviceProvider, c, () => Snapshot);
await DeleteCore(c, operation, ct);
}, ct);
default:
ThrowHelper.NotSupportedException();
return default!;
}
}
private async Task CreateCore(CreateContent c, ContentOperation operation,
CancellationToken ct)
{
operation.MustNotCreateComponent();
operation.MustNotCreateSingleton();
operation.MustNotCreateForUnpublishedSchema();
operation.MustHaveData(c.Data);
if (!c.DoNotValidate)
{
await operation.ValidateInputAsync(c.Data, c.OptimizeValidation, Snapshot.IsPublished, ct);
}
var status = await operation.GetInitialStatusAsync();
if (!c.DoNotScript)
{
c.Data = await operation.ExecuteCreateScriptAsync(c.Data, status, ct);
}
c.Data = operation.GenerateDefaultValues(c.Data);
if (!c.DoNotValidate)
{
await operation.ValidateContentAsync(c.Data, c.OptimizeValidation, Snapshot.IsPublished, ct);
}
Create(c, status);
}
private async Task ChangeCore(ChangeContentStatus c, ContentOperation operation,
CancellationToken ct)
{
operation.MustHavePermission(PermissionIds.AppContentsChangeStatus);
operation.MustNotChangeSingleton(c.Status);
if (c.Status == Snapshot.EditingStatus)
{
return;
}
if (c.DoNotValidateWorkflow)
{
await operation.CheckStatusAsync(c.Status);
}
else
{
await operation.CheckTransitionAsync(c.Status);
}
if (!c.DoNotScript)
{
var newData = await operation.ExecuteChangeScriptAsync(c.Status, GetChange(c.Status), ct);
if (!newData.Equals(Snapshot.EditingData))
{
Update(c, newData);
}
}
if (c.CheckReferrers && Snapshot.IsPublished)
{
await operation.CheckReferrersAsync(ct);
}
if (!c.DoNotValidate && await operation.ShouldValidateAsync(c.Status))
{
await operation.ValidateContentAndInputAsync(Snapshot.EditingData, c.OptimizeValidation, true, ct);
}
ChangeStatus(c);
}
private async Task UpdateCore(UpdateContent c, ContentOperation operation,
CancellationToken ct)
{
operation.MustHavePermission(PermissionIds.AppContentsUpdate);
operation.MustHaveData(c.Data);
var newData = operation.InvokeUpdates(c.Data, Snapshot.EditingData);
if (!c.DoNotValidate)
{
await operation.ValidateInputAsync(newData, c.OptimizeValidation, Snapshot.IsPublished, ct);
}
if (!c.DoNotValidateWorkflow)
{
await operation.CheckUpdateAsync();
}
if (c.EnrichDefaults)
{
newData = operation.GenerateDefaultValues(newData);
}
if (newData.Equals(Snapshot.EditingData))
{
return;
}
if (!c.DoNotScript)
{
newData = await operation.ExecuteUpdateScriptAsync(newData, ct);
}
if (!c.DoNotValidate)
{
await operation.ValidateContentAsync(newData, c.OptimizeValidation, Snapshot.IsPublished, ct);
}
Update(c, newData);
}
private async Task PatchCore(UpdateContent c, ContentOperation operation,
CancellationToken ct)
{
operation.MustHavePermission(PermissionIds.AppContentsUpdate);
operation.MustHaveData(c.Data);
c.Data = operation.InvokeUpdates(c.Data, Snapshot.EditingData);
if (!c.DoNotValidate)
{
await operation.ValidateInputPartialAsync(c.Data, c.OptimizeValidation, Snapshot.IsPublished, ct);
}
if (!c.DoNotValidateWorkflow)
{
await operation.CheckUpdateAsync();
}
var newData = c.Data.MergeInto(Snapshot.EditingData);
if (newData.Equals(Snapshot.EditingData))
{
return;
}
if (!c.DoNotScript)
{
newData = await operation.ExecuteUpdateScriptAsync(newData, ct);
}
if (!c.DoNotValidate)
{
await operation.ValidateContentAsync(newData, c.OptimizeValidation, Snapshot.IsPublished, ct);
}
Update(c, newData);
}
private void CancelChangeCore(CancelContentSchedule c, ContentOperation operation)
{
operation.MustHavePermission(PermissionIds.AppContentsChangeStatusCancel);
if (Snapshot.ScheduleJob != null)
{
CancelChangeStatus(c);
}
}
private async Task ValidateCore(ContentOperation operation,
CancellationToken ct)
{
operation.MustHavePermission(PermissionIds.AppContentsRead);
await operation.ValidateContentAndInputAsync(Snapshot.EditingData, false, Snapshot.IsPublished, ct);
}
private async Task CreateDraftCore(CreateContentDraft c, ContentOperation operation)
{
operation.MustHavePermission(PermissionIds.AppContentsVersionCreate);
operation.MustCreateDraft();
var status = await operation.GetInitialStatusAsync();
CreateDraft(c, status);
}
private void DeleteDraftCore(DeleteContentDraft c, ContentOperation operation)
{
operation.MustHavePermission(PermissionIds.AppContentsVersionDelete);
operation.MustDeleteDraft();
DeleteDraft(c);
}
private async Task DeleteCore(DeleteContent c, ContentOperation operation,
CancellationToken ct)
{
operation.MustHavePermission(PermissionIds.AppContentsDelete);
operation.MustNotDeleteSingleton();
if (!c.DoNotScript)
{
await operation.ExecuteDeleteScriptAsync(c.Permanent, ct);
}
if (c.CheckReferrers)
{
await operation.CheckReferrersAsync(ct);
}
Delete(c);
}
private void Create(CreateContent command, Status status)
{
var @event = SimpleMapper.Map(command, new ContentCreated());
@event.Status = status;
RaiseEvent(Envelope.Create(@event));
}
private void Update(ContentCommand command, ContentData data)
{
Raise(command, new ContentUpdated { Data = data });
}
private void ChangeStatus(ChangeContentStatus command)
{
Raise(command, new ContentStatusChanged { Change = GetChange(command.Status) });
}
private void ChangeStatusScheduled(ChangeContentStatus command, Instant dueTime)
{
Raise(command, new ContentStatusScheduled { DueTime = dueTime });
}
private void CancelChangeStatus(ContentCommand command)
{
Raise(command, new ContentSchedulingCancelled());
}
private void CreateDraft(CreateContentDraft command, Status status)
{
Raise(command, new ContentDraftCreated { Status = status });
}
private void Delete(DeleteContent command)
{
Raise(command, new ContentDeleted());
}
private void DeleteDraft(DeleteContentDraft command)
{
Raise(command, new ContentDraftDeleted());
}
private void Raise<T, TEvent>(T command, TEvent @event) where T : class where TEvent : AppEvent
{
RaiseEvent(Envelope.Create(SimpleMapper.Map(command, @event)));
}
private StatusChange GetChange(Status status)
{
if (status == Status.Published)
{
return StatusChange.Published;
}
else if (Snapshot.IsPublished)
{
return StatusChange.Unpublished;
}
else
{
return StatusChange.Change;
}
}
}