diff --git a/rentals_service/main.py b/rentals_service/main.py index b17c8c5ae78f46bd2e659e03588eb564bb13f25b..ab91660a4c79e51228a55403925f0a04f085b960 100644 --- a/rentals_service/main.py +++ b/rentals_service/main.py @@ -72,6 +72,7 @@ cursor = db_connection.cursor() # Rental model class Rental(BaseModel): + booking_id: int user_id: int bike_id: int year: int @@ -150,10 +151,10 @@ async def create_rental(rental: Rental): try: cursor.execute(''' INSERT INTO Rentals - (user_id, bike_id, year, hour, season, holiday, workingday, weather, temp, atemp, humidity, windspeed, count) - VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + (booking_id, user_id, bike_id, year, hour, season, holiday, workingday, weather, temp, atemp, humidity, windspeed, count) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ''', ( - rental.user_id, rental.bike_id, rental.year, rental.hour, + rental.booking_id, rental.user_id, rental.bike_id, rental.year, rental.hour, rental.season, rental.holiday, rental.workingday, rental.weather, rental.temp, rental.atemp, rental.humidity, rental.windspeed, rental.count )) @@ -161,7 +162,7 @@ async def create_rental(rental: Rental): # Get the auto-generated id of the new rental rental_id = cursor.lastrowid # Return the created rental with the generated id - rental.id = rental_id + rental.booking_id = rental_id return rental except Exception as e: # Log the exception @@ -175,26 +176,9 @@ async def create_rental(rental: Rental): async def read_rentals(): cursor.execute('SELECT * FROM Rentals') rentals = cursor.fetchall() - rental_objects = [] - # for rental in rentals: - # rental_objects.append(Rental( - # id=rental[0], - # user_id=rental[1], - # bike_id=rental[2], - # year=rental[3], - # hour=rental[4], - # season=rental[5], - # holiday=bool(rental[6]), - # workingday=bool(rental[7]), - # weather=rental[8], - # temp=float(rental[9]), - # atemp=float(rental[10]), - # humidity=float(rental[11]), - # windspeed=float(rental[12]), - # count=int(rental[13]) # Convert to integer - # )) - for rental in rentals: - rental_objects.append(Rental( + rental_objects = [ + Rental( + booking_id=rental[0], user_id=rental[1], bike_id=rental[2], year=rental[3], @@ -208,8 +192,8 @@ async def read_rentals(): humidity=float(rental[11]), windspeed=float(rental[12]), count=rental[13] - )) - + ) for rental in rentals + ] return rental_objects @@ -222,6 +206,7 @@ async def read_rental(rental_id: int): # Convert the tuple to a dictionary rental_dict = { + 'booking_id': rental[0], 'user_id': rental[1], 'bike_id': rental[2], 'year': rental[3], @@ -244,12 +229,12 @@ async def read_rental(rental_id: int): async def update_rental(rental_id: int, rental: Rental): cursor.execute(''' UPDATE Rentals - SET id = ?, user_id = ?, bike_id = ?, year = ?, hour = ?, season = ?, - holiday = ?, workingday = ?, weather = ?, temp = ?, atemp = ?, - humidity = ?, windspeed = ?, count = ? - WHERE rental_id = ? + SET booking_id = %s, user_id = %s, bike_id = %s, year = %s, hour = %s, season = %s, + holiday = %s, workingday = %s, weather = %s, temp = %s, atemp = %s, + humidity = %s, windspeed = %s, count = %s + WHERE rental_id = %s ''', ( - rental.id, rental.user_id, rental.bike_id, rental.year, rental.hour, + rental.booking_id, rental.user_id, rental.bike_id, rental.year, rental.hour, rental.season, rental.holiday, rental.workingday, rental.weather, rental.temp, rental.atemp, rental.humidity, rental.windspeed, rental.count, rental_id diff --git a/rentals_service/testcode.py b/rentals_service/testcode.py deleted file mode 100644 index fd7779717fcd3ddff007459a650bc94337e99d0e..0000000000000000000000000000000000000000 --- a/rentals_service/testcode.py +++ /dev/null @@ -1,210 +0,0 @@ -from fastapi import FastAPI, HTTPException -from pydantic import BaseModel -from fastapi.middleware.cors import CORSMiddleware # type: ignore -from typing import List -import sqlite3 -from pathlib import Path -from typing import Optional -from fastapi import HTTPException # type: ignore -import mysql.connector - -app = FastAPI() - -# Allow requests from all origins -app.add_middleware( - CORSMiddleware, - allow_origins=["*"], - allow_credentials=True, - allow_methods=["GET", "POST", "PUT", "DELETE"], - allow_headers=["*"], -) - -# SQLite connection -parent_directory = Path(__file__).resolve().parent.parent -db_file_path = parent_directory / "my_ride.db" -# print(db_file_path) -conn = sqlite3.connect(db_file_path) - - -cursor = conn.cursor() - -# Create the Rentals table if it doesn't exist -cursor.execute(''' - CREATE TABLE IF NOT EXISTS Rentals ( - rental_id INTEGER PRIMARY KEY, - id INT, - user_id INT, - bike_id INT, - year INT, - hour INT, - season INT, - holiday BOOLEAN, - workingday BOOLEAN, - weather INT, - temp DECIMAL(5, 2), - atemp DECIMAL(5, 2), - humidity DECIMAL(5, 2), - windspeed DECIMAL(5, 2), - count INT, - FOREIGN KEY (user_id) REFERENCES Users(user_id), - FOREIGN KEY (bike_id) REFERENCES Bikes(bike_id) - ) -''') -conn.commit() - - -# Rental model -class Rental(BaseModel): - user_id: Optional[int] = None - bike_id: int - year: int - hour: int - season: int - holiday: bool - workingday: bool - weather: int - temp: float - atemp: float - humidity: float - windspeed: float - count: float - - -class PostRental(BaseModel): - id:Optional[int] = None - user_id: Optional[int] = None - bike_id: int - year: int - hour: int - season: int - holiday: bool - workingday: bool - weather: int - temp: float - atemp: float - humidity: float - windspeed: float - count: float - - -@app.post("/rentals/", response_model=Rental) -async def create_rental(rental: Rental): - try: - cursor.execute(''' - INSERT INTO Rentals - (id,user_id, bike_id, year, hour, season, holiday, workingday, weather, temp, atemp, humidity, windspeed, count) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - ''', ( - rental.id,rental.user_id, rental.bike_id, rental.year, rental.hour, - rental.season, rental.holiday, rental.workingday, rental.weather, - rental.temp, rental.atemp, rental.humidity, rental.windspeed, rental.count - )) - conn.commit() - # Get the auto-generated id of the new rental - rental_id = cursor.lastrowid - # Return the created rental with the generated id - rental.id = rental_id - return rental - except Exception as e: - print(f"Error creating rental: {e}") - raise HTTPException(status_code=422, detail=str(e)) - - - -@app.get("/rentals/", response_model=List[Rental]) -async def read_rentals(): - cursor.execute('SELECT * FROM Rentals') - rentals = cursor.fetchall() - rental_objects = [] - # for rental in rentals: - # rental_objects.append(Rental( - # id=rental[0], - # user_id=rental[1], - # bike_id=rental[2], - # year=rental[3], - # hour=rental[4], - # season=rental[5], - # holiday=bool(rental[6]), - # workingday=bool(rental[7]), - # weather=rental[8], - # temp=float(rental[9]), - # atemp=float(rental[10]), - # humidity=float(rental[11]), - # windspeed=float(rental[12]), - # count=int(rental[13]) # Convert to integer - # )) - for rental in rentals: - rental_objects.append(Rental( - user_id=rental[1], - bike_id=rental[2], - year=rental[3], - hour=rental[4], - season=rental[5], - holiday=bool(rental[6]), - workingday=bool(rental[7]), - weather=rental[8], - temp=float(rental[9]), - atemp=float(rental[10]), - humidity=float(rental[11]), - windspeed=float(rental[12]), - count=rental[13] - )) - - return rental_objects - - -@app.get("/rentals/{rental_id}", response_model=Rental) -async def read_rental(rental_id: int): - cursor.execute('SELECT * FROM Rentals WHERE rental_id = ?', (rental_id,)) - rental_data = cursor.fetchone() - if rental_data is None: - raise HTTPException(status_code=404, detail="Rental not found") - - # Construct a Rental instance from the tuple data - rental = Rental( - id=rental_data[0], # Assuming the rental_id is the first column in your table - user_id=rental_data[1], - bike_id=rental_data[2], - year=rental_data[3], - hour=rental_data[4], - season=rental_data[5], - holiday=bool(rental_data[6]), - workingday=bool(rental_data[7]), - weather=rental_data[8], - temp=float(rental_data[9]), - atemp=float(rental_data[10]), - humidity=float(rental_data[11]), - windspeed=float(rental_data[12]), - count=int(rental_data[13]) # Assuming count should be converted to int - ) - - return rental - - -@app.put("/rentals/{rental_id}", response_model=Rental) -async def update_rental(rental_id: int, rental: Rental): - try: - cursor.execute(''' - UPDATE Rentals - SET user_id = ?, bike_id = ?, year = ?, hour = ?, season = ?, - holiday = ?, workingday = ?, weather = ?, temp = ?, atemp = ?, - humidity = ?, windspeed = ?, count = ? - WHERE rental_id = ? - ''', ( - rental.user_id, rental.bike_id, rental.year, rental.hour, - rental.season, rental.holiday, rental.workingday, rental.weather, - rental.temp, rental.atemp, rental.humidity, rental.windspeed, - rental.count, rental_id - )) - conn.commit() - return rental - except sqlite3.OperationalError as e: - print("SQL error:", e) - raise HTTPException(status_code=400, detail=str(e)) - - -@app.delete("/rentals/{rental_id}") -async def delete_rental(rental_id: int): - cursor.execute('DELETE FROM Rentals WHERE rental_id = ?', (rental_id,)) - conn.commit() - return {"message": "Rental deleted successfully"}