Hello. Everyone!.
I hope you are doing well.
Need the CRUD api in asp net core 7.0 with Cosmosdb.
If someeone can help me, I appreciate that.
Hello. Everyone!.
I hope you are doing well.
Need the CRUD api in asp net core 7.0 with Cosmosdb.
If someeone can help me, I appreciate that.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted May 28, 2023, 2:47 PM EDT
https://dev.to/francescoxx/c-c-sharp-crud-rest-api-using-net-7-aspnet-entity-framework-postgres-docker-and-docker-compose-493a
https://www.learmoreseekmore.com/2022/11/dotnet7-api-crud-efcore.html?m=1
https://jayanttripathy.com/building-crud-rest-apis-in-asp-net-core-7-0-with-ef/
https://jasonwatmore.com/net-7-dapper-ms-sql-server-crud-api-tutorial-in-aspnet-core
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 28, 2023, 7:41 AM EDT
First, make sure you have the required NuGet packages installed: `Microsoft.Azure.Cosmos`, `Microsoft.Azure.Cosmos.Fluent`, and `Microsoft.Extensions.Configuration.AzureKeyVault`.
1. Create a `Product` class to represent your data model:
public class Product { public string Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } }2. Create an interface to define the operations for working with the Cosmos DB:
public interface IProductRepository { Task> GetAllProductsAsync(); Task GetProductByIdAsync(string id); Task AddProductAsync(Product product); Task UpdateProductAsync(string id, Product product); Task DeleteProductAsync(string id); }3. Implement the `IProductRepository` interface using the Cosmos DB SDK:
using Microsoft.Azure.Cosmos; public class CosmosDbProductRepository : IProductRepository { private readonly Container _container; public CosmosDbProductRepository(CosmosClient cosmosClient, string databaseName, string containerName) { _container = cosmosClient.GetContainer(databaseName, containerName); } public async Task> GetAllProductsAsync() { var query = _container.GetItemQueryIterator(); var products = new List(); while (query.HasMoreResults) { var response = await query.ReadNextAsync(); products.AddRange(response.Resource); } return products; } public async Task GetProductByIdAsync(string id) { try { ItemResponse response = await _container.ReadItemAsync(id, new PartitionKey(id)); return response.Resource; } catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { return null; } } public async Task AddProductAsync(Product product) { ItemResponse response = await _container.CreateItemAsync(product, new PartitionKey(product.Id)); return response.Resource; } public async Task UpdateProductAsync(string id, Product product) { ItemResponse response = await _container.UpsertItemAsync(product, new PartitionKey(id)); return response.Resource; } public async Task DeleteProductAsync(string id) { try { await _container.DeleteItemAsync(id, new PartitionKey(id)); return true; } catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { return false; } } }4. Create a controller to handle the API endpoints:
[ApiController] [Route("api/products")] public class ProductController : ControllerBase { private readonly IProductRepository _productRepository; public ProductController(IProductRepository productRepository) { _productRepository = productRepository; } [HttpGet] public async Task>> GetAllProducts() { var products = await _productRepository.GetAllProductsAsync(); return Ok(products); } [HttpGet("{id}")] public async Task> GetProductById(string id) { var product = await _productRepository.GetProductByIdAsync(id); if (product == null) return NotFound(); return Ok(product); } [HttpPost] public async Task> CreateProduct(Product product) { var createdProduct = await _productRepository.AddProductAsync(product); return CreatedAtAction(nameof(GetProductById), new { id = createdProduct.Id }, createdProduct); } [HttpPut("{id}")] public async Task UpdateProduct(string id, Product product) { var updatedProduct = await _productRepository.UpdateProductAsync(id, product); if (updatedProduct == null) return NotFound(); return NoContent(); } [HttpDelete("{id}")] public async Task DeleteProduct(string id) { var result = await _productRepository.DeleteProductAsync(id); if (!result) return NotFound(); return NoContent(); } }Note: You will need to configure the Cosmos DB connection and set up dependency injection for the `CosmosClient` and `IProductRepository` in the ASP.NET Core Startup class.
That's it! You now have a basic CRUD API using Cosmos DB in ASP.NET Core 7.0. You can test the API endpoints using tools like Postman or any other HTTP client.