-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tryout: tracking versions in atomic:operations
- Loading branch information
Bart Koelman
committed
Dec 1, 2021
1 parent
48d8c12
commit cee13f6
Showing
13 changed files
with
518 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Resources; | ||
|
||
namespace JsonApiDotNetCore.AtomicOperations | ||
{ | ||
public interface IVersionTracker | ||
{ | ||
bool RequiresVersionTracking(); | ||
|
||
void CaptureVersions(ResourceType resourceType, IIdentifiable resource); | ||
|
||
string? GetVersion(ResourceType resourceType, string stringId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Middleware; | ||
using JsonApiDotNetCore.Resources; | ||
using JsonApiDotNetCore.Resources.Annotations; | ||
|
||
namespace JsonApiDotNetCore.AtomicOperations | ||
{ | ||
public sealed class VersionTracker : IVersionTracker | ||
{ | ||
private static readonly CollectionConverter CollectionConverter = new(); | ||
|
||
private readonly ITargetedFields _targetedFields; | ||
private readonly IJsonApiRequest _request; | ||
private readonly Dictionary<string, string> _versionPerResource = new(); | ||
|
||
public VersionTracker(ITargetedFields targetedFields, IJsonApiRequest request) | ||
{ | ||
ArgumentGuard.NotNull(targetedFields, nameof(targetedFields)); | ||
ArgumentGuard.NotNull(request, nameof(request)); | ||
|
||
_targetedFields = targetedFields; | ||
_request = request; | ||
} | ||
|
||
public bool RequiresVersionTracking() | ||
{ | ||
if (_request.Kind != EndpointKind.AtomicOperations) | ||
{ | ||
return false; | ||
} | ||
|
||
return _request.PrimaryResourceType!.IsVersioned || _targetedFields.Relationships.Any(relationship => relationship.RightType.IsVersioned); | ||
} | ||
|
||
public void CaptureVersions(ResourceType resourceType, IIdentifiable resource) | ||
{ | ||
if (_request.Kind == EndpointKind.AtomicOperations) | ||
{ | ||
if (resourceType.IsVersioned) | ||
{ | ||
string? leftVersion = resource.GetVersion(); | ||
SetVersion(resourceType, resource.StringId!, leftVersion); | ||
} | ||
|
||
foreach (var relationship in _targetedFields.Relationships) | ||
{ | ||
if (relationship.RightType.IsVersioned) | ||
{ | ||
CaptureVersionsInRelationship(resource, relationship); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void CaptureVersionsInRelationship(IIdentifiable resource, RelationshipAttribute relationship) | ||
{ | ||
object? afterRightValue = relationship.GetValue(resource); | ||
ICollection<IIdentifiable> afterRightResources = CollectionConverter.ExtractResources(afterRightValue); | ||
|
||
foreach (var rightResource in afterRightResources) | ||
{ | ||
string? rightVersion = rightResource.GetVersion(); | ||
SetVersion(relationship.RightType, rightResource.StringId!, rightVersion); | ||
} | ||
} | ||
|
||
private void SetVersion(ResourceType resourceType, string stringId, string? version) | ||
{ | ||
string key = GetKey(resourceType, stringId); | ||
|
||
if (version == null) | ||
{ | ||
_versionPerResource.Remove(key); | ||
} | ||
else | ||
{ | ||
_versionPerResource[key] = version; | ||
} | ||
} | ||
|
||
public string? GetVersion(ResourceType resourceType, string stringId) | ||
{ | ||
string key = GetKey(resourceType, stringId); | ||
return _versionPerResource.TryGetValue(key, out string? version) ? version : null; | ||
} | ||
|
||
private string GetKey(ResourceType resourceType, string stringId) | ||
{ | ||
return $"{resourceType.PublicName}::{stringId}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.