This project is a RESTful API developed using .NET and C# as part of a backend developer assessment. It provides CRUD operations for users and integrates with an external API (PokéAPI) to fetch and display Pokémon data. The API also features JWT authentication for protected routes.
- User management (Create, Read, Update, Delete)
- Integration with PokéAPI to fetch Pokémon data
- JWT-based authentication for secure endpoints
- Layered architecture (API, Application, Domain, Persistence, ExternalServices)
- Entity Framework Core for data persistence
- Swagger UI for API documentation and testing
Before you begin, ensure you have the following installed:
- .NET SDK (NET 6.0)
- Git
- A SQL database: SQLite
- An IDE like Visual Studio or VS Code
-
Clone the repository:
git clone https://github.com/vini-sousa/TestApi.git cd TestApi -
Navigate to the API project folder: The main API project is typically named
Midgar.API. If you are in the root of the cloned repository, you might navigate like this:cd src/Midgar.API
The main configuration files are appsettings.json and appsettings.Development.json located in the Midgar.API project folder.
-
Database Connection String:
- Open
Midgar.API/appsettings.Development.json. - The project is likely configured to use SQLite by default. The connection string might look like:
"ConnectionStrings": { "DefaultConnection": "Data Source=Midgar.db" }
- Open
-
JWT Settings:
- In
Midgar.API/appsettings.Development.json, ensure theJwtSettingsare configured, especially theSecretKey."JwtSettings": { "SecretKey": "YOUR_SECRET_KEY_HERE", "Issuer": "MidgarAPI", "Audience": "MidgarClients" }
- For local development, the existing key is fine. For a real deployment, use a strong, unique key stored securely.
- In
-
Apply Database Migrations:
- Open a terminal or command prompt in the root directory of the solution (where your
Midgar.slnfile is) or in theMidgar.APIproject directory. - Run the following command to create or update the database schema based on the Entity Framework Core migrations:
(Adjust
dotnet ef database update --project Midgar.Persistence --startup-project Midgar.API
--projectand--startup-projectpaths if your folder structure differs significantly or if you run it from a specific subfolder). If you are already in theMidgar.APIfolder, you might only need:dotnet ef database update --project ../Midgar.Persistence
- Open a terminal or command prompt in the root directory of the solution (where your
-
Using the .NET CLI:
- Navigate to the
Midgar.APIproject folder in your terminal. - Run the command:
dotnet watch run
- Navigate to the
-
Using an IDE:
- Open the solution file (
Midgar.sln) in Visual Studio or open the project folder in VS Code. - Run the project (usually by pressing F5 or a "Start Debugging" button) or
- Run the command on terminal:
dotnet watch run
- Open the solution file (
By default, the API will typically be accessible at https://localhost:7167 (HTTPS) and http://localhost:5037 (HTTP). Check your terminal output for the exact URLs.
This project includes unit tests to ensure code quality.
- Using the .NET CLI:
Open a terminal in the root directory of the solution (the
srcfolder).- Run the following command to execute all tests in the solution:
dotnet test - To run tests for a specific test project (e.g.,
Midgar.API.Tests), you can navigate to its folder or specify the project:dotnet test tests/Midgar.API.Tests/Midgar.API.Tests.csproj
- Run the following command to execute all tests in the solution:
Once the API is running, you can access the Swagger UI for interactive documentation and to test the endpoints:
- Navigate to
https://localhost:71670/swagger(or the appropriate HTTPS URL and port from your terminal output).
Some routes are protected and require a JWT token for access.
-
Login to get a token:
- Make a
POSTrequest to the/api/auth/loginendpoint. - Request Body:
{ "username": "testuser", "password": "password123" } - The response will contain a JWT token if the credentials are valid.
- Make a
-
Accessing Protected Routes:
- Copy the token received from the login response.
- For any protected route (e.g.,
GET /api/users/), include the token in theAuthorizationheader of your request:Authorization: Bearer <YOUR_JWT_TOKEN_HERE> - Swagger UI can be configured to send this header after you authorize it using the "Authorize" button.