Update generated backend for blog_app with entities: posts, comments, tags, user
This commit is contained in:
parent
4ddfd4f91f
commit
b8c75e0593
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 @@
|
||||
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.time()
|
||||
response: Response = await call_next(request)
|
||||
process_time = 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]}...", created_at="{self.created_at}", updated_at="{self.updated_at}")'
|
16
app/api/v1/models/posts.py
Normal file
16
app/api/v1/models/posts.py
Normal file
@ -0,0 +1,16 @@
|
||||
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)
|
||||
title = Column(String)
|
||||
content = Column(Text)
|
||||
user_id = Column(Integer, ForeignKey('users.id'))
|
||||
|
||||
user = relationship('User', back_populates='posts')
|
||||
|
||||
def __repr__(self):
|
||||
return f'Post(id={self.id}, title={self.title}, content={self.content[:20]}...)'
|
19
app/api/v1/models/tags.py
Normal file
19
app/api/v1/models/tags.py
Normal file
@ -0,0 +1,19 @@
|
||||
# app/api/v1/models/tags.py
|
||||
|
||||
from typing import Optional
|
||||
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||
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, nullable=True)
|
||||
post_id = Column(Integer, ForeignKey('posts.id'), nullable=False)
|
||||
|
||||
post = relationship('Post', back_populates='tags')
|
||||
|
||||
def __repr__(self):
|
||||
return f'Tag(id={self.id}, name={self.name}, description={self.description})'
|
18
app/api/v1/models/user.py
Normal file
18
app/api/v1/models/user.py
Normal file
@ -0,0 +1,18 @@
|
||||
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_hash = Column(String, nullable=False)
|
||||
is_active = Column(Integer, default=1)
|
||||
bio = Column(Text, nullable=True)
|
||||
|
||||
posts = relationship("Post", back_populates="author")
|
||||
|
||||
def __repr__(self):
|
||||
return f"User(id={self.id}, username='{self.username}', email='{self.email}')"
|
@ -0,0 +1,2 @@
|
||||
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'}
|
10
app/api/v1/schemas/comments.py
Normal file
10
app/api/v1/schemas/comments.py
Normal file
@ -0,0 +1,10 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class CommentsBase(BaseModel):
|
||||
pass
|
||||
|
||||
class Comments(BaseModel):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
15
app/api/v1/schemas/posts.py
Normal file
15
app/api/v1/schemas/posts.py
Normal file
@ -0,0 +1,15 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
class PostsBase(BaseModel):
|
||||
title: str
|
||||
content: str
|
||||
published: bool = True
|
||||
rating: Optional[int] = None
|
||||
|
||||
class Posts(PostsBase):
|
||||
id: int
|
||||
owner_id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
12
app/api/v1/schemas/tags.py
Normal file
12
app/api/v1/schemas/tags.py
Normal file
@ -0,0 +1,12 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
class TagsBase(BaseModel):
|
||||
name: str
|
||||
|
||||
class Tags(TagsBase):
|
||||
id: int
|
||||
posts: Optional[list]
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
10
app/api/v1/schemas/user.py
Normal file
10
app/api/v1/schemas/user.py
Normal file
@ -0,0 +1,10 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class UserBase(BaseModel):
|
||||
pass
|
||||
|
||||
class User(BaseModel):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
15
main.py
15
main.py
@ -1,7 +1,10 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI(title="Generated Backend")
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"message": "Welcome to the generated backend"}
|
||||
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()
|
||||
app.add_middleware(ActivityTrackerMiddleware)
|
||||
app.include_router(router, prefix='/v1')
|
||||
@app.on_event('startup')
|
||||
def startup():
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
@ -2,3 +2,4 @@ fastapi
|
||||
uvicorn
|
||||
sqlalchemy
|
||||
pydantic
|
||||
loguru
|
Loading…
x
Reference in New Issue
Block a user