diff --git a/microservices/admins_service/__pycache__/main.cpython-39.pyc b/microservices/admins_service/__pycache__/main.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1904b053b79bfcbac7d8579d77b369c94bc1cd95 Binary files /dev/null and b/microservices/admins_service/__pycache__/main.cpython-39.pyc differ diff --git a/microservices/admins_service/admin.py b/microservices/admins_service/admin.py deleted file mode 100644 index 6922c762a881a275f44b21875a38f775111cdfa6..0000000000000000000000000000000000000000 --- a/microservices/admins_service/admin.py +++ /dev/null @@ -1,80 +0,0 @@ -from fastapi import FastAPI, HTTPException -from pydantic import BaseModel -from typing import List -import sqlite3 - -app = FastAPI() - -# SQLite connection -conn = sqlite3.connect('bikes.db') -cursor = conn.cursor() - -# Create the Admins table if it doesn't exist -cursor.execute(''' - CREATE TABLE IF NOT EXISTS Admins ( - admin_id INTEGER PRIMARY KEY, - username TEXT, - password TEXT, - email TEXT, - phone_number TEXT - ) -''') -conn.commit() - - -# Admin model -class Admin(BaseModel): - username: str - password: str - email: str - phone_number: str - - -# Routes -@app.post("/admins/", response_model=Admin) -async def create_admin(admin: Admin): - cursor.execute(''' - INSERT INTO Admins - (username, password, email, phone_number) - VALUES (?, ?, ?, ?) - ''', ( - admin.username, admin.password, admin.email, admin.phone_number - )) - conn.commit() - return admin - - -@app.get("/admins/", response_model=List[Admin]) -async def read_admins(): - cursor.execute('SELECT * FROM Admins') - admins = cursor.fetchall() - 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,)) - admin = cursor.fetchone() - if admin is None: - raise HTTPException(status_code=404, detail="Admin not found") - return admin - - -@app.put("/admins/{admin_id}", response_model=Admin) -async def update_admin(admin_id: int, admin: Admin): - cursor.execute(''' - UPDATE Admins - SET username = ?, password = ?, email = ?, phone_number = ? - WHERE admin_id = ? - ''', ( - admin.username, admin.password, admin.email, admin.phone_number, admin_id - )) - conn.commit() - return admin - - -@app.delete("/admins/{admin_id}") -async def delete_admin(admin_id: int): - cursor.execute('DELETE FROM Admins WHERE admin_id = ?', (admin_id,)) - conn.commit() - return {"message": "Admin deleted successfully"} diff --git a/microservices/admins_service/bikes.db b/microservices/admins_service/bikes.db index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..827cf0623a57f5ff3882cd088bf01bf0e21d9050 100644 Binary files a/microservices/admins_service/bikes.db and b/microservices/admins_service/bikes.db differ diff --git a/microservices/admins_service/main.py b/microservices/admins_service/main.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..bfa1cb5ee949042b31e02d1c21f4e51f2b64c3fc 100644 --- a/microservices/admins_service/main.py +++ b/microservices/admins_service/main.py @@ -0,0 +1,93 @@ +from fastapi import FastAPI, HTTPException +from pydantic import BaseModel +from typing import List +import sqlite3 + +app = FastAPI() + +# SQLite connection +conn = sqlite3.connect('bikes.db') +cursor = conn.cursor() + +# Create the Admins table if it doesn't exist +cursor.execute(''' + CREATE TABLE IF NOT EXISTS Admins ( + admin_id INTEGER PRIMARY KEY, + username TEXT, + password TEXT, + email TEXT, + phone_number TEXT + ) +''') +conn.commit() + + +# Admin model +class Admin(BaseModel): + username: str + password: str + email: str + phone_number: str + + +# Routes +@app.post("/admins/", response_model=Admin) +async def create_admin(admin: Admin): + cursor.execute(''' + INSERT INTO Admins + (username, password, email, phone_number) + VALUES (?, ?, ?, ?) + ''', ( + admin.username, admin.password, admin.email, admin.phone_number + )) + conn.commit() + return admin + + +# @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]) +async def read_admins(): + cursor.execute('SELECT username, email, phone_number FROM Admins') + admins_data: List[Tuple[str, str, str]] = cursor.fetchall() + admins = [ + Admin( + username=admin[0], + email=admin[1], + phone_number=admin[2] + ) for admin in admins_data + ] + 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,)) + admin = cursor.fetchone() + if admin is None: + raise HTTPException(status_code=404, detail="Admin not found") + return admin + + +@app.put("/admins/{admin_id}", response_model=Admin) +async def update_admin(admin_id: int, admin: Admin): + cursor.execute(''' + UPDATE Admins + SET username = ?, password = ?, email = ?, phone_number = ? + WHERE admin_id = ? + ''', ( + admin.username, admin.password, admin.email, admin.phone_number, admin_id + )) + conn.commit() + return admin + + +@app.delete("/admins/{admin_id}") +async def delete_admin(admin_id: int): + cursor.execute('DELETE FROM Admins WHERE admin_id = ?', (admin_id,)) + conn.commit() + return {"message": "Admin deleted successfully"} diff --git a/microservices/rentals_service/__pycache__/main.cpython-39.pyc b/microservices/rentals_service/__pycache__/main.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6ea2d7749d2668bedbf38f1657b4b0d5ed013382 Binary files /dev/null and b/microservices/rentals_service/__pycache__/main.cpython-39.pyc differ diff --git a/microservices/rentals_service/main.py b/microservices/rentals_service/main.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e204743b5307a65118e75841c0ea6eb4fd5d948f 100644 --- a/microservices/rentals_service/main.py +++ b/microservices/rentals_service/main.py @@ -0,0 +1,128 @@ +from fastapi import FastAPI, HTTPException +from pydantic import BaseModel +from typing import List +import sqlite3 + +app = FastAPI() + +# SQLite connection +conn = sqlite3.connect('rentals.db') +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): + id: int + user_id: int + bike_id: int + year: int + hour: int + season: int + holiday: bool + workingday: bool + weather: int + temp: float + atemp: float + humidity: float + windspeed: float + count: int + + +# 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.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=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 = cursor.fetchone() + if rental is None: + raise HTTPException(status_code=404, detail="Rental not found") + return rental + + +@app.put("/rentals/{rental_id}", response_model=Rental) +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 = ? + ''', ( + 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 + + +@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"} diff --git a/microservices/rentals_service/rentals.db b/microservices/rentals_service/rentals.db index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..b9e3d839ec0a26d0ceca6587137ffe766d990c68 100644 Binary files a/microservices/rentals_service/rentals.db and b/microservices/rentals_service/rentals.db differ diff --git a/microservices/rentals_service/rentals.py b/microservices/rentals_service/rentals.py deleted file mode 100644 index d183201077a061495db93c2f74dd7ba418f2330a..0000000000000000000000000000000000000000 --- a/microservices/rentals_service/rentals.py +++ /dev/null @@ -1,109 +0,0 @@ -from fastapi import FastAPI, HTTPException -from pydantic import BaseModel -from typing import List -import sqlite3 - -app = FastAPI() - -# SQLite connection -conn = sqlite3.connect('bikes.db') -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): - id: int - user_id: int - bike_id: int - year: int - hour: int - season: int - holiday: bool - workingday: bool - weather: int - temp: float - atemp: float - humidity: float - windspeed: float - count: int - - -# 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.get("/rentals/", response_model=List[Rental]) -async def read_rentals(): - cursor.execute('SELECT * FROM Rentals') - rentals = cursor.fetchall() - return rentals - - -@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 = cursor.fetchone() - if rental is None: - raise HTTPException(status_code=404, detail="Rental not found") - return rental - - -@app.put("/rentals/{rental_id}", response_model=Rental) -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 = ? - ''', ( - 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 - - -@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"}