This project demonstrates a comprehensive API for interacting with Azure Cosmos DB using the official Azure Cosmos DB SDK. The solution is designed to be modular, testable, and professional—mirroring the structure of our Blob Storage project.
The API provides endpoints for common operations, including:
- Creating, reading, updating, and deleting items.
- Querying items using SQL queries.
The implementation leverages:
- Dependency Injection for loose coupling.
- Interface-based design to facilitate unit testing.
- Standardized service responses for consistency.
- Create Item: Add a new item to Cosmos DB.
- Read Item: Retrieve an item by its id and partition key.
- Update Item: Update an existing item.
- Delete Item: Remove an item from the database.
- Query Items: Execute SQL queries to retrieve items.
-
An active Azure Cosmos DB Account
-
Cosmos DB settings (ConnectionString, DatabaseName, ContainerName) configured in
appsettings.json:{ "CosmosDb": { "ConnectionString": "YOUR_COSMOS_DB_CONNECTION_STRING", "DatabaseName": "YourDatabaseName", "ContainerName": "YourContainerName" } }
-
Clone the repository:
git clone https://github.com/yourusername/azure-cosmos-db-api.git cd azure-cosmos-db-api -
Restore dependencies and build the project:
dotnet restore dotnet build
-
Run the application:
dotnet run
-
Access the API:
The API will be available at https://localhost:5001 (or another configured port). Swagger UI is available at /swagger for interactive documentation and testing.
- Endpoint: POST /api/cosmos
- Description: Creates a new item in Cosmos DB.
- Request: JSON payload representing the item.
- Response: A JSON object containing the created item.
- Endpoint: GET /api/cosmos/{id}?partitionKey=yourPartitionKey
- Description: Retrieves an item by its id and partition key.
- Response: A JSON object representing the item.
- Endpoint: PUT /api/cosmos/{id}?partitionKey=yourPartitionKey
- Description: Updates an existing item.
- Request: JSON payload with the updated item data.
- Response: A JSON object containing the updated item.
- Endpoint: DELETE /api/cosmos/{id}?partitionKey=yourPartitionKey
- Description: Deletes the specified item.
- Response: A JSON object indicating the deletion status.
- Endpoint: GET /api/cosmos/query?query=SELECT * FROM c
- Description: Executes a SQL query against the Cosmos DB container.
- Response: A JSON array of items matching the query.
The CosmosAdminController provides endpoints to manage Cosmos DB resources:
-
Create Container
- Endpoint:
POST /api/cosmosadmin/container?containerName={name}&partitionKey={key} - Description: Creates a new container with the specified name and partition key.
- Response: Returns the created container object.
- Endpoint:
-
Delete Container
- Endpoint:
DELETE /api/cosmosadmin/container?containerName={name} - Description: Deletes the container with the specified name.
- Response: Returns a status message.
- Endpoint:
-
Create Database
- Endpoint:
POST /api/cosmosadmin/database?databaseName={name} - Description: Creates a new database with the specified name.
- Response: Returns the created database object.
- Endpoint:
-
Delete Database
- Endpoint:
DELETE /api/cosmosadmin/database?databaseName={name} - Description: Deletes the database with the specified name.
- Response: Returns a status message.
- Endpoint:
The interface-based design (using ICosmosService) allows you to easily mock dependencies and write unit tests for your controllers.
You can also set up integration tests to verify the end-to-end behavior of the API with a real or emulator Cosmos DB instance.
Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or feature suggestions.
This project is licensed under the MIT License.