|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using JetBrains.Annotations; |
| 7 | +using JsonApiDotNetCore; |
| 8 | +using JsonApiDotNetCore.Configuration; |
| 9 | +using JsonApiDotNetCore.Errors; |
| 10 | +using JsonApiDotNetCore.Middleware; |
| 11 | +using JsonApiDotNetCore.Queries; |
| 12 | +using JsonApiDotNetCore.Repositories; |
| 13 | +using JsonApiDotNetCore.Resources; |
| 14 | +using JsonApiDotNetCore.Resources.Annotations; |
| 15 | +using Microsoft.AspNetCore.Authentication; |
| 16 | +using Microsoft.EntityFrameworkCore; |
| 17 | +using Microsoft.Extensions.Logging; |
| 18 | + |
| 19 | +namespace JsonApiDotNetCoreExampleTests.IntegrationTests.SoftDeletion |
| 20 | +{ |
| 21 | + [UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)] |
| 22 | + public class SoftDeletionAwareRepository<TResource, TId> : EntityFrameworkCoreRepository<TResource, TId> |
| 23 | + where TResource : class, IIdentifiable<TId> |
| 24 | + { |
| 25 | + private readonly CollectionConverter _collectionConverter = new CollectionConverter(); |
| 26 | + private readonly IJsonApiRequest _request; |
| 27 | + private readonly ISystemClock _systemClock; |
| 28 | + private readonly ITargetedFields _targetedFields; |
| 29 | + private readonly DbContext _dbContext; |
| 30 | + |
| 31 | + public SoftDeletionAwareRepository(IJsonApiRequest request, ISystemClock systemClock, ITargetedFields targetedFields, |
| 32 | + IDbContextResolver contextResolver, IResourceGraph resourceGraph, IResourceFactory resourceFactory, |
| 33 | + IEnumerable<IQueryConstraintProvider> constraintProviders, ILoggerFactory loggerFactory) |
| 34 | + : base(targetedFields, contextResolver, resourceGraph, resourceFactory, constraintProviders, loggerFactory) |
| 35 | + { |
| 36 | + _request = request; |
| 37 | + _systemClock = systemClock; |
| 38 | + _targetedFields = targetedFields; |
| 39 | + _dbContext = contextResolver.GetContext(); |
| 40 | + } |
| 41 | + |
| 42 | + public override async Task CreateAsync(TResource resourceFromRequest, TResource resourceForDatabase, CancellationToken cancellationToken) |
| 43 | + { |
| 44 | + await AssertResourcesToAssignInRelationshipsExistAsync(resourceFromRequest, cancellationToken); |
| 45 | + |
| 46 | + await base.CreateAsync(resourceFromRequest, resourceForDatabase, cancellationToken); |
| 47 | + } |
| 48 | + |
| 49 | + public override async Task UpdateAsync(TResource resourceFromRequest, TResource resourceFromDatabase, CancellationToken cancellationToken) |
| 50 | + { |
| 51 | + await AssertResourcesToAssignInRelationshipsExistAsync(resourceFromRequest, cancellationToken); |
| 52 | + |
| 53 | + await base.UpdateAsync(resourceFromRequest, resourceFromDatabase, cancellationToken); |
| 54 | + } |
| 55 | + |
| 56 | + public override async Task SetRelationshipAsync(TResource primaryResource, object secondaryResourceIds, CancellationToken cancellationToken) |
| 57 | + { |
| 58 | + await AssertRelationshipRightValueExistsAsync(_request.Relationship, secondaryResourceIds, cancellationToken); |
| 59 | + |
| 60 | + await base.SetRelationshipAsync(primaryResource, secondaryResourceIds, cancellationToken); |
| 61 | + } |
| 62 | + |
| 63 | + public override async Task AddToToManyRelationshipAsync(TId primaryId, ISet<IIdentifiable> secondaryResourceIds, CancellationToken cancellationToken) |
| 64 | + { |
| 65 | + if (IsSoftDeletable(typeof(TResource))) |
| 66 | + { |
| 67 | + await AssertPrimaryResourceExistsAsync(primaryId, cancellationToken); |
| 68 | + } |
| 69 | + |
| 70 | + await AssertRelationshipRightValueExistsAsync(_request.Relationship, secondaryResourceIds, cancellationToken); |
| 71 | + |
| 72 | + await base.AddToToManyRelationshipAsync(primaryId, secondaryResourceIds, cancellationToken); |
| 73 | + } |
| 74 | + |
| 75 | + private async Task AssertResourcesToAssignInRelationshipsExistAsync(TResource resourceFromRequest, CancellationToken cancellationToken) |
| 76 | + { |
| 77 | + foreach (RelationshipAttribute relationship in _targetedFields.Relationships) |
| 78 | + { |
| 79 | + object rightValue = relationship.GetValue(resourceFromRequest); |
| 80 | + await AssertRelationshipRightValueExistsAsync(relationship, rightValue, cancellationToken); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private async Task AssertRelationshipRightValueExistsAsync(RelationshipAttribute relationship, object rightValue, CancellationToken cancellationToken) |
| 85 | + { |
| 86 | + if (IsSoftDeletable(relationship.RightType)) |
| 87 | + { |
| 88 | + ICollection<IIdentifiable> rightResources = _collectionConverter.ExtractResources(rightValue); |
| 89 | + object[] rightResourceIds = rightResources.Select(identifiable => identifiable.GetTypedId()).ToArray(); |
| 90 | + |
| 91 | + foreach (object rightResourceId in rightResourceIds) |
| 92 | + { |
| 93 | + object resource = await _dbContext.FindAsync(relationship.RightType, ArrayFactory.Create(rightResourceId), cancellationToken); |
| 94 | + |
| 95 | + if (resource == null) |
| 96 | + { |
| 97 | + throw new DataStoreUpdateException(new Exception("One or more related resources do not exist.")); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private async Task AssertPrimaryResourceExistsAsync(TId primaryId, CancellationToken cancellationToken) |
| 104 | + { |
| 105 | + _ = await GetPrimaryResourceByIdAsync(primaryId, cancellationToken); |
| 106 | + } |
| 107 | + |
| 108 | + public override async Task DeleteAsync(TId id, CancellationToken cancellationToken) |
| 109 | + { |
| 110 | + if (IsSoftDeletable(typeof(TResource))) |
| 111 | + { |
| 112 | + await SoftDeleteAsync(id, cancellationToken); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + await base.DeleteAsync(id, cancellationToken); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private async Task SoftDeleteAsync(TId id, CancellationToken cancellationToken) |
| 121 | + { |
| 122 | + var resource = (ISoftDeletable)await GetPrimaryResourceByIdAsync(id, cancellationToken); |
| 123 | + |
| 124 | + if (resource == null) |
| 125 | + { |
| 126 | + throw new ResourceNotFoundException(_request.PrimaryId, _request.PrimaryResource.PublicName); |
| 127 | + } |
| 128 | + |
| 129 | + resource.SoftDeletedAt = _systemClock.UtcNow; |
| 130 | + |
| 131 | + await _dbContext.SaveChangesAsync(cancellationToken); |
| 132 | + } |
| 133 | + |
| 134 | + private async Task<TResource> GetPrimaryResourceByIdAsync(TId id, CancellationToken cancellationToken) |
| 135 | + { |
| 136 | + var resource = await _dbContext.FindAsync<TResource>(ArrayFactory.Create((object)id), cancellationToken); |
| 137 | + |
| 138 | + if (resource == null) |
| 139 | + { |
| 140 | + throw new ResourceNotFoundException(_request.PrimaryId, _request.PrimaryResource.PublicName); |
| 141 | + } |
| 142 | + |
| 143 | + return resource; |
| 144 | + } |
| 145 | + |
| 146 | + private static bool IsSoftDeletable(Type resourceType) |
| 147 | + { |
| 148 | + return typeof(ISoftDeletable).IsAssignableFrom(resourceType); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + [UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)] |
| 153 | + public class SoftDeletionAwareRepository<TResource> : SoftDeletionAwareRepository<TResource, int>, IResourceRepository<TResource> |
| 154 | + where TResource : class, IIdentifiable<int> |
| 155 | + { |
| 156 | + public SoftDeletionAwareRepository(IJsonApiRequest request, ISystemClock systemClock, ITargetedFields targetedFields, |
| 157 | + IDbContextResolver contextResolver, IResourceGraph resourceGraph, IResourceFactory resourceFactory, |
| 158 | + IEnumerable<IQueryConstraintProvider> constraintProviders, ILoggerFactory loggerFactory) |
| 159 | + : base(request, systemClock, targetedFields, contextResolver, resourceGraph, resourceFactory, constraintProviders, loggerFactory) |
| 160 | + { |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments