public async Task Run() { WriteLine("1. Insert, pre trigger"); WriteLine("2. Update, pre trigger"); WriteLine("3. Create triggers"); SPOption option = (SPOption)ProgramHelper.EnterInt(""); switch (option) { case SPOption.InsertPreTrigger: { ZipModel model = new ZipModel() { City = "Sarajevo", Pop = 1854, State = "BiH", Loc = new Point(43.8607994, 18.4018904), Id = "989898" }; Document findZipModel = await _documentRepository.ReadDocumentByIdAsync <Document>(model.Id, UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)); if (findZipModel != null) { await _documentRepository.DeleteDocument(DatabaseId, CollectionId, findZipModel.Id); } Document document = await _documentRepository.InsertDocument(DatabaseId, CollectionId, model, new RequestOptions { PreTriggerInclude = new[] { "setCreatedDate" } }); Success("New document successfully created."); break; } case SPOption.ReplacePostTrigger: { ZipModel model = await _documentRepository.ReadDocumentByIdAsync <ZipModel>("989898", UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)); model.City = "Neum"; ZipModel updatedModel = await _documentRepository.UpdateDocument <ZipModel>(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, model.Id), model, new RequestOptions { PreTriggerInclude = new[] { "setUpdatedDate" } }); Success("Document with id >>> " + updatedModel.Id + " <<< updated."); Success("Created date time >>> " + updatedModel.CreatedDateTime.Value.ToString("MM/dd/yyyy hh:mm:ss tt") + " <<<."); Success("Updated date time >>> " + updatedModel.UpdatedDateTime.Value.ToString("MM/dd/yyyy hh:mm:ss tt") + " <<< updated."); break; } case SPOption.CreateTriggers: { Trigger setCreatedDate = new Trigger() { Id = "setCreatedDate", Body = File.ReadAllText(@"Scripts\trSetCreatedDate.js"), TriggerOperation = TriggerOperation.Create, TriggerType = TriggerType.Pre }; Trigger setUpdatedDate = new Trigger() { Id = "setUpdatedDate", Body = File.ReadAllText(@"Scripts\trSetUpdatedDate.js"), TriggerOperation = TriggerOperation.Update, TriggerType = TriggerType.Pre }; Trigger newCreatedTrigger = await _triggerRepository.CreateTriggerAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), setCreatedDate); WriteLine(string.Format("Created trigger {0}; RID: {1}", newCreatedTrigger.Id, newCreatedTrigger.ResourceId)); Trigger newUpdatedTrigger = await _triggerRepository.CreateTriggerAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), setUpdatedDate); WriteLine(string.Format("Created trigger {0}; RID: {1}", newUpdatedTrigger.Id, newUpdatedTrigger.ResourceId)); break; } } }
public async Task Run() { string databaseName = ""; DocumentCollection collectionName = null; if (!InsertCollAndDatabase(ref databaseName, ref collectionName)) { Warning("Collection >>> " + collectionName + " <<< don't exist."); return; } WriteLine("1. Call sp, insert StoreModel"); WriteLine("2. Create"); SPOption option = (SPOption)ProgramHelper.EnterInt(""); switch (option) { case SPOption.Read: { StoreModel storeModel = new StoreModel() { Address = new AddressModel() { AddressType = "Back Office", CountryRegionName = "Neum", Location = new LocationModel() { City = "Neum", StateProvinceName = "Jadran" }, PostalCode = "88390" }, Name = "Super new Network bank RVS" }; try { Document resultSP = await _baseRepository.RunStoredProcedureAsync(UriFactory.CreateStoredProcedureUri(databaseName, collectionName.Id, "spCreateDocIfIdIsUnique"), storeModel); ProgramHelper.Divider(); Success("Result: "); WriteLine(resultSP.ToString()); } catch (Exception ex) { Error(ex.Message); } break; } case SPOption.Create: { StoredProcedure sprocDefinition = new StoredProcedure { Id = "spGetCountByRegion", Body = File.ReadAllText(@"Scripts\spGetCountByRegion.js") }; StoredProcedure result = await _baseRepository.CreateStoredProcedureAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id), sprocDefinition); Success("Created stored procedure " + result.Id + " RID: " + result.ResourceId); /////////////////////////////////////////////// sprocDefinition.Id = "spCreateDocIfIdlsUnique"; sprocDefinition.Body = File.ReadAllText(@"Scripts\spCreateDocIfIdlsUnique.js"); result = await _baseRepository.CreateStoredProcedureAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id), sprocDefinition); Success("Created stored procedure " + result.Id + " RID: " + result.ResourceId); break; } } }