This is a simple API demo using .NET and MongoDB.
-
MongoDB
Option 1. Install MongoDB from here Option 2. Use Docker.
See the docker-compose.yml sample file in the folder: ./docker/infrastructure/
Just execute docker-compose up -d from that location. -
Node.js (for migrations)
Install Node.js from here (use version 20.x.x or higher)
The migrations are located in the folder: ./mongodb/migrations/
It uses the tool migrate-mongo.
Follow the instructions in the link to install it.
From the referred folder, execute the following command to run the migrations:
migrate-mongo up
This will create the database (product-catalogue) and relevant elements (e.g., views).
The API is a .NET Core 8.0 application.
It uses the appsettings.developement.json file to configure the connection to the MongoDB database.
Open the solution in Visual Studio and run the project ApiDemo.Api.
The API is a simple CRUD for products.
It requires users to be authenticated to access the endpoints (except api/accounts).
Swagger can be used to access the endpoints: https://localhost:7225/swagger/index.html
Use the api/accounts/register endpoint to create/register an account.
Example:
curl -X 'POST' \
'https://localhost:7225/api/accounts/register' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"email": "[email protected]",
"confirmPassword": "Password123!",
"password": "Password123!"
}'
Use the api/accounts/sign-in endpoint to authenticate and obtain the access token.
Example:
curl -X 'POST' \
'https://localhost:7225/api/accounts/sign-in' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"email": "[email protected]",
"password": "Password123!"
}'
Use the access token in the Authorization header to access the other endpoints.
Example:
curl -X 'GET' \
'https://localhost:7225/api/categories' \
-H 'accept: application/json' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQ...'
All test projects are located under the solution folder tests.
It requires MongoDB to be running.
Run the tests using Visual Studio or the command line (dotnet test from the ./src folder).