Beispiel #1
0
        //Update
        async Task ToggleExplored(string name)
        {
            ItemResponse <SolPlanet> resp = await this.container.ReadItemAsync <SolPlanet>(name, new PartitionKey("Sol"));

            SolPlanet itemBody = resp.Resource;

            itemBody.IsExplored = !itemBody.IsExplored;
            resp = await container.ReplaceItemAsync <SolPlanet>(itemBody, itemBody.Name, new PartitionKey("Sol"));

            Console.WriteLine($"Updated {name} - explored set up {itemBody.IsExplored} - response {resp}");
        }
Beispiel #2
0
        async Task AddPlanetIfDoesNotExist(SolPlanet p)
        {
            try
            {
                // Read the item to see if it exists.  The ID (unique) is Name property
                ItemResponse <SolPlanet> planetResponse = await this.container.ReadItemAsync <SolPlanet>(p.Name, new PartitionKey(p.Orbits));

                Console.WriteLine("Item in database with id: {0} already exists\n", planetResponse.Resource.Name);
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                ItemResponse <SolPlanet> planetResponse = await this.container.CreateItemAsync <SolPlanet>(p, new PartitionKey(p.Orbits));

                // Note that after creating the item, we can access the body of the item with the Resource property off the ItemResponse. We can also access the RequestCharge property to see the amount of RUs consumed on this request.
                Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.\n", planetResponse.Resource.Name, planetResponse.RequestCharge);
            }
        }
Beispiel #3
0
        public async Task Go()
        {
            this.cosmosClient = new CosmosClient(EndpointUri, PrimaryKey, new CosmosClientOptions()
            {
                ApplicationName = "ListOfPlanets"
            });

            // Create a new database
            this.database = await this.cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId);

            Console.WriteLine("Created Database: {0}\n", this.database.Id);

            //Create container
            this.container = await this.database.CreateContainerIfNotExistsAsync(containerId, "/Orbits", 400);

            //Add initial records
            await AddPlanetIfDoesNotExist(new SolPlanet("Mercury", 57.0));
            await AddPlanetIfDoesNotExist(new SolPlanet("Venus", 108.0));
            await AddPlanetIfDoesNotExist(new SolPlanet("Earth", 150.0, true));
            await AddPlanetIfDoesNotExist(new SolPlanet("Mars", 228.0));
            await AddPlanetIfDoesNotExist(new SolPlanet("Jupiter", 779.0));
            await AddPlanetIfDoesNotExist(new SolPlanet("Saturn", 1430.0));
            await AddPlanetIfDoesNotExist(new SolPlanet("Uranus", 2880.0));
            await AddPlanetIfDoesNotExist(new SolPlanet("Nepture", 4500.0));
            await AddPlanetIfDoesNotExist(new SolPlanet("Pluto", 5910.0));

            //Read back all records
            await QueryAllRecords(true);
            await QueryAllRecords(false);

            //Update record
            var mars = new SolPlanet("Mars", 228, true);

            await ToggleExplored("Mars");

            //Delete record
            await DeleteItemAsync("Uranus");
        }