|
| 1 | +# Import necessary modules and classes |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from fastapi import FastAPI |
| 5 | +from models.posts import Post, post_db # Import the Post model and post_db database instance |
| 6 | +from models.users import User, user_db # Import the User model and user_db database instance |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | +from fastapi_sqlalchemy import ( |
| 10 | + DBSessionMiddleware, # Import the DBSessionMiddleware for database sessions |
| 11 | +) |
| 12 | + |
| 13 | +# Create a FastAPI application instance |
| 14 | +app = FastAPI() |
| 15 | + |
| 16 | +# Add the DBSessionMiddleware as a middleware to the FastAPI app, connecting it to the specified databases |
| 17 | +app.add_middleware(DBSessionMiddleware, db=[post_db, user_db]) |
| 18 | + |
| 19 | + |
| 20 | +# Define an endpoint for retrieving all users |
| 21 | +@app.get("/users") |
| 22 | +def get_users(): |
| 23 | + """ |
| 24 | + Endpoint to retrieve a list of all users. |
| 25 | + Returns: |
| 26 | + List[User]: A list of User objects representing all users in the database. |
| 27 | + """ |
| 28 | + return User.get_all() |
| 29 | + |
| 30 | + |
| 31 | +# Define a Pydantic model for creating a new user |
| 32 | +class UserCreate(BaseModel): |
| 33 | + name: str |
| 34 | + email: str |
| 35 | + |
| 36 | + |
| 37 | +# Define an endpoint for adding a new user |
| 38 | +@app.post("/add_user") |
| 39 | +def add_user(user_data: UserCreate): |
| 40 | + """ |
| 41 | + Endpoint to add a new user to the database. |
| 42 | + Args: |
| 43 | + user_data (UserCreate): User data provided in the request body. |
| 44 | + Returns: |
| 45 | + dict: A message indicating the success of the operation. |
| 46 | + """ |
| 47 | + user = User(**user_data.dict()) |
| 48 | + user.save() |
| 49 | + return {"message": "User created successfully"} |
| 50 | + |
| 51 | + |
| 52 | +# Define a Pydantic model for updating user information |
| 53 | +class UserUpdate(UserCreate): |
| 54 | + id: int |
| 55 | + name: Optional[str] |
| 56 | + email: Optional[str] |
| 57 | + |
| 58 | + |
| 59 | +# Define an endpoint for updating user information |
| 60 | +@app.post("/update_user") |
| 61 | +def update_user(user_data: UserUpdate): |
| 62 | + """ |
| 63 | + Endpoint to update user information in the database. |
| 64 | + Args: |
| 65 | + user_data (UserUpdate): User data provided in the request body. |
| 66 | + Returns: |
| 67 | + dict: A message indicating the success of the operation. |
| 68 | + """ |
| 69 | + user = User.get(id=user_data.id) |
| 70 | + user.update(**user_data.dict()) |
| 71 | + user.save() |
| 72 | + return {"message": "User updated successfully"} |
| 73 | + |
| 74 | + |
| 75 | +# Define a Pydantic model for retrieving posts by user ID |
| 76 | +class UserPosts(BaseModel): |
| 77 | + user_id: int |
| 78 | + |
| 79 | + |
| 80 | +# Define an endpoint for retrieving posts by user ID |
| 81 | +@app.get("/posts") |
| 82 | +def get_posts(user: UserPosts): |
| 83 | + """ |
| 84 | + Endpoint to retrieve posts by a specific user ID. |
| 85 | + Args: |
| 86 | + user (UserPosts): User ID provided in the query parameters. |
| 87 | + Returns: |
| 88 | + List[Post]: A list of Post objects belonging to the specified user. |
| 89 | + """ |
| 90 | + posts = Post.get_all(user_id=user.user_id) |
| 91 | + return posts |
| 92 | + |
| 93 | + |
| 94 | +# Define a Pydantic model for creating a new post |
| 95 | +class PostCreate(UserPosts): |
| 96 | + title: str |
| 97 | + content: str |
| 98 | + |
| 99 | + |
| 100 | +# Define an endpoint for adding a new post |
| 101 | +@app.post("/add_post") |
| 102 | +def add_post(post_data: PostCreate): |
| 103 | + """ |
| 104 | + Endpoint to add a new post to the database. |
| 105 | + Args: |
| 106 | + post_data (PostCreate): Post data provided in the request body. |
| 107 | + Returns: |
| 108 | + dict: A message indicating the success of the operation. |
| 109 | + """ |
| 110 | + post = Post(**post_data.dict()) |
| 111 | + post.save() |
| 112 | + return {"message": "Post created successfully"} |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + import uvicorn |
| 117 | + |
| 118 | + uvicorn.run(app, host="127.0.0.1", port=8000) |
0 commit comments