|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Collections.Immutable; |
| 3 | +using System.Linq; |
| 4 | +using JetBrains.Annotations; |
| 5 | +using JsonApiDotNetCore.Configuration; |
| 6 | +using JsonApiDotNetCore.Queries.Expressions; |
| 7 | +using JsonApiDotNetCore.Queries.Internal; |
| 8 | +using JsonApiDotNetCore.Resources; |
| 9 | +using JsonApiDotNetCore.Resources.Annotations; |
| 10 | + |
| 11 | +namespace JsonApiDotNetCore.Queries |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Default implementation of the <see cref="INoSqlQueryLayerComposer" />. |
| 15 | + /// </summary> |
| 16 | + /// <remarks> |
| 17 | + /// Register <see cref="NoSqlQueryLayerComposer"/> with the service container as |
| 18 | + /// shown in the following example. |
| 19 | + /// </remarks> |
| 20 | + /// <example> |
| 21 | + /// <code><![CDATA[ |
| 22 | + /// public class Startup |
| 23 | + /// { |
| 24 | + /// public void ConfigureServices(IServiceCollection services) |
| 25 | + /// { |
| 26 | + /// services.AddNoSqlResourceServices(); |
| 27 | + /// } |
| 28 | + /// } |
| 29 | + /// ]]></code> |
| 30 | + /// </example> |
| 31 | + [PublicAPI] |
| 32 | + public class NoSqlQueryLayerComposer : QueryLayerComposer, INoSqlQueryLayerComposer |
| 33 | + { |
| 34 | + private readonly IEnumerable<IQueryConstraintProvider> _constraintProviders; |
| 35 | + private readonly ITargetedFields _targetedFields; |
| 36 | + |
| 37 | + public NoSqlQueryLayerComposer( |
| 38 | + IEnumerable<IQueryConstraintProvider> constraintProviders, |
| 39 | + IResourceDefinitionAccessor resourceDefinitionAccessor, |
| 40 | + IJsonApiOptions options, |
| 41 | + IPaginationContext paginationContext, |
| 42 | + ITargetedFields targetedFields, |
| 43 | + IEvaluatedIncludeCache evaluatedIncludeCache, |
| 44 | + ISparseFieldSetCache sparseFieldSetCache) : base(constraintProviders, resourceDefinitionAccessor, options, paginationContext, targetedFields, evaluatedIncludeCache, sparseFieldSetCache) |
| 45 | + { |
| 46 | + _constraintProviders = constraintProviders; |
| 47 | + _targetedFields = targetedFields; |
| 48 | + } |
| 49 | + |
| 50 | + /// <inheritdoc /> |
| 51 | + public FilterExpression? GetPrimaryFilterFromConstraintsForNoSql(ResourceType primaryResourceType) |
| 52 | + { |
| 53 | + return GetPrimaryFilterFromConstraints(primaryResourceType); |
| 54 | + } |
| 55 | + |
| 56 | + /// <inheritdoc /> |
| 57 | + public (QueryLayer QueryLayer, IncludeExpression Include) ComposeFromConstraintsForNoSql(ResourceType requestResourceType) |
| 58 | + { |
| 59 | + QueryLayer queryLayer = ComposeFromConstraints(requestResourceType); |
| 60 | + IncludeExpression include = queryLayer.Include ?? IncludeExpression.Empty; |
| 61 | + |
| 62 | + queryLayer.Include = IncludeExpression.Empty; |
| 63 | + queryLayer.Projection = null; |
| 64 | + |
| 65 | + return (queryLayer, include); |
| 66 | + } |
| 67 | + |
| 68 | + /// <inheritdoc /> |
| 69 | + public (QueryLayer QueryLayer, IncludeExpression Include) ComposeForGetByIdWithConstraintsForNoSql<TId>( |
| 70 | + TId id, |
| 71 | + ResourceType primaryResourceType, |
| 72 | + TopFieldSelection fieldSelection) |
| 73 | + where TId : notnull |
| 74 | + { |
| 75 | + QueryLayer queryLayer = ComposeForGetById(id, primaryResourceType, fieldSelection); |
| 76 | + IncludeExpression include = queryLayer.Include ?? IncludeExpression.Empty; |
| 77 | + |
| 78 | + queryLayer.Include = IncludeExpression.Empty; |
| 79 | + queryLayer.Projection = null; |
| 80 | + |
| 81 | + return (queryLayer, include); |
| 82 | + } |
| 83 | + |
| 84 | + /// <inheritdoc /> |
| 85 | + public QueryLayer ComposeForGetByIdForNoSql<TId>(TId id, ResourceType primaryResourceType) |
| 86 | + where TId : notnull |
| 87 | + { |
| 88 | + return new QueryLayer(primaryResourceType) |
| 89 | + { |
| 90 | + Filter = new ComparisonExpression( |
| 91 | + ComparisonOperator.Equals, |
| 92 | + new ResourceFieldChainExpression( |
| 93 | + primaryResourceType.Fields.Single(f => f.Property.Name == nameof(IIdentifiable<TId>.Id))), |
| 94 | + new LiteralConstantExpression(id.ToString()!)), |
| 95 | + Include = IncludeExpression.Empty, |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + /// <inheritdoc /> |
| 100 | + public (QueryLayer QueryLayer, IncludeExpression Include) ComposeFromConstraintsAndFilterForNoSql( |
| 101 | + ResourceType requestResourceType, |
| 102 | + string propertyName, |
| 103 | + string propertyValue, |
| 104 | + bool isIncluded) |
| 105 | + { |
| 106 | + // Compose a secondary resource filter in the form "equals({propertyName},'{propertyValue}')". |
| 107 | + FilterExpression[] secondaryResourceFilterExpressions = |
| 108 | + { |
| 109 | + ComposeSecondaryResourceFilter(requestResourceType, propertyName, propertyValue), |
| 110 | + }; |
| 111 | + |
| 112 | + // Get the query expressions from the request. |
| 113 | + ExpressionInScope[] constraints = _constraintProviders |
| 114 | + .SelectMany(provider => provider.GetConstraints()) |
| 115 | + .ToArray(); |
| 116 | + |
| 117 | + bool IsQueryLayerConstraint(ExpressionInScope constraint) |
| 118 | + { |
| 119 | + return constraint.Expression is not IncludeExpression && |
| 120 | + (!isIncluded || (constraint.Scope is not null && |
| 121 | + constraint.Scope.Fields.Any(field => field.PublicName == requestResourceType.PublicName))); |
| 122 | + } |
| 123 | + |
| 124 | + IEnumerable<QueryExpression> requestQueryExpressions = constraints |
| 125 | + .Where(IsQueryLayerConstraint) |
| 126 | + .Select(constraint => constraint.Expression); |
| 127 | + |
| 128 | + // Combine the secondary resource filter and request query expressions and |
| 129 | + // create the query layer from the combined query expressions. |
| 130 | + QueryExpression[] queryExpressions = secondaryResourceFilterExpressions |
| 131 | + .Concat(requestQueryExpressions) |
| 132 | + .ToArray(); |
| 133 | + |
| 134 | + var queryLayer = new QueryLayer(requestResourceType) |
| 135 | + { |
| 136 | + Include = IncludeExpression.Empty, |
| 137 | + Filter = GetFilter(queryExpressions, requestResourceType), |
| 138 | + Sort = GetSort(queryExpressions, requestResourceType), |
| 139 | + Pagination = GetPagination(queryExpressions, requestResourceType), |
| 140 | + }; |
| 141 | + |
| 142 | + // Retrieve the IncludeExpression from the constraints collection. |
| 143 | + // There will be zero or one IncludeExpression, even if multiple include query |
| 144 | + // parameters were specified in the request. JsonApiDotNetCore combines those |
| 145 | + // into a single expression. |
| 146 | + IncludeExpression include = isIncluded |
| 147 | + ? IncludeExpression.Empty |
| 148 | + : constraints |
| 149 | + .Select(constraint => constraint.Expression) |
| 150 | + .OfType<IncludeExpression>() |
| 151 | + .DefaultIfEmpty(IncludeExpression.Empty) |
| 152 | + .Single(); |
| 153 | + |
| 154 | + return (queryLayer, include); |
| 155 | + } |
| 156 | + |
| 157 | + private static FilterExpression ComposeSecondaryResourceFilter( |
| 158 | + ResourceType resourceType, |
| 159 | + string propertyName, |
| 160 | + string properyValue) |
| 161 | + { |
| 162 | + return new ComparisonExpression( |
| 163 | + ComparisonOperator.Equals, |
| 164 | + new ResourceFieldChainExpression(resourceType.Fields.Single(f => f.Property.Name == propertyName)), |
| 165 | + new LiteralConstantExpression(properyValue)); |
| 166 | + } |
| 167 | + |
| 168 | + public (QueryLayer QueryLayer, IncludeExpression Include) ComposeForUpdateForNoSql<TId>( |
| 169 | + TId id, |
| 170 | + ResourceType primaryResourceType) |
| 171 | + where TId : notnull |
| 172 | + { |
| 173 | + // Create primary layer without an include expression. |
| 174 | + AttrAttribute primaryIdAttribute = GetIdAttribute(primaryResourceType); |
| 175 | + QueryLayer primaryLayer = new(primaryResourceType) |
| 176 | + { |
| 177 | + Include = IncludeExpression.Empty, |
| 178 | + Filter = new ComparisonExpression( |
| 179 | + ComparisonOperator.Equals, |
| 180 | + new ResourceFieldChainExpression(primaryIdAttribute), |
| 181 | + new LiteralConstantExpression(id.ToString()!)), |
| 182 | + }; |
| 183 | + |
| 184 | + // Create a separate include expression. |
| 185 | + ImmutableHashSet<IncludeElementExpression> includeElements = _targetedFields.Relationships |
| 186 | + .Select(relationship => new IncludeElementExpression(relationship)) |
| 187 | + .ToImmutableHashSet(); |
| 188 | + |
| 189 | + IncludeExpression include = includeElements.Any() ? new IncludeExpression(includeElements) : IncludeExpression.Empty; |
| 190 | + |
| 191 | + return (primaryLayer, include); |
| 192 | + } |
| 193 | + |
| 194 | + private static AttrAttribute GetIdAttribute(ResourceType resourceType) |
| 195 | + { |
| 196 | + return resourceType.GetAttributeByPropertyName(nameof(Identifiable<object>.Id)); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments