private async Task <TResource> TryGetPrimaryResourceByIdAsync(TId id, TopFieldSelection fieldSelection, CancellationToken cancellationToken)
        {
            var primaryLayer = _queryLayerComposer.ComposeForGetById(id, _request.PrimaryResource, fieldSelection);

            var primaryResources = await _repositoryAccessor.GetAsync <TResource>(primaryLayer, cancellationToken);

            return(primaryResources.SingleOrDefault());
        }
Esempio n. 2
0
        protected async Task <TResource> GetPrimaryResourceByIdAsync(TId id, TopFieldSelection fieldSelection, CancellationToken cancellationToken)
        {
            TResource primaryResource = await TryGetPrimaryResourceByIdAsync(id, fieldSelection, cancellationToken);

            AssertPrimaryResourceExists(primaryResource);

            return(primaryResource);
        }
Esempio n. 3
0
        /// <inheritdoc />
        public QueryLayer ComposeForGetById <TId>(TId id, ResourceContext resourceContext, TopFieldSelection fieldSelection)
        {
            ArgumentGuard.NotNull(resourceContext, nameof(resourceContext));

            var idAttribute = GetIdAttribute(resourceContext);

            var queryLayer = ComposeFromConstraints(resourceContext);

            queryLayer.Sort       = null;
            queryLayer.Pagination = null;
            queryLayer.Filter     = CreateFilterByIds(id.AsArray(), idAttribute, queryLayer.Filter);

            if (fieldSelection == TopFieldSelection.OnlyIdAttribute)
            {
                queryLayer.Projection = new Dictionary <ResourceFieldAttribute, QueryLayer>
                {
                    [idAttribute] = null
                };
            }
            else if (fieldSelection == TopFieldSelection.WithAllAttributes && queryLayer.Projection != null)
            {
                // Discard any top-level ?fields[]= or attribute exclusions from resource definition, because we need the full database row.
                while (queryLayer.Projection.Any(pair => pair.Key is AttrAttribute))
                {
                    queryLayer.Projection.Remove(queryLayer.Projection.First(pair => pair.Key is AttrAttribute));
                }
            }

            return(queryLayer);
        }