Esempio n. 1
0
        /// <summary>
        /// Adds a link for this descriptor.
        /// </summary>
        /// <param name="apiOperationLink">The link to add.</param>
        /// <exception cref="NotImplementedException">If the link has not been created for this descriptor.</exception>
        public void AddLink(ApiOperationLink apiOperationLink)
        {
            if (apiOperationLink.OperationDescriptor != this)
            {
                throw new InvalidOperationException($"The ApiOperationLink MUST have been created for this ApiOperationDescriptor, but was instead created for the description {apiOperationLink.OperationDescriptor}");
            }

            this._links.Add(apiOperationLink);
        }
Esempio n. 2
0
        /// <summary>
        /// Registers the specified link, checking that the link represents a unique combination of HTTP method
        /// and URL format to avoid clashes.
        /// </summary>
        /// <remarks>
        /// In addition the link <see cref="ApiOperationLink.Rel" /> property will be checked to ensure no two links with
        /// the same rel can be associated with a single resource type.
        /// </remarks>
        /// <param name="link">The link to register.</param>
        /// <exception cref="InvalidOperationException">If the link is not unique.</exception>
        private void RegisterLink(ApiOperationLink link)
        {
            var existing = this._allLinks.Where(l =>
                                                l.UrlFormat.Equals(link.UrlFormat, StringComparison.CurrentCultureIgnoreCase) &&
                                                l.OperationDescriptor.Name == link.OperationDescriptor.Name)
                           .ToList();

            if (existing.Any())
            {
                throw new InvalidOperationException(
                          $"Could not register {link} from {link.OperationDescriptor.Source} as it conflicts with existing Link registrations:\n\n" +
                          string.Join("\n", existing.Select(e => $" {e} sourced from {e.OperationDescriptor.Source}")));
            }

            this._allLinks.Add(link);

            if (!link.IsRootLink)
            {
                if (!this._resourceTypeToLinks.ContainsKey(link.ResourceType))
                {
                    this._resourceTypeToLinks[link.ResourceType] = new List <ApiOperationLink>();
                }

                var existingLink = this._resourceTypeToLinks[link.ResourceType].SingleOrDefault(l => l.Rel == link.Rel);

                if (existingLink != null)
                {
                    throw new InvalidOperationException(
                              $"Duplicate link found for resource type {link.ResourceType.Name} with rel {link.Rel}. Existing link is for the operation: \n\n" +
                              $"  {existingLink.OperationDescriptor.OperationType.FullName}: {existingLink.UrlFormat}\n\n" +
                              "Every link must be a unique pairing of resource type (i.e. UserApiResource) and rel (i.e. \"self\" or \"activate\"");
                }

                this._resourceTypeToLinks[link.ResourceType].Add(link);
            }

            if (!this._operationTypeToLinks.ContainsKey(link.OperationDescriptor.OperationType))
            {
                this._operationTypeToLinks[link.OperationDescriptor.OperationType] = new List <ApiOperationLink>();
            }

            this._operationTypeToLinks[link.OperationDescriptor.OperationType].Add(link);
        }
Esempio n. 3
0
        /// <summary>
        /// Registers the specified link, checking that the link represents a unique combination of HTTP method
        /// and URL format to avoid clashes.
        /// </summary>
        /// <remarks>
        /// In addition the link <see cref="ApiOperationLink.Rel" /> property will be checked to ensure no two links with
        /// the same rel can be associated with a single resource type.
        /// </remarks>
        /// <param name="link">The link to register.</param>
        /// <exception cref="InvalidOperationException">If the link is not unique.</exception>
        private void RegisterLink(ApiOperationLink link)
        {
            var existing = this._allLinks.Where(l =>
                                                l.UrlFormat.Equals(link.UrlFormat, StringComparison.CurrentCultureIgnoreCase) &&
                                                l.OperationDescriptor.Name == link.OperationDescriptor.Name)
                           .ToList();

            if (existing.Any())
            {
                throw new InvalidOperationException(
                          $"Could not register {link} from {link.OperationDescriptor.Source} as it conflicts with existing Link registrations:\n\n" +
                          string.Join("\n", existing.Select(e => $" {e} sourced from {e.OperationDescriptor.Source}")));
            }

            this._allLinks.Add(link);

            if (!link.IsRootLink)
            {
                if (!this._resourceTypeToLinks.ContainsKey(link.ResourceType))
                {
                    this._resourceTypeToLinks[link.ResourceType] = new List <ApiOperationLink>();
                }

                if (this._resourceTypeToLinks[link.ResourceType].Any(l => l.Rel == link.Rel))
                {
                    throw new InvalidOperationException(
                              $"Duplicate link found for resource type {link.ResourceType.Name} with rel {link.Rel}");
                }

                this._resourceTypeToLinks[link.ResourceType].Add(link);
            }

            if (!this._operationTypeToLinks.ContainsKey(link.OperationDescriptor.OperationType))
            {
                this._operationTypeToLinks[link.OperationDescriptor.OperationType] = new List <ApiOperationLink>();
            }

            this._operationTypeToLinks[link.OperationDescriptor.OperationType].Add(link);
        }