Exemple #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;
            }
        }
        public async Task UpdateDocument()
        {
            string             databaseName   = "";
            DocumentCollection collectionName = null;

            InsertCollAndDatabase(ref databaseName, ref collectionName);
            string         id    = ProgramHelper.EnterText("Enter document id");
            WorldBankModel model = await _repository.ReadDocumentByIdAsync <WorldBankModel>(id, UriFactory.CreateDocumentCollectionUri(databaseName, collectionName.Id));

            model.Status = "Inactive";

            Document updatedModel = await _repository.UpdateDocument <Document>(UriFactory.CreateDocumentUri(databaseName, collectionName.Id, model.Id), model);

            Success("Document with id >>> " + updatedModel.Id + " <<< updated.");
        }
        public async Task DeleteDocument()
        {
            string databaseName   = ProgramHelper.ReadDatabaseName();
            string collectionName = ProgramHelper.ReadCollectionName();
            string id             = ProgramHelper.EnterText("Enter document id");

            bool result = await _repository.DeleteDocument(databaseName, collectionName, id);

            if (result)
            {
                Success("Document deleted.");
            }
            else
            {
                Error("Document was not deleted.");
            }
        }
        public async Task Run()
        {
            ProgramHelper.EnterText("Start the exception program...");

            SqlQuerySpec query = new SqlQuerySpec()
            {
                QueryText = "SELECT *, c.InvalidProperty FROM c"
            };

            try
            {
                _documentRepository.ReadDocumentByQuery <Document>(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), query).ToList();
            }
            catch (AggregateException aex)
            {
                Error(aex.InnerException.Message);
            }
            catch (Exception ex)
            {
                Error(ex.Message);
            }
        }
        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;
            }
        }