Update generated backend for blog_app with entities: posts, comments, tags, user
This commit is contained in:
parent
d5a6377dff
commit
9fa6b8df88
8
app/api/core/dependencies/dependencies.py
Normal file
8
app/api/core/dependencies/dependencies.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.api.db.database import SessionLocal
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
12
app/api/core/middleware/activity_tracker.py
Normal file
12
app/api/core/middleware/activity_tracker.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
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 = await call_next(request)
|
||||||
|
process_time = time.time() - start_time
|
||||||
|
logger.info(f'{request.method} {request.url} - Process Time: {process_time:.6f} 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()
|
7
app/api/v1/models/comments.py
Normal file
7
app/api/v1/models/comments.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||||
|
from app.api.db.database import Base
|
||||||
|
|
||||||
|
class Comments(Base):
|
||||||
|
__tablename__ = 'comments'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
7
app/api/v1/models/posts.py
Normal file
7
app/api/v1/models/posts.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||||
|
from app.api.db.database import Base
|
||||||
|
|
||||||
|
class Posts(Base):
|
||||||
|
__tablename__ = 'posts'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
7
app/api/v1/models/tags.py
Normal file
7
app/api/v1/models/tags.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||||
|
from app.api.db.database import Base
|
||||||
|
|
||||||
|
class Tags(Base):
|
||||||
|
__tablename__ = 'tags'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
7
app/api/v1/models/user.py
Normal file
7
app/api/v1/models/user.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||||
|
from app.api.db.database import Base
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = 'user'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
@ -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
|
10
app/api/v1/schemas/posts.py
Normal file
10
app/api/v1/schemas/posts.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class PostsBase(BaseModel):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Posts(BaseModel):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
10
app/api/v1/schemas/tags.py
Normal file
10
app/api/v1/schemas/tags.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class TagsBase(BaseModel):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Tags(BaseModel):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
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
|
from fastapi import FastAPI
|
||||||
|
from app.api.db.database import engine, Base
|
||||||
app = FastAPI(title="Generated Backend")
|
from app.api.v1.routes import router
|
||||||
|
from app.api.core.middleware.activity_tracker import ActivityTrackerMiddleware
|
||||||
@app.get("/")
|
app = FastAPI()
|
||||||
def read_root():
|
app.add_middleware(ActivityTrackerMiddleware)
|
||||||
return {"message": "Welcome to the generated backend"}
|
app.include_router(router, prefix='/v1')
|
||||||
|
@app.on_event('startup')
|
||||||
|
def startup():
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
@ -1,4 +1 @@
|
|||||||
fastapi
|
# No code generated
|
||||||
uvicorn
|
|
||||||
sqlalchemy
|
|
||||||
pydantic
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user