Exemplo n.º 1
0
        public async Task AddLinks <T>(
            string sourceId,
            Entities.EntityType sourceType,
            Entities.LinkType linkType,
            string partitionId,
            IEnumerable <T> links) where T : Entities.Entry
        {
            Guard.AgainstNullOrWhitespace(nameof(sourceId), sourceId);
            Guard.AgainstNullOrWhitespace(nameof(partitionId), partitionId);

            await this.EnsureInitialized();

            string collectionLink = UriFactory.CreateDocumentCollectionUri(this.databaseName, this.collectionName).ToString();

            Entities.LinksCollection <T> result = this
                                                  .client
                                                  .CreateDocumentQuery <Entities.DefaultLinksCollection <T> >(
                collectionLink,
                new FeedOptions()
            {
                PartitionKey = new PartitionKey(partitionId)
            })
                                                  .Where(item => item.SourceId == sourceId && item.LinkType == linkType && item.SourceType == sourceType && item.IsFull == false)
                                                  .ToList()
                                                  .FirstOrDefault();

            if (result == null)
            {
                result = Entities.LinksCollection <T> .Create <T>(sourceId, partitionId, sourceType, linkType, links);

                await this.Create(result);

                return;
            }

            foreach (T link in links)
            {
                result.TargetEntities.Add(link);
            }

            await this.Update(result);

            // TODO: Check if the current page collection has exceeded collection size and create a new collection accordingly.
        }
Exemplo n.º 2
0
        public async Task RemoveLink <T>(
            string sourceId,
            Entities.EntityType sourceType,
            Entities.LinkType linkType,
            string partitionId,
            T link) where T : Entities.Entry
        {
            Guard.AgainstNullOrWhitespace(nameof(sourceId), sourceId);
            Guard.AgainstNullOrWhitespace(nameof(partitionId), partitionId);

            await this.EnsureInitialized();

            string collectionLink = UriFactory.CreateDocumentCollectionUri(this.databaseName, this.collectionName).ToString();

            Entities.LinksCollection <T> result = this
                                                  .client
                                                  .CreateDocumentQuery <Entities.DefaultLinksCollection <T> >(
                collectionLink,
                new FeedOptions()
            {
                PartitionKey = new PartitionKey(partitionId)
            })
                                                  .Where(item => item.SourceId == sourceId && item.LinkType == linkType && item.SourceType == sourceType && item.TargetEntities.Contains(link))
                                                  .ToList()
                                                  .FirstOrDefault();

            if (result == null)
            {
                // No such link.
                return;
            }

            T linkToRemove = result.TargetEntities.First(item => item.id == link.id);

            result.TargetEntities.Remove(linkToRemove);

            await this.Update(result);
        }