-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathSoftDeletionAwareResourceService.cs
102 lines (85 loc) · 4.45 KB
/
SoftDeletionAwareResourceService.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
using JetBrains.Annotations;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Queries;
using JsonApiDotNetCore.Repositories;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Services;
using Microsoft.Extensions.Logging;
using TestBuildingBlocks;
namespace JsonApiDotNetCoreTests.IntegrationTests.SoftDeletion;
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public class SoftDeletionAwareResourceService<TResource, TId>(
ISystemClock systemClock, ITargetedFields targetedFields, IResourceRepositoryAccessor repositoryAccessor, IQueryLayerComposer queryLayerComposer,
IPaginationContext paginationContext, IJsonApiOptions options, ILoggerFactory loggerFactory, IJsonApiRequest request,
IResourceChangeTracker<TResource> resourceChangeTracker, IResourceDefinitionAccessor resourceDefinitionAccessor)
: JsonApiResourceService<TResource, TId>(repositoryAccessor, queryLayerComposer, paginationContext, options, loggerFactory, request, resourceChangeTracker,
resourceDefinitionAccessor)
where TResource : class, IIdentifiable<TId>
{
private readonly ISystemClock _systemClock = systemClock;
private readonly ITargetedFields _targetedFields = targetedFields;
private readonly IResourceRepositoryAccessor _repositoryAccessor = repositoryAccessor;
private readonly IJsonApiRequest _request = request;
// To optimize performance, the default resource service does not always fetch all resources on write operations.
// We do that here, to assure a 404 error is thrown for soft-deleted resources.
public override async Task<TResource?> CreateAsync(TResource resource, CancellationToken cancellationToken)
{
if (_targetedFields.Relationships.Any(relationship => IsSoftDeletable(relationship.RightType.ClrType)))
{
await AssertResourcesToAssignInRelationshipsExistAsync(resource, cancellationToken);
}
return await base.CreateAsync(resource, cancellationToken);
}
public override async Task<TResource?> UpdateAsync(TId id, TResource resource, CancellationToken cancellationToken)
{
if (_targetedFields.Relationships.Any(relationship => IsSoftDeletable(relationship.RightType.ClrType)))
{
await AssertResourcesToAssignInRelationshipsExistAsync(resource, cancellationToken);
}
return await base.UpdateAsync(id, resource, cancellationToken);
}
public override async Task SetRelationshipAsync(TId leftId, string relationshipName, object? rightValue, CancellationToken cancellationToken)
{
if (IsSoftDeletable(_request.Relationship!.RightType.ClrType))
{
await AssertRightResourcesExistAsync(rightValue, cancellationToken);
}
await base.SetRelationshipAsync(leftId, relationshipName, rightValue, cancellationToken);
}
public override async Task AddToToManyRelationshipAsync(TId leftId, string relationshipName, ISet<IIdentifiable> rightResourceIds,
CancellationToken cancellationToken)
{
if (IsSoftDeletable(typeof(TResource)))
{
_ = await GetPrimaryResourceByIdAsync(leftId, TopFieldSelection.OnlyIdAttribute, cancellationToken);
}
if (IsSoftDeletable(_request.Relationship!.RightType.ClrType))
{
await AssertRightResourcesExistAsync(rightResourceIds, cancellationToken);
}
await base.AddToToManyRelationshipAsync(leftId, relationshipName, rightResourceIds, cancellationToken);
}
public override async Task DeleteAsync(TId id, CancellationToken cancellationToken)
{
if (IsSoftDeletable(typeof(TResource)))
{
await SoftDeleteAsync(id, cancellationToken);
}
else
{
await base.DeleteAsync(id, cancellationToken);
}
}
private async Task SoftDeleteAsync(TId id, CancellationToken cancellationToken)
{
TResource resourceFromDatabase = await GetPrimaryResourceForUpdateAsync(id, cancellationToken);
((ISoftDeletable)resourceFromDatabase).SoftDeletedAt = _systemClock.UtcNow;
// A delete operation does not target any fields, so we can just pass resourceFromDatabase twice.
await _repositoryAccessor.UpdateAsync(resourceFromDatabase, resourceFromDatabase, cancellationToken);
}
private static bool IsSoftDeletable(Type resourceClrType)
{
return typeof(ISoftDeletable).IsAssignableFrom(resourceClrType);
}
}