diff --git a/microservices/admins_service/__pycache__/main.cpython-39.pyc b/microservices/admins_service/__pycache__/main.cpython-39.pyc index 1904b053b79bfcbac7d8579d77b369c94bc1cd95..687606db4c306a7c3087b29661218edc17c01d2f 100644 Binary files a/microservices/admins_service/__pycache__/main.cpython-39.pyc and b/microservices/admins_service/__pycache__/main.cpython-39.pyc differ diff --git a/microservices/admins_service/main.py b/microservices/admins_service/main.py index bfa1cb5ee949042b31e02d1c21f4e51f2b64c3fc..df9bf5684278b1d058228ff8ea6889e951da1a3f 100644 --- a/microservices/admins_service/main.py +++ b/microservices/admins_service/main.py @@ -1,12 +1,28 @@ from fastapi import FastAPI, HTTPException from pydantic import BaseModel -from typing import List +from fastapi.middleware.cors import CORSMiddleware import sqlite3 +from pathlib import Path +from typing import Optional, Tuple, List 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 # SQLite connection -conn = sqlite3.connect('bikes.db') +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 Admins table if it doesn't exist @@ -23,15 +39,26 @@ conn.commit() # Admin model +# class Admin(BaseModel): +# username: str +# password: str +# email: str +# phone_number: str + class Admin(BaseModel): username: str - password: str + password: Optional[str] + email: str + phone_number: str + +class AdminResponse(BaseModel): + username: str email: str phone_number: str # Routes -@app.post("/admins/", response_model=Admin) +@app.post("/admins/", response_model=AdminResponse) async def create_admin(admin: Admin): cursor.execute(''' INSERT INTO Admins @@ -41,21 +68,19 @@ async def create_admin(admin: Admin): admin.username, admin.password, admin.email, admin.phone_number )) conn.commit() - return admin - + return AdminResponse( + username=admin.username, + email=admin.email, + phone_number=admin.phone_number + ) -# @app.get("/admins/", response_model=List[Admin]) -# async def read_admins(): -# cursor.execute('SELECT * FROM Admins') -# admins = cursor.fetchall() -# return admins -@app.get("/admins/", response_model=List[Admin]) +@app.get("/admins/", response_model=List[AdminResponse]) async def read_admins(): cursor.execute('SELECT username, email, phone_number FROM Admins') admins_data: List[Tuple[str, str, str]] = cursor.fetchall() admins = [ - Admin( + AdminResponse( username=admin[0], email=admin[1], phone_number=admin[2] @@ -63,7 +88,6 @@ async def read_admins(): ] return admins - @app.get("/admins/{admin_id}", response_model=Admin) async def read_admin(admin_id: int): cursor.execute('SELECT * FROM Admins WHERE admin_id = ?', (admin_id,)) diff --git a/microservices/bikes_service/main.py b/microservices/bikes_service/main.py index 22834e6cffc9dce74ca1467e64e5212d2d187831..e2ef2a88a49857891c72697da47a73712bc92f59 100644 --- a/microservices/bikes_service/main.py +++ b/microservices/bikes_service/main.py @@ -1,12 +1,33 @@ from fastapi import FastAPI, HTTPException from pydantic import BaseModel +from fastapi.middleware.cors import CORSMiddleware from typing import List import sqlite3 +from pathlib import Path +from typing import Optional 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 -conn = sqlite3.connect('bikes.db') +# conn = sqlite3.connect('bikes.db') +# Get the path to the parent directory of your FastAPI application +parent_directory = Path(__file__).resolve().parent.parent +# print(parent_directory) + +# Specify the path to the SQLite database file in the data directory +db_file_path = parent_directory / "my_ride.db" +# print(db_file_path) +conn = sqlite3.connect(db_file_path) + cursor = conn.cursor() # Create the Bikes table if it doesn't exist @@ -28,14 +49,12 @@ conn.commit() # Bike model class Bike(BaseModel): model: str - status: str + status: Optional[str] location: str condition: str price_per_hour: float last_maintenance_date: str maintenance_history: str - - # Routes @app.post("/bikes/", response_model=Bike) @@ -52,6 +71,7 @@ async def create_bike(bike: Bike): return bike + @app.get("/bikes/", response_model=List[Bike]) async def read_bikes(): cursor.execute('SELECT * FROM Bikes') @@ -59,15 +79,17 @@ async def read_bikes(): bike_objects = [] for bike in bikes: bike_obj = Bike( - brand=bike[0], model=bike[1], - year=bike[2], - price=bike[3] + status=bike[2], + location=bike[3], + condition=bike[4], + price_per_hour=bike[5], + last_maintenance_date=bike[6], + maintenance_history=bike[7] ) 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,)) diff --git a/microservices/my_ride.db b/microservices/my_ride.db new file mode 100644 index 0000000000000000000000000000000000000000..bb4f85488514110d898318b5d2972870eb635a51 Binary files /dev/null and b/microservices/my_ride.db differ diff --git a/microservices/rentals_service/main.py b/microservices/rentals_service/main.py index e204743b5307a65118e75841c0ea6eb4fd5d948f..ebeb17d5b44c578c0d1405641f339ab727e295ff 100644 --- a/microservices/rentals_service/main.py +++ b/microservices/rentals_service/main.py @@ -1,12 +1,30 @@ from fastapi import FastAPI, HTTPException from pydantic import BaseModel +from fastapi.middleware.cors import CORSMiddleware from typing import List import sqlite3 +from pathlib import Path +from typing import Optional +from fastapi import HTTPException 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 -conn = sqlite3.connect('rentals.db') +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 @@ -36,7 +54,6 @@ conn.commit() # Rental model class Rental(BaseModel): - id: int user_id: int bike_id: int year: int @@ -52,20 +69,88 @@ class Rental(BaseModel): count: int -# Routes +# # Routes +# @app.post("/rentals/", response_model=Rental) +# async def create_rental(rental: Rental): +# 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() +# return rental + + +# @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() +# return rental +# except Exception as e: +# # Log the exception +# print(f"Error creating rental: {e}") +# # Raise an HTTPException with status code 422 and error message +# raise HTTPException(status_code=422, detail=str(e)) + + +# @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() +# return rental +# except Exception as e: +# # Log the exception +# print(f"Error creating rental: {e}") +# # Raise an HTTPException with status code 422 and error message +# raise HTTPException(status_code=422, detail=str(e)) + + @app.post("/rentals/", response_model=Rental) async def create_rental(rental: Rental): - 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() - return rental + try: + cursor.execute(''' + INSERT INTO Rentals + (user_id, bike_id, year, hour, season, holiday, workingday, weather, temp, atemp, humidity, windspeed, count) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ''', ( + 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: + # Log the exception + print(f"Error creating rental: {e}") + # Raise an HTTPException with status code 422 and error message + raise HTTPException(status_code=422, detail=str(e)) + @app.get("/rentals/", response_model=List[Rental]) @@ -73,9 +158,25 @@ 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( - id=rental[0], user_id=rental[1], bike_id=rental[2], year=rental[3], diff --git a/microservices/reviews_service/main.py b/microservices/reviews_service/main.py index fef99cc98a5f5f9f828e61d5db897c1501def2f9..8a5d39ec6247a137c6b920e7380d8ae5a75d9d41 100644 --- a/microservices/reviews_service/main.py +++ b/microservices/reviews_service/main.py @@ -1,12 +1,28 @@ from fastapi import FastAPI, HTTPException from pydantic import BaseModel +from fastapi.middleware.cors import CORSMiddleware from typing import List import sqlite3 +from pathlib import Path +from typing import Optional 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 -conn = sqlite3.connect('review.db') +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 Reviews table if it doesn't exist diff --git a/microservices/users_service/main.py b/microservices/users_service/main.py index 4634780e76f80e9aea44759df1a0004ef5572bc7..4ab60bea97d77134b61a3c5d90a5eeeafd9158ea 100644 --- a/microservices/users_service/main.py +++ b/microservices/users_service/main.py @@ -3,6 +3,7 @@ from pydantic import BaseModel from fastapi.middleware.cors import CORSMiddleware from typing import List import sqlite3 +from pathlib import Path from typing import Optional app = FastAPI() @@ -17,7 +18,11 @@ app.add_middleware( ) # SQLite connection -conn = sqlite3.connect('bikes.db') +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 Users table if it doesn't exist @@ -38,7 +43,7 @@ conn.commit() class User(BaseModel): username: str - password: str + password: str email: str phone_number: str credit_card_info: Optional[str] = None