Beispiel #1
0
        public async Task Run()
        {
            WriteLine("1. Use UDF");
            WriteLine("2. Create");

            UDFOption option = (UDFOption)ProgramHelper.EnterInt("");

            switch (option)
            {
            case UDFOption.Use:
            {
                // The function work with zips collection and msnet18sql database.
                // If you need a different collection or database, please write a piece of code to input data into the program.
                string       id    = ProgramHelper.EnterText("Id ");
                SqlQuerySpec query = new SqlQuerySpec()
                {
                    QueryText  = "SELECT c.id, c.loc, udf.udfCityState(c) as CityState FROM c where c.id = @id",
                    Parameters = new SqlParameterCollection()
                    {
                        new SqlParameter("@id", id)
                    }
                };

                Document document = _documentRepository.ReadDocumentByQuery <Document>(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), query).ToList().FirstOrDefault();

                WriteLine(document.ToString());

                break;
            }

            case UDFOption.Create:
            {
                UserDefinedFunction udfDefinition = new UserDefinedFunction
                {
                    Id   = "udfRegex",
                    Body = File.ReadAllText(@"Scripts\udfRegex.js")
                };

                string             databaseName   = "";
                DocumentCollection collectionName = null;

                if (!InsertCollAndDatabase(ref databaseName, ref collectionName))
                {
                    Warning("Collection >>> " + collectionName + " <<< don't exist.");
                    collectionName = await _collectionRepository.CreateCollection(databaseName, collectionName.Id);

                    ProgramHelper.Wait();
                }

                UserDefinedFunction newUDFunction = await _udfRepository.CreateUDFAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id), udfDefinition);

                WriteLine(string.Format("Created trigger {0}; RID: {1}", newUDFunction.Id, newUDFunction.ResourceId));

                break;
            }

            default:
                break;
            }
        }
Beispiel #2
0
        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 ReadDocument()
        {
            string             databaseName   = "";
            DocumentCollection collectionName = null;

            if (!InsertCollAndDatabase(ref databaseName, ref collectionName))
            {
                Warning("Collection >>> " + collectionName + " <<< don't exist.");
                return;
            }

            WriteLine("1. Dynamic");
            WriteLine("2. Document");
            WriteLine("3. Model");
            WriteLine("4. To JSON output");

            ReadOptionEnum option = (ReadOptionEnum)ProgramHelper.EnterInt("");
            string         id     = ProgramHelper.EnterText("Enter document id");

            switch (option)
            {
            case ReadOptionEnum.Dynamic:
            {
                dynamic documentDynamic = await _repository.ReadDocumentByIdAsync <dynamic>(id, UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id));

                WriteLine("Id:\t" + documentDynamic.id);
                WriteLine("Country code:\t" + documentDynamic.countrycode);
                WriteLine("Country name:\t" + documentDynamic.countryname);

                List <dynamic> lstMajorSector = documentDynamic.majorsector_percent.ToObject <List <dynamic> >();

                foreach (var majorSector in lstMajorSector)
                {
                    WriteLine("  Sector name:\t\t" + majorSector.Name);
                    WriteLine("  Sector percent:\t" + majorSector.Percent);
                    ProgramHelper.Divider();
                }

                break;
            }

            case ReadOptionEnum.Document:
            {
                Document document = await _repository.ReadDocumentByIdAsync <Document>(id, UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id));

                WriteLine("Id:\t" + document.GetPropertyValue <string>("id"));
                WriteLine("Country code:\t" + document.GetPropertyValue <string>("countrycode"));
                WriteLine("Country name:\t" + document.GetPropertyValue <string>("countryname"));
                JArray childArrayDocument = document.GetPropertyValue <JArray>("majorsector_percent");

                foreach (var item in childArrayDocument)
                {
                    WriteLine("  Sector name:\t\t" + item["Name"]);
                    WriteLine("  Sector percent:\t" + item["Percent"]);
                    ProgramHelper.Divider();
                }
                break;
            }

            case ReadOptionEnum.Model:
            {
                WorldBankModel model = await _repository.ReadDocumentByIdAsync <WorldBankModel>(id, UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id));

                WriteLine("Id:\t" + model.Id);
                WriteLine("Country code:\t" + model.Countrycode);
                WriteLine("Country name:\t" + model.Countryname);

                ProgramHelper.Divider();
                foreach (var majorSector in model.MajorsectorPercent)
                {
                    WriteLine("  Sector name:\t\t" + majorSector.Name);
                    WriteLine("  Sector percent:\t" + majorSector.Percent);
                    ProgramHelper.Divider();
                }

                break;
            }

            case ReadOptionEnum.ToJSONOutput:
            {
                Document document = await _repository.ReadDocumentByIdAsync <Document>(id, UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id));

                WriteLine(document.ToString());
                break;
            }

            default:
                break;
            }
        }
        public async Task InsertDocument()
        {
            string             databaseName   = "";
            DocumentCollection collectionName = null;

            if (!InsertCollAndDatabase(ref databaseName, ref collectionName))
            {
                Warning("Collection >>> " + collectionName + " <<< don't exist.");
                collectionName = await _collectionRepository.CreateCollection(databaseName, collectionName.Id);

                ProgramHelper.Wait();
            }

            WriteLine("1. New document from file");
            WriteLine("2. New document from anonymous type");
            WriteLine("3. New document from model");

            InsertOptionEnum option = (InsertOptionEnum)ProgramHelper.EnterInt("");

            switch (option)
            {
            case InsertOptionEnum.FromFile:
            {
                using (StreamReader file = new StreamReader(@"Scripts\zips.json"))
                {
                    string line;
                    int    count = 0;
                    while ((line = file.ReadLine()) != null && count != 100)
                    {
                        byte[] byteArray = Encoding.UTF8.GetBytes(line);
                        using (MemoryStream stream = new MemoryStream(byteArray))
                        {
                            Document newDocument = await _repository.InsertDocument(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id), JsonSerializable.LoadFrom <Document>(stream));

                            Success(++count + ", document import.");
                        }
                    }
                }

                break;
            }

            case InsertOptionEnum.FromAnonymous:
            {
                var newDocument = new
                {
                    firstName = "Ratomir",
                    lastName  = "Vukadin",
                    bookmarks = new[]
                    {
                        new { url = "https://try.dot.net/", name = "Try dot net" },
                        new { url = "https://dotnetfiddle.net/", name = "Dot net fiddle" }
                    },
                    city  = "TORONTO",
                    loc   = new Point(-80.632504, 40.473298),
                    pop   = 11981,
                    state = "OH"
                };

                Document createdNewDocument = await _repository.InsertDocument(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id), newDocument);

                Success("New document successfully created.");
                break;
            }

            case InsertOptionEnum.FromModel:
            {
                EmailModel emailModel = new EmailModel()
                {
                    Bcc = new List <string>()
                    {
                        "*****@*****.**"
                    },
                    Cc = new List <string>()
                    {
                        "*****@*****.**"
                    },
                    Ctype      = "text/plain; charset=us-ascii",
                    Date       = DateTime.Now,
                    Fname      = "Ratomir",
                    Folder     = "_inbox",
                    Fpath      = "enron_mail_20110402/maildir/lay-k/_sent/81.",
                    Mid        = "33068967.1075840285147.JavaMail.evans@thyme",
                    Recipients = new List <string>()
                    {
                        "*****@*****.**", "*****@*****.**", "*****@*****.**"
                    },
                    Replyto = null,
                    Sender  = "*****@*****.**",
                    Subject = "Re: EXECUTIVE COMMITTEE MEETING - MONDAY, JULY 10",
                    Text    = "Katherine:\n\nMr.Lay is planning on attending in person.\n\nTori\n\n\n\nKatherine Brown\n07 / 05 / 2000 01:15 PM\n\n\nTo: James M Bannantine / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Cliff \nBaxter / HOU / ECT@ECT, Sanjay Bhatnagar/ ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, \nRick Buy/ HOU / ECT@ECT, Richard Causey/ Corp / Enron@ENRON, Diomedes \nChristodoulou / SA / Enron@Enron, David W Delainey / HOU / ECT@ECT, James \nDerrick / Corp / Enron@ENRON, Andrew S Fastow / HOU / ECT@ECT, Peggy_Fowler @pgn.com, \nMark Frevert/ NA / Enron@Enron, Ben F Glisan / HOU / ECT@ECT, Kevin Hannon/ Enron \nCommunications @Enron Communications, David \nHaug / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Joe Hirko/ Enron \nCommunications @Enron Communications, Stanley Horton/ Corp / Enron@Enron, Larry L \nIzzo / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Steven J Kean / HOU / EES@EES, Mark \nKoenig / Corp / Enron@ENRON, Kenneth Lay/ Corp / Enron@ENRON, Rebecca P \nMark / HOU / AZURIX@AZURIX, Mike McConnell/ HOU / ECT@ECT, Rebecca \nMcDonald / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Jeffrey McMahon/ HOU / ECT@ECT, J \nMark Metts/ NA / Enron@Enron, Cindy Olson/ Corp / Enron@ENRON, Lou L \nPai / HOU / EES@EES, Ken Rice/ Enron Communications @Enron Communications, Jeffrey \nSherrick / Corp / Enron@ENRON, John Sherriff/ LON / ECT@ECT, Jeff \nSkilling / Corp / Enron@ENRON, Joseph W \nSutton / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Greg Whalley/ HOU / ECT@ECT, Thomas \nE White/ HOU / EES@EES, Brenda Garza-Castillo / NA / Enron@Enron, Marcia \nManarin / SA / Enron@Enron, Susan Skarness/ HOU / ECT@ECT, Stacy \nGuidroz / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Beena \nPradhan / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Karen K Heathman / HOU / ECT@ECT, \nSharron Westbrook/ Corp / Enron@ENRON, Kay Chapman/ HOU / ECT@ECT, Molly \nBobrow / NA / Enron@Enron, Rosane Fabozzi/ SA / Enron@Enron, Stephanie \nHarris / Corp / Enron@ENRON, Bridget Maronge/ HOU / ECT@ECT, Mary_trosper @pgn.com, \nNicki Daw/ NA / Enron@Enron, Inez Dauterive/ HOU / ECT@ECT, Carol Ann Brown / Enron \nCommunications @Enron Communications, Elaine \nRodriguez / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Nancy Young/ Enron \nCommunications @Enron Communications, Ann Joyner/ Corp / Enron@ENRON, Cindy \nStark / Corp / Enron@ENRON, Mary E Garza / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, \nMaureen McVicker/ HOU / EES@EES, Joannie Williamson/ Corp / Enron@ENRON, Rosalee \nFleming / Corp / Enron@ENRON, Vanessa Groscrand/ Corp / Enron@ENRON, Marsha \nLindsey / HOU / AZURIX@AZURIX, Cathy Phillips/ HOU / ECT@ECT, Loretta \nBrelsford / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Sue Ford/ HOU / ECT@ECT, Dolores \nFisher / NA / Enron@Enron, Karen Owens/ HOU / EES@EES, Dorothy Dalton/ Enron \nCommunications @Enron Communications, Jewel Meeks/ Enron Communications @Enron \nCommunications, Christina Grow/ Corp / Enron@ENRON, Lauren Urquhart/ LON / ECT@ECT, \nSherri Sera/ Corp / Enron@ENRON, Katherine Brown/ Corp / Enron@ENRON, Pam \nBenson / ENRON_DEVELOPMENT@ENRON_DEVELOPMENT, Jana Mills/ HOU / ECT@ECT, Liz M \nTaylor / HOU / ECT@ECT, Judy G Smith / HOU / EES@EES, Bobbie Power/ Corp / Enron@ENRON\ncc: Suzanne Danz/ Corp / Enron@ENRON, Videoconference @enron, Vanessa \nGroscrand / Corp / Enron@ENRON \nSubject: EXECUTIVE COMMITTEE MEETING - MONDAY, JULY 10\n\n\nExecutive Committee Weekly Meeting\nDate: Monday, July 10\nTime: 11:00 a.m. (CDT)\nLocation: 50th Floor Boardroom\nVideo: Connections will be established with remote locations upon request.\nConf call: AT & T lines have been reserved.  Please contact Sherri Sera \n(713 / 853 - 5984) \n or Katherine Brown(713 / 345 - 7774) for the weekly dial -in number and \npasscode.\n\nPlease indicate below whether or not you plan to attend this meeting and \nthrough what medium. \n\n Yes, I will attend in person _______\n\n By video conference from _______\n\n By conference call  _______\n\n No, I will not attend  _______\n\n * **\n\nPlease return this e - mail to me with your response by 12:00 p.m., Friday, \nJuly 7.\n\nThank you, \nKatherine\n\n",
                    To      = new List <string>()
                    {
                        "*****@*****.**"
                    }
                };

                Document newDocument = await _repository.InsertDocument(databaseName, collectionName.Id, emailModel);

                emailModel.Id = newDocument.Id;
                Success("New document successfully created.");
                Info("New Email model id: " + emailModel.Id);

                break;
            }

            default:
                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;
            }
            }
        }