Update generated backend for blog_app
This commit is contained in:
parent
5fd9d11ddf
commit
46e7d863f2
8
app/api/core/dependencies/dependencies.py
Normal file
8
app/api/core/dependencies/dependencies.py
Normal file
@ -0,0 +1,8 @@
|
||||
from app.api.db.database import SessionLocal
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
16
app/api/core/middleware/activity_tracker.py
Normal file
16
app/api/core/middleware/activity_tracker.py
Normal file
@ -0,0 +1,16 @@
|
||||
from typing import Callable, Awaitable
|
||||
from loguru import logger
|
||||
from time import time
|
||||
|
||||
class ActivityTrackerMiddleware:
|
||||
async def __call__(self, request: Request, call_next: Callable[[Request], Awaitable[Response]]) -> Response:
|
||||
start_time = time()
|
||||
request_method = request.method
|
||||
request_url = str(request.url)
|
||||
|
||||
response = await call_next(request)
|
||||
|
||||
process_time = time() - start_time
|
||||
logger.info(f"{request_method} {request_url} - {process_time:.6f} seconds")
|
||||
|
||||
return response
|
11
app/api/db/database.py
Normal file
11
app/api/db/database.py
Normal file
@ -0,0 +1,11 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = "sqlite:///./blog_app.db"
|
||||
|
||||
engine = create_engine(
|
||||
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
13
app/api/v1/models/and.py
Normal file
13
app/api/v1/models/and.py
Normal file
@ -0,0 +1,13 @@
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.api.db.database import Base
|
||||
|
||||
class And(Base):
|
||||
__tablename__ = "ands"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
title = Column(String)
|
||||
content = Column(String)
|
||||
blog_id = Column(Integer, ForeignKey("blogs.id"))
|
||||
|
||||
blog = relationship("Blog", back_populates="ands")
|
14
app/api/v1/models/comments.py
Normal file
14
app/api/v1/models/comments.py
Normal file
@ -0,0 +1,14 @@
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.api.db.database import Base
|
||||
|
||||
class Comments(Base):
|
||||
__tablename__ = "comments"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
content = Column(String)
|
||||
post_id = Column(Integer, ForeignKey("posts.id"))
|
||||
author_id = Column(Integer, ForeignKey("users.id"))
|
||||
|
||||
post = relationship("Posts", back_populates="comments")
|
||||
author = relationship("Users", back_populates="comments")
|
13
app/api/v1/models/posts.py
Normal file
13
app/api/v1/models/posts.py
Normal file
@ -0,0 +1,13 @@
|
||||
from sqlalchemy import Column, Integer, String, Text, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.api.db.database import Base
|
||||
|
||||
class Posts(Base):
|
||||
__tablename__ = "posts"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
title = Column(String)
|
||||
content = Column(Text)
|
||||
user_id = Column(Integer, ForeignKey("users.id"))
|
||||
|
||||
user = relationship("Users", back_populates="posts")
|
13
app/api/v1/models/user.py
Normal file
13
app/api/v1/models/user.py
Normal file
@ -0,0 +1,13 @@
|
||||
from sqlalchemy import Column, Integer, String
|
||||
from app.api.db.database import Base
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
username = Column(String, unique=True, index=True)
|
||||
email = Column(String, unique=True, index=True)
|
||||
password = Column(String)
|
||||
full_name = Column(String)
|
||||
bio = Column(String)
|
||||
profile_picture = Column(String)
|
@ -0,0 +1,13 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from .users import router as users_router
|
||||
from .auths import router as auths_router
|
||||
from .comments import router as comments_router
|
||||
from .posts import router as posts_router
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
router.include_router(users_router, prefix="/users", tags=["users"])
|
||||
router.include_router(auths_router, prefix="/auths", tags=["auths"])
|
||||
router.include_router(comments_router, prefix="/comments", tags=["comments"])
|
||||
router.include_router(posts_router, prefix="/posts", tags=["posts"])
|
29
app/api/v1/routes/and.py
Normal file
29
app/api/v1/routes/and.py
Normal file
@ -0,0 +1,29 @@
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.v1.models.and import And
|
||||
from app.api.v1.schemas.and import AndCreate, AndResponse
|
||||
from app.api.core.dependencies.dependencies import get_db
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/ands", response_model=List[AndResponse])
|
||||
def read_ands(db: Session = Depends(get_db)):
|
||||
ands = db.query(And).all()
|
||||
return ands
|
||||
|
||||
@router.post("/ands", response_model=AndResponse)
|
||||
def create_and(and_create: AndCreate, db: Session = Depends(get_db)):
|
||||
and_db = And(**and_create.dict())
|
||||
db.add(and_db)
|
||||
db.commit()
|
||||
db.refresh(and_db)
|
||||
return and_db
|
||||
|
||||
@router.get("/ands/{id}", response_model=AndResponse)
|
||||
def read_and(id: int, db: Session = Depends(get_db)):
|
||||
and_db = db.query(And).filter(And.id == id).first()
|
||||
if not and_db:
|
||||
raise HTTPException(status_code=404, detail="And not found")
|
||||
return and_db
|
29
app/api/v1/routes/comments.py
Normal file
29
app/api/v1/routes/comments.py
Normal file
@ -0,0 +1,29 @@
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.v1.models.comments import Comment
|
||||
from app.api.v1.schemas.comments import CommentCreate, CommentResponse
|
||||
from app.api.core.dependencies.dependencies import get_db
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/commentss", response_model=List[CommentResponse])
|
||||
def read_commentss(db: Session = Depends(get_db)):
|
||||
commentss = db.query(Comment).all()
|
||||
return commentss
|
||||
|
||||
@router.post("/commentss", response_model=CommentResponse)
|
||||
def create_comments(comments: CommentCreate, db: Session = Depends(get_db)):
|
||||
db_comments = Comment(**comments.dict())
|
||||
db.add(db_comments)
|
||||
db.commit()
|
||||
db.refresh(db_comments)
|
||||
return db_comments
|
||||
|
||||
@router.get("/commentss/{id}", response_model=CommentResponse)
|
||||
def read_comments(id: int, db: Session = Depends(get_db)):
|
||||
db_comments = db.query(Comment).get(id)
|
||||
if not db_comments:
|
||||
raise HTTPException(status_code=404, detail="Comments not found")
|
||||
return db_comments
|
29
app/api/v1/routes/posts.py
Normal file
29
app/api/v1/routes/posts.py
Normal file
@ -0,0 +1,29 @@
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.v1.models.posts import Posts
|
||||
from app.api.v1.schemas.posts import PostsCreate, PostsOut
|
||||
from app.api.core.dependencies.dependencies import get_db
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/postss", response_model=List[PostsOut])
|
||||
def read_postss(db: Session = Depends(get_db)):
|
||||
postss = db.query(Posts).all()
|
||||
return postss
|
||||
|
||||
@router.post("/postss", response_model=PostsOut)
|
||||
def create_posts(posts: PostsCreate, db: Session = Depends(get_db)):
|
||||
db_posts = Posts(**posts.dict())
|
||||
db.add(db_posts)
|
||||
db.commit()
|
||||
db.refresh(db_posts)
|
||||
return db_posts
|
||||
|
||||
@router.get("/postss/{id}", response_model=PostsOut)
|
||||
def read_posts(id: int, db: Session = Depends(get_db)):
|
||||
db_posts = db.query(Posts).get(id)
|
||||
if not db_posts:
|
||||
raise HTTPException(status_code=404, detail="Posts not found")
|
||||
return db_posts
|
29
app/api/v1/routes/user.py
Normal file
29
app/api/v1/routes/user.py
Normal file
@ -0,0 +1,29 @@
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.core.dependencies import get_db
|
||||
from app.api.v1.models.user import User
|
||||
from app.api.v1.schemas.user import UserCreate, UserResponse
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/users", response_model=List[UserResponse])
|
||||
def read_users(db: Session = Depends(get_db)):
|
||||
users = db.query(User).all()
|
||||
return users
|
||||
|
||||
@router.post("/users", response_model=UserResponse)
|
||||
def create_user(user: UserCreate, db: Session = Depends(get_db)):
|
||||
db_user = User(**user.dict())
|
||||
db.add(db_user)
|
||||
db.commit()
|
||||
db.refresh(db_user)
|
||||
return db_user
|
||||
|
||||
@router.get("/users/{user_id}", response_model=UserResponse)
|
||||
def read_user(user_id: int, db: Session = Depends(get_db)):
|
||||
db_user = db.query(User).filter(User.id == user_id).first()
|
||||
if not db_user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
return db_user
|
13
app/api/v1/schemas/and.py
Normal file
13
app/api/v1/schemas/and.py
Normal file
@ -0,0 +1,13 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class AndBase(BaseModel):
|
||||
pass
|
||||
|
||||
class AndCreate(AndBase):
|
||||
pass
|
||||
|
||||
class And(AndBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
17
app/api/v1/schemas/comments.py
Normal file
17
app/api/v1/schemas/comments.py
Normal file
@ -0,0 +1,17 @@
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
class CommentsBase(BaseModel):
|
||||
comment_text: str
|
||||
post_id: int
|
||||
|
||||
class CommentsCreate(CommentsBase):
|
||||
pass
|
||||
|
||||
class Comments(CommentsBase):
|
||||
id: int
|
||||
created_at: Optional[str] = None
|
||||
updated_at: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
17
app/api/v1/schemas/posts.py
Normal file
17
app/api/v1/schemas/posts.py
Normal file
@ -0,0 +1,17 @@
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
class PostsBase(BaseModel):
|
||||
title: str
|
||||
content: str
|
||||
|
||||
class PostsCreate(PostsBase):
|
||||
pass
|
||||
|
||||
class Posts(PostsBase):
|
||||
id: int
|
||||
is_published: bool
|
||||
rating: Optional[int] = None
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
21
app/api/v1/schemas/user.py
Normal file
21
app/api/v1/schemas/user.py
Normal file
@ -0,0 +1,21 @@
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, EmailStr
|
||||
|
||||
class UserBase(BaseModel):
|
||||
email: Optional[EmailStr] = None
|
||||
is_active: Optional[bool] = True
|
||||
is_superuser: bool = False
|
||||
full_name: Optional[str] = None
|
||||
|
||||
class UserCreate(UserBase):
|
||||
email: EmailStr
|
||||
password: str
|
||||
|
||||
class User(UserBase):
|
||||
id: int
|
||||
is_active: bool
|
||||
is_superuser: bool
|
||||
full_name: str
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
12
main.py
12
main.py
@ -1,7 +1,11 @@
|
||||
from fastapi import FastAPI
|
||||
from app.api.db.database import Base, engine
|
||||
from app.api.v1.routes import router as v1_router
|
||||
from app.api.core.middleware.activity_tracker import ActivityTrackerMiddleware
|
||||
|
||||
app = FastAPI(title="Generated Backend")
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"message": "Welcome to the generated backend"}
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
app.include_router(v1_router, prefix="/v1")
|
||||
app.add_middleware(ActivityTrackerMiddleware)
|
@ -1,4 +1 @@
|
||||
fastapi
|
||||
uvicorn
|
||||
sqlalchemy
|
||||
pydantic
|
||||
# No valid code generated
|
||||
|
Loading…
x
Reference in New Issue
Block a user