ASP.NET Rest API C# exam
ASP.NET Rest API
This API provides endpoints to manage geographic data including continents, countries, and rivers. It uses ASP.NET Core with LINQ and Unit of Work patterns, and is backed by a SQL Server database. The project is designed to handle CRUD operations with appropriate validation and error handling.
Source code: here.
Overview
This ASP.NET Rest API allows you to perform the following operations:
- Add: Create new continents, countries, and rivers.
- Manage: Update existing records.
- Delete: Remove records.
- Retrieve: Get information about continents, countries, and rivers.
The API uses Unit of Work and Repository patterns to manage database transactions and ensure data consistency.
Features
- CRUD Operations: Implemented for continents, countries, and rivers.
- Validation: Checks for population limits and existing cities.
- Error Handling: Comprehensive error messages for common issues.
- Unit Testing: Test coverage with xUnit.
API Endpoints
Continent Endpoints
- Create a Continent:
POST /api/continent
- Request Body:
1 2 3
{ "name": "Random Continent" }
- Response: Details of the created continent.
- Get a Continent:
GET /api/continent/{id}
- Response: Details of the continent.
- Delete a Continent:
DELETE /api/continent/{id}
- Response: Status of the deletion.
- Update a Continent:
PUT /api/continent/{id}
- Request Body:
1 2 3
{ "name": "Updated Continent" }
- Response: Details of the updated continent.
Country Endpoints
- Create a Country:
POST /api/continent/{continentID}/Country
- Request Body:
1 2 3 4
{ "name": "Random Country", "population": 350 }
- Response:
1 2 3 4 5 6 7 8 9 10
{ "id": 26, "name": "Random Country", "continent": "http://localhost:1337/api/continent/14", "citiesSum": 0, "population": 350, "surface": 0, "cities": [], "rivers": [] }
- Get a Country:
GET /api/continent/{continentID}/Country/{countryId}
- Response: Details of the country.
- Delete a Country:
DELETE /api/continent/{continentID}/Country/{countryId}
- Response: Status of the deletion.
- Update a Country:
PUT /api/continent/{continentID}/Country/{countryId}
- Request Body:
1 2 3 4
{ "name": "Updated Country", "population": 500 }
- Response: Details of the updated country.
City Endpoints
- Create a City:
POST /api/city
- Request Body:
1 2 3 4
{ "name": "New City", "population": 100000 }
- Response: Details of the created city.
- List All Cities:
GET /api/city
- Response: List of all cities.
- Get a City:
GET /api/city/{id}
- Response: Details of the city.
- Delete a City:
DELETE /api/city/{id}
- Response: Status of the deletion.
- Update a City:
PUT /api/city/{id}
- Request Body:
1 2 3 4
{ "name": "Updated City", "population": 200000 }
- Response: Details of the updated city.
River Endpoints
- Create a River:
POST /api/river
- Request Body:
1 2 3 4
{ "name": "New River", "length": 500 }
- Response: Details of the created river.
- Get a River:
GET /api/river/{id}
- Response: Details of the river.
- Delete a River:
DELETE /api/river/{id}
- Response: Status of the deletion.
- Update a River:
The API provides informative error messages for common issues:
- Population Limit Exceeded: If you try to add a city with a population exceeding the limit.
- City Already Exists: If you attempt to add a city that already exists in the database.
This post is licensed under
CC BY 4.0
by the author.