From 9b03a799638a39c96fef31f4bb0a560baf74025b Mon Sep 17 00:00:00 2001 From: Navin Chandra <nc01009@surrey.ac.uk> Date: Wed, 1 May 2024 18:30:01 +0530 Subject: [PATCH] get bike details based on id --- microservices/bikes_service/main.py | 30 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/microservices/bikes_service/main.py b/microservices/bikes_service/main.py index f3397ec..4fd56d7 100644 --- a/microservices/bikes_service/main.py +++ b/microservices/bikes_service/main.py @@ -30,21 +30,6 @@ conn = sqlite3.connect(db_file_path) cursor = conn.cursor() -# Create the Bikes table if it doesn't exist -cursor.execute(''' - CREATE TABLE IF NOT EXISTS Bikes ( - bike_id INTEGER PRIMARY KEY, - model TEXT, - status TEXT, - location TEXT, - condition TEXT, - price_per_hour DECIMAL(10, 2), - last_maintenance_date DATE, - maintenance_history TEXT - ) -''') -conn.commit() - # Bike model class Bike(BaseModel): @@ -103,13 +88,26 @@ async def read_bikes(): bike_objects.append(bike_obj) return bike_objects + @app.get("/bikes/{bike_id}", response_model=Bike) async def read_bike(bike_id: int): cursor.execute('SELECT * FROM Bikes WHERE bike_id = ?', (bike_id,)) bike = cursor.fetchone() if bike is None: raise HTTPException(status_code=404, detail="Bike not found") - return bike + + # Create a Bike object from the fetched data + bike_obj = Bike( + model=bike[1], + status=bike[2], + location=bike[3], + condition=bike[4], + price_per_hour=bike[5], + last_maintenance_date=bike[6], + maintenance_history=bike[7] + ) + + return bike_obj @app.put("/bikes/{bike_id}", response_model=Bike) -- GitLab