Azure Cosmos DB CRUD using SQL API

In this post we can create a Web API sample using Cosmos DB.

(this is similar to the MVC Sample of MS)

Step 1: Create ASP.NET Core web application

image

Step 2: Choose API

image

Step 3: Add Controller

Name it as ItemController

image

Step 4: Add Nuget Package

image

Step 5: Create Class to represent Item

namespace CosmosDB_WebAPI.Models
{
     public class Item
     {
         [JsonProperty(PropertyName = “id”)]
         public string Id { get; set; }

        [JsonProperty(PropertyName = “name”)]
         public string Name { get; set; }

        [JsonProperty(PropertyName = “description”)]
         public string Description { get; set; }

        [JsonProperty(PropertyName = “isComplete”)]
         public bool Completed { get; set; }
     }
}

Step 6: Modify Controller Code

Add the following content to the ItemConroller to include GET, POST, PUT, DELETE functionalities.

namespace CosmosDB_WebAPI.Controllers
{
     [Route(“api/[controller]”)]
     [ApiController]
     public class ItemController : ControllerBase
     {
         private readonly ICosmosDbService _cosmosDbService;
         public ItemController(ICosmosDbService cosmosDbService)
         {
             _cosmosDbService = cosmosDbService;
         }

        // GET api/values
         [HttpGet]
         public async Task<IEnumerable<Item>> Get()
         {
             return await _cosmosDbService.GetItemsAsync(“SELECT * FROM c”);
         }

        // POST api/values
         [HttpPost]
         public async void Post([FromBody] Item item)
         {
             item.Id = Guid.NewGuid().ToString();
             await _cosmosDbService.AddItemAsync(item);
         }

        // PUT api/values/5
         [HttpPut(“{id}”)]
         public async void Put(string id, [FromBody] Item item)
         {
             await _cosmosDbService.UpdateItemAsync(id, item);
         }

        // DELETE api/values/5
         [HttpDelete(“{id}”)]
         public async void Delete(string id)
         {
             await _cosmosDbService.DeleteItemAsync(id);
         }
     }
}
Step 7: Modify Config class

Configuration file below.

{
   “AllowedHosts”: “*”,
   “CosmosDb”: {
     “Account”: “https://localhost:8081″,
     “Key”: “C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==”,
     “DatabaseName”: “Tasks”,
     “ContainerName”: “Items”
   }
}

Step 8: Run the application

Step 9: Test with Postman

image

GET https://localhost:44364/api/item

POST https://localhost:44364/api/item

{
     “name”: “Item 2”,
     “description”: “Description 2”,
     “isComplete”: true
}

PUT https://localhost:44364/api/item/1

{
     “id”: “1”,
     “name”: “Item (modified)”,
     “description”: “Description (modified)”,
     “isComplete”: true
}

DELETE https://localhost:44364/api/item/566b8151-3fc4-4138-8a2f-22b6939ee60a

Download

You can download the sample here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s