Car Rental API is an application for managing a car rental service, built using Onion Architecture with ASP.NET Core. The application utilizes Identity, an SQL database, and JSON Web Token (JWT) for authentication.
- Microsoft.AspNetCore.Authentication.JwtBearer
- Microsoft.AspNetCore.Identity.EntityFrameworkCore
- Microsoft.AspNetCore.Identity.UI
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools These packages need to be installed for the entire project.
To perform a database migration, set the startup project to ProjektBackend.Infrastructure in the Package Manager Console and then run the following commands:
add-migration Init
update-databaseMake sure to delete the Migrations folder if it already exists.
{
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=.\\LOCALHOST;Database=Projekt;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=true"
}
}Replace the Server field with the appropriate name for your machine, e.g., localhost.
Contains controllers for handling HTTP requests.
AdminController
- Creates a user with the Administrator role.
AuthorizationController
- Registers new users.
- Logs in users and generates JWT tokens.
CarController
- Retrieves all cars.
- Retrieves car details.
- Creates new cars (Administrator only).
- Edits cars (Administrator only).
- Deletes cars.
CategoryController
- Retrieves all car categories.
- Retrieves category details.
ClientController
- Retrieves all clients (Administrator, User).
- Retrieves client details (Administrator, User).
- Creates new clients (Administrator).
OrderController
- Retrieves all orders (Administrator, User).
- Retrieves order details (Administrator, User).
- Creates new orders (Administrator).
Contains services for business logic.
CarService
- Handles operations related to cars.
CategoryService
- Handles operations related to categories.
ClientService
- Handles operations related to clients.
OrderService
- Handles operations related to orders.
Contains entities and interfaces.
Cars
- Car ID, brand, model, category, related orders.
Categories
- Category ID, category name, related cars.
Clients
- Client ID, first name, last name, email, related orders.
Orders
- Order ID, order date, user ID, client ID, car ID, pickup and return dates.
The entities consist of 4 main entities:
- Categories
- Cars
- Clients
- Orders
And 7 relationships:
- Categories - Cars: One-to-Many
- Cars - Categories: Many-to-One
- Cars - Orders: One-to-Many
- Clients - Orders: One-to-Many
- Orders - Users: Many-to-One
- Orders - Clients: Many-to-One
- Orders - Cars: Many-to-One
Contains repository implementations and database configuration.
CarRepository
- Implements CRUD operations for cars.
CategoryRepository
- Implements CRUD operations for categories.
ClientRepository
- Implements CRUD operations for clients.
OrderRepository
- Implements CRUD operations for orders.
- Configures the database and relationships between entities.
Contains models used in the application.
ApplicationUser
- Extends the
IdentityUserclass with additional fields: FirstName, LastName.
RegisterModel
- Model for user registration.
LoginModel
- Model for user login.
-
Clone the repository:
git clone https://github.com/dstudnicki/CarRentAPI.git
-
Navigate to the project directory:
cd ProjektBackend -
Install dependencies:
dotnet restore
-
Configure the database in the
appsettings.jsonfile. -
Run migrations:
dotnet ef database update
-
Run the application:
dotnet run
- Endpoint:
POST /Authorization/register - Body:
{ "UserName": "string", "Email": "string", "Password": "string", "FirstName": "string", "LastName": "string" }
- Endpoint:
POST /Authorization/login - Body:
{ "Email": "string", "Password": "string" }
- Endpoint:
POST /Car - Header:
Authorization: Bearer {token} - Body:
{ "Brand": "string", "Model": "string", "CategoryId": int }
- Endpoint:
GET /Car - Header:
Authorization: Bearer {token}
To test the API, you can use tools like Postman or Swagger UI, available at /swagger.