Update generated backend for blog_app with entities: posts, comments, tags, user
This commit is contained in:
parent
aae611c508
commit
c61abb1d18
9
app/api/core/dependencies/dependencies.py
Normal file
9
app/api/core/dependencies/dependencies.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.api.db.database import SessionLocal
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
13
app/api/core/middleware/activity_tracker.py
Normal file
13
app/api/core/middleware/activity_tracker.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from time import time
|
||||||
|
from starlette.middleware.base import BaseHTTPMiddleware
|
||||||
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import Response
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
class ActivityTrackerMiddleware(BaseHTTPMiddleware):
|
||||||
|
async def dispatch(self, request: Request, call_next):
|
||||||
|
start_time = time()
|
||||||
|
response = await call_next(request)
|
||||||
|
process_time = time() - start_time
|
||||||
|
logger.info(f"Request processed in {process_time:.4f} seconds")
|
||||||
|
return response
|
7
app/api/db/database.py
Normal file
7
app/api/db/database.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
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()
|
21
app/api/v1/models/comments.py
Normal file
21
app/api/v1/models/comments.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# app/api/v1/models/comments.py
|
||||||
|
|
||||||
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||||
|
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)
|
||||||
|
post_id = Column(Integer, ForeignKey('posts.id'))
|
||||||
|
user_id = Column(Integer, ForeignKey('users.id'))
|
||||||
|
content = Column(Text)
|
||||||
|
created_at = Column(String)
|
||||||
|
updated_at = Column(String, nullable=True)
|
||||||
|
|
||||||
|
post = relationship('Post', back_populates='comments')
|
||||||
|
user = relationship('User', back_populates='comments')
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'Comment(id={self.id}, post_id={self.post_id}, user_id={self.user_id}, content="{self.content[:20]}...")'
|
18
app/api/v1/models/posts.py
Normal file
18
app/api/v1/models/posts.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# app/api/v1/models/posts.py
|
||||||
|
|
||||||
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||||
|
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)
|
||||||
|
user_id = Column(Integer, ForeignKey('users.id'))
|
||||||
|
title = Column(String)
|
||||||
|
content = Column(Text)
|
||||||
|
|
||||||
|
user = relationship('User', back_populates='posts')
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'Post(id={self.id}, title="{self.title}", content="{self.content[:20]}...")'
|
17
app/api/v1/models/tags.py
Normal file
17
app/api/v1/models/tags.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# app/api/v1/models/tags.py
|
||||||
|
|
||||||
|
from sqlalchemy import Column, ForeignKey, Integer, String
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from app.api.db.database import Base
|
||||||
|
|
||||||
|
class Tags(Base):
|
||||||
|
__tablename__ = 'tags'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String, nullable=False)
|
||||||
|
description = Column(Text)
|
||||||
|
|
||||||
|
posts = relationship('Post', back_populates='tags', secondary='post_tags')
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'Tag(id={self.id}, name={self.name})'
|
20
app/api/v1/models/user.py
Normal file
20
app/api/v1/models/user.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# app/api/v1/models/user.py
|
||||||
|
|
||||||
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from app.api.db.database import Base
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = 'user'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
username = Column(String, unique=True, nullable=False)
|
||||||
|
email = Column(String, unique=True, nullable=False)
|
||||||
|
password = Column(String, nullable=False)
|
||||||
|
bio = Column(Text, nullable=True)
|
||||||
|
|
||||||
|
# Define relationships here if needed
|
||||||
|
# posts = relationship("Post", back_populates="author")
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"User(id={self.id}, username='{self.username}', email='{self.email}')"
|
@ -0,0 +1,3 @@
|
|||||||
|
from fastapi import APIRouter
|
||||||
|
|
||||||
|
router = APIRouter()
|
9
app/api/v1/routes/comments.py
Normal file
9
app/api/v1/routes/comments.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.api.core.dependencies import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get('/comments/')
|
||||||
|
def read_comments(db: Session = Depends(get_db)):
|
||||||
|
return {'message': 'Read comments'}
|
9
app/api/v1/routes/posts.py
Normal file
9
app/api/v1/routes/posts.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.api.core.dependencies import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get('/posts/')
|
||||||
|
def read_posts(db: Session = Depends(get_db)):
|
||||||
|
return {'message': 'Read posts'}
|
9
app/api/v1/routes/tags.py
Normal file
9
app/api/v1/routes/tags.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.api.core.dependencies import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get('/tags/')
|
||||||
|
def read_tags(db: Session = Depends(get_db)):
|
||||||
|
return {'message': 'Read tags'}
|
9
app/api/v1/routes/user.py
Normal file
9
app/api/v1/routes/user.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.api.core.dependencies import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get('/user/')
|
||||||
|
def read_user(db: Session = Depends(get_db)):
|
||||||
|
return {'message': 'Read user'}
|
13
app/api/v1/schemas/comments.py
Normal file
13
app/api/v1/schemas/comments.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
app/api/v1/schemas/comments.py:
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class CommentsBase(BaseModel):
|
||||||
|
text: str
|
||||||
|
|
||||||
|
class Comments(CommentsBase):
|
||||||
|
id: int
|
||||||
|
post_id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
12
app/api/v1/schemas/posts.py
Normal file
12
app/api/v1/schemas/posts.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class PostsBase(BaseModel):
|
||||||
|
title: str
|
||||||
|
content: str
|
||||||
|
|
||||||
|
class Posts(PostsBase):
|
||||||
|
id: int
|
||||||
|
owner_id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
11
app/api/v1/schemas/tags.py
Normal file
11
app/api/v1/schemas/tags.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class TagsBase(BaseModel):
|
||||||
|
name: str
|
||||||
|
|
||||||
|
class Tags(TagsBase):
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
11
app/api/v1/schemas/user.py
Normal file
11
app/api/v1/schemas/user.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from pydantic import BaseModel, EmailStr
|
||||||
|
|
||||||
|
class UserBase(BaseModel):
|
||||||
|
email: EmailStr
|
||||||
|
username: str
|
||||||
|
|
||||||
|
class User(UserBase):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
13
main.py
13
main.py
@ -1,7 +1,12 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
from app.api.db.database import engine, Base
|
||||||
|
from app.api.v1.routes import router
|
||||||
|
from app.api.core.middleware.activity_tracker import ActivityTrackerMiddleware
|
||||||
|
|
||||||
app = FastAPI(title="Generated Backend")
|
app = FastAPI()
|
||||||
|
app.add_middleware(ActivityTrackerMiddleware)
|
||||||
|
app.include_router(router, prefix='/v1')
|
||||||
|
|
||||||
@app.get("/")
|
@app.on_event("startup")
|
||||||
def read_root():
|
def startup_event():
|
||||||
return {"message": "Welcome to the generated backend"}
|
Base.metadata.create_all(bind=engine)
|
@ -2,3 +2,4 @@ fastapi
|
|||||||
uvicorn
|
uvicorn
|
||||||
sqlalchemy
|
sqlalchemy
|
||||||
pydantic
|
pydantic
|
||||||
|
loguru
|
Loading…
x
Reference in New Issue
Block a user