Skip to content

Commit

Permalink
feat: reading a user
Browse files Browse the repository at this point in the history
  • Loading branch information
rupeshtiwari committed Mar 12, 2021
1 parent 401020d commit fa6fae9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
18 changes: 17 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json;

namespace azure_cosmos_db_example {
class Program {
Expand Down Expand Up @@ -40,6 +40,7 @@ class Program {

await this.CreateUserDocumentIfNotExists ("customers", "users", new UserData ().nelapin);
await this.CreateUserDocumentIfNotExists ("customers", "users", new UserData ().yanhe);
await this.ReadUserDocument ("customers", "users", new UserData ().yanhe);
}

private async Task CreateUserDocumentIfNotExists (string databaseName, string collectionName, User user) {
Expand All @@ -62,5 +63,20 @@ class Program {
Console.WriteLine ("Press any key to continue ...");
Console.ReadKey ();
}

private async Task ReadUserDocument (string databaseName, string collectionName, User user) {
try {
var userResource = await this.client.ReadDocumentAsync (UriFactory.CreateDocumentUri (databaseName, collectionName, user.Id), new RequestOptions { PartitionKey = new PartitionKey (user.UserId) });
var userFromDb = userResource.Resource;
this.WriteToConsoleAndPromptToContinue ("Read user {0}", user.Id);
this.WriteToConsoleAndPromptToContinue ("Read user {0}", Newtonsoft.Json.JsonConvert.SerializeObject (userFromDb, Formatting.Indented));
} catch (DocumentClientException de) {
if (de.StatusCode == HttpStatusCode.NotFound) {
this.WriteToConsoleAndPromptToContinue ("User {0} not read", user.Id);
} else {
throw;
}
}
}
}
}
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## Getting Started

1. Download the code to local box
2. Read below articles to setup and activate your free azure subscription
1. [Get Free Sandbox Azure account for learning Azure](http://www.rupeshtiwari.com/blog/azure-sandbox-free-account-for-learning/)
2. [Creating Cosmos DB from DotNet Core Project](http://www.rupeshtiwari.com/blog/creating-cosmos-db-from-dotnet-core-project/)
3. Run `dotnet restore` to install NuGet packages
4. Run `dotnet run` to create database.

## Creating Cosmos DB database and Collection from Dotnet Console App

Write **csharp** code to connect **cosmos DB**
Expand All @@ -6,8 +15,6 @@ Write **csharp** code to connect **cosmos DB**
2. You will create Collection in Cosmos DB




## Running App Locally

1. Install NuGet packages (This is only first time you have to do once you get the project from GitHub.)
Expand Down

0 comments on commit fa6fae9

Please sign in to comment.