Esempio n. 1
0
        /// <summary>
        /// Gets the trigger, or creates a new one if one with the specified trigger.id doesn't exist.
        /// </summary>
        /// <param name="dbId">The id of the Database to search for, or create.</param>
        /// <param name="collectionId">The id of the document db collection.</param>
        /// <param name="trigger">The trigger to get or create.</param>
        /// <param name="deleteTrigger">Indicator to delete the trigger before creating it.</param>
        /// <returns>The matched, or created, Trigger object</returns>
        public async Task <Trigger> GetOrCreateTriggerAsync(
            string dbId,
            string collectionId,
            Trigger trigger,
            bool deleteTrigger = false)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b14e /* tag_961fo */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b14f /* tag_961fp */));
            Code.ExpectsArgument(trigger, nameof(trigger), TaggingUtilities.ReserveTag(0x2381b150 /* tag_961fq */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            Uri colUri = UriFactory.CreateDocumentCollectionUri(dbId, collectionId);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b151 /* tag_961fr */),
                                                              async() =>
            {
                try
                {
                    if (deleteTrigger)
                    {
                        await DeleteTriggerAsync(dbId, collectionId, trigger.Id).ConfigureAwait(false);
                    }

                    Trigger tr = client.CreateTriggerQuery(colUri).Where(t => t.Id == trigger.Id).AsEnumerable().FirstOrDefault();

                    if (tr != null)
                    {
                        return tr;
                    }

                    return await client.CreateTriggerAsync(colUri, trigger).ConfigureAwait(false);
                }
                catch (DocumentClientException ex)
                {
                    if (ex.StatusCode != HttpStatusCode.Conflict)
                    {
                        throw;
                    }

                    return client.CreateTriggerQuery(colUri)
                    .Where(p => p.Id == trigger.Id).AsEnumerable().FirstOrDefault();
                }
            }));
        }
Esempio n. 2
0
        private static async Task <Trigger> CreateTrigger(
            IDocumentClient client,
            string triggerId,
            string triggerBody,
            TriggerType triggerType,
            TriggerOperation triggerOperation)
        {
            Trigger triggerDefinition = new Trigger
            {
                Id               = triggerId,
                Body             = triggerBody,
                TriggerType      = triggerType,
                TriggerOperation = triggerOperation
            };

            ResourceResponse <Trigger> result = await client.CreateTriggerAsync(MyStoreCollectionUri, triggerDefinition);

            Trigger trigger = result.Resource;

            Console.WriteLine($" Created trigger {trigger.Id}; RID: {trigger.ResourceId}");

            return(trigger);
        }