diff --git a/index.py b/index.py index 0b87b12..1a8c0d6 100644 --- a/index.py +++ b/index.py @@ -16,15 +16,20 @@ class User(SQLModel, table=True): secret_name: str age: Optional[str] = None + engine = create_engine("sqlite:///database.db") SQLModel.metadata.create_all(engine) # Create an instance of the API class app = FastAPI() + +# Instance of the class User +my_user = User(id=9,name="Juliet",secret_name="wenoteak",age=22) + @app.get("/") async def root(): # TODO include the user's name if they are logged in - return {"message": "Hello {}".format("World")} + return {"message": "Hello {name}".format(name=my_user.name)} @app.post("/user") @@ -33,15 +38,21 @@ async def create_new_user(*, user: User): session.add(user) session.commit() # TODO return the User ID - return {"message": "User created"} + return my_user.id + + # return {"message": "User created"} @app.get("/user/{id}") async def get_user(id: int): with Session(engine) as session: # TODO return the user based on the ID (and an error if not) + # I will first create an if statement that check if the id of the + # user is equal to the id we have else return an error if it doesnt exist statement = select(User).where(User.name == id) - user = session.exec(statement).first() - return {"user": user} + if statement: + return {"user": User.name} + else: + return "User not found" @app.get("/api/webhooks") async def handle_hook(*, event: Any):