This project is a Car Rental System API built using .NET 8.0, Entity Framework Core, and SQL Server for database management. The API handles car rentals, calculates prices, tracks customer loyalty points, and manages car availability.
- 📅 Rent one or several cars and calculate the rental price.
- 🔄 Return a car and calculate surcharges (if applicable).
- ⚙️ Update car availability after a rental is created.
- ⭐ Track and update customer loyalty points based on the rented car type.
Ensure the following are installed on your machine:
- .NET 8 SDK: Download here
- SQL Server: Download here (or use SQL Server Express)
- Entity Framework Core CLI: Install using the command:
dotnet tool install --global dotnet-ef
🔧 Step 1: Clone the Project 1.Open your terminal or command prompt.
2.Run the following command to clone the repository:
https://github.com/EberthCastro/CarReSys.git
3.Navigate into the project directory:
```bash
cd CarRentalSystem
📦 Step 2: Install Dependencies
Once inside the project directory, restore the necessary NuGet packages by running:
```bash
dotnet restore
This command will install all the required dependencies such as:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
🗄 Step 3: Configure the Database Connection
1.Open the project in your preferred editor (Visual Studio, VS Code, etc.).
2.Navigate to the appsettings.json file.
3.Update the DefaultConnection string to point to your SQL Server instance. Replace YourServerName with your SQL Server name and YourDatabaseName with your desired database name.
```bash
{
"ConnectionStrings": {
"DefaultConnection": "Server=YourServerName;Database=YourDatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
🏗 Step 4: Migrate the Database
1.Run the following command to create the database schema:
```bash
dotnet ef migrations add InitialCreate
dotnet ef database update
This will create the necessary tables in your database ( Cars and Customers).
## 🧪 Step 5: Test the API Using Swagger
### 1. **Rent a Car**
- **Endpoint**: `POST /api/rentals`
- **Description**: Rent a car by providing the `CarId`, `CustomerId`, and rental details.
#### Steps:
1. Find the **POST /api/rentals** endpoint in Swagger.
2. Click on **Try it out**.
3. Fill in the example request body:
```json
{
"CarId": 1,
"CustomerId": 1,
"RentalDate": "2024-09-30T00:00:00",
"ReturnDate": "2024-10-07T00:00:00",
"CarType": "Small"
}
4. Calculate Total Price and Update Loyalty Points
Endpoint: POST /api/rentals/calculateTotalPrice/{rentalId}/{extraDays}
Description: Calculate the total price of a rental, including any extra days, and update loyalty points for the customer.
Steps:
Find the POST /api/rentals/calculateTotalPrice/{rentalId}/{extraDays} endpoint in Swagger.
Click Try it out.
Enter the following parameters:
rentalId: The ID of the rental (e.g., 1).
extraDays: The number of extra days (e.g., 2).
Click Execute.
Swagger will show the updated rental with the calculated total price and loyalty points.
5. Get All Cars
Endpoint: GET /api/cars
Description: Retrieve a list of all available cars.
Steps:
Find the GET /api/cars endpoint in Swagger.
Click Try it out.
Click Execute.
The response will show a list of all cars, including details like Brand, Model, Type, PricePerDay, and IsAvailable.
6. Update Car Availability
Endpoint: PUT /api/cars/{id}
Description: Update a car's availability (set IsAvailable to true or false).
Steps:
Find the PUT /api/cars/{id} endpoint in Swagger.
Click Try it out.
Enter the id of the car you want to update.
Modify the request body, for example:
json
Copiar código
{
"Brand": "Toyota",
"Model": "Corolla",
"Type": "Small",
"PricePerDay": "50",
"IsAvailable": false
}
Click Execute.
Swagger will show the updated car details.
👏 Conclusion
You've now set up the Car Rental System API on your local machine. From here, you can extend the functionality or integrate it with a front-end for a complete car rental management system. 🚀