/
OperationContainer.cs
63 lines (50 loc) · 2.11 KB
/
OperationContainer.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
using JetBrains.Annotations;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Resources.Annotations;
namespace JsonApiDotNetCore.Resources;
/// <summary>
/// Represents a write operation on a JSON:API resource.
/// </summary>
[PublicAPI]
public sealed class OperationContainer
{
private static readonly CollectionConverter CollectionConverter = new();
public IIdentifiable Resource { get; }
public ITargetedFields TargetedFields { get; }
public IJsonApiRequest Request { get; }
public OperationContainer(IIdentifiable resource, ITargetedFields targetedFields, IJsonApiRequest request)
{
ArgumentGuard.NotNull(resource);
ArgumentGuard.NotNull(targetedFields);
ArgumentGuard.NotNull(request);
Resource = resource;
TargetedFields = targetedFields;
Request = request;
}
public void SetTransactionId(string transactionId)
{
((JsonApiRequest)Request).TransactionId = transactionId;
}
public OperationContainer WithResource(IIdentifiable resource)
{
ArgumentGuard.NotNull(resource);
return new OperationContainer(resource, TargetedFields, Request);
}
#pragma warning disable AV1130 // Return type in method signature should be an interface to an unchangeable collection
public ISet<IIdentifiable> GetSecondaryResources()
#pragma warning restore AV1130 // Return type in method signature should be an interface to an unchangeable collection
{
var secondaryResources = new HashSet<IIdentifiable>(IdentifiableComparer.Instance);
foreach (RelationshipAttribute relationship in TargetedFields.Relationships)
{
AddSecondaryResources(relationship, secondaryResources);
}
return secondaryResources;
}
private void AddSecondaryResources(RelationshipAttribute relationship, HashSet<IIdentifiable> secondaryResources)
{
object? rightValue = relationship.GetValue(Resource);
IReadOnlyCollection<IIdentifiable> rightResources = CollectionConverter.ExtractResources(rightValue);
secondaryResources.UnionWith(rightResources);
}
}