Update generated backend for blog_app with entities: posts, comments, tags, user
This commit is contained in:
parent
c61abb1d18
commit
78cdbac2cf
@ -1,4 +1,4 @@
|
||||
from time import time
|
||||
import time
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
@ -6,8 +6,8 @@ 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")
|
||||
start_time = time.time()
|
||||
response: Response = await call_next(request)
|
||||
process_time = time.time() - start_time
|
||||
logger.info(f"Request processing time: {process_time:.5f} seconds")
|
||||
return response
|
@ -10,7 +10,7 @@ class Comments(Base):
|
||||
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)
|
||||
comment_text = Column(Text)
|
||||
created_at = Column(String)
|
||||
updated_at = Column(String, nullable=True)
|
||||
|
||||
@ -18,4 +18,4 @@ class Comments(Base):
|
||||
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]}...")'
|
||||
return f'Comment(id={self.id}, post_id={self.post_id}, user_id={self.user_id}, comment_text="{self.comment_text[:20]}...", created_at="{self.created_at}", updated_at="{self.updated_at}")'
|
@ -1,5 +1,3 @@
|
||||
# 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
|
||||
@ -8,9 +6,9 @@ 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_id = Column(Integer, ForeignKey('users.id'))
|
||||
|
||||
user = relationship('User', back_populates='posts')
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# app/api/v1/models/tags.py
|
||||
|
||||
from sqlalchemy import Column, ForeignKey, Integer, String
|
||||
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.api.db.database import Base
|
||||
|
||||
@ -8,10 +8,10 @@ class Tags(Base):
|
||||
__tablename__ = 'tags'
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, nullable=False)
|
||||
description = Column(Text)
|
||||
name = Column(String, nullable=False, unique=True)
|
||||
description = Column(Text, nullable=True)
|
||||
|
||||
posts = relationship('Post', back_populates='tags', secondary='post_tags')
|
||||
|
||||
def __repr__(self):
|
||||
return f'Tag(id={self.id}, name={self.name})'
|
||||
return f'Tag(id={self.id}, name="{self.name}")'
|
@ -1,20 +1,25 @@
|
||||
# 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
|
||||
|
||||
# Optional imports
|
||||
# from typing import Optional
|
||||
|
||||
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)
|
||||
# Add relevant columns for user entity
|
||||
# For example:
|
||||
# name = Column(String)
|
||||
# email = Column(String, unique=True, index=True)
|
||||
# hashed_password = Column(String)
|
||||
# bio = Column(Text, nullable=True)
|
||||
|
||||
# Define relationships here if needed
|
||||
# Define relationships
|
||||
# For example:
|
||||
# posts = relationship("Post", back_populates="author")
|
||||
|
||||
def __repr__(self):
|
||||
return f"User(id={self.id}, username='{self.username}', email='{self.email}')"
|
||||
# Optional __repr__ method
|
||||
# def __repr__(self):
|
||||
# return f"User(id={self.id}, name={self.name}, email={self.email})"
|
@ -1,13 +1,14 @@
|
||||
app/api/v1/schemas/comments.py:
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
class CommentsBase(BaseModel):
|
||||
text: str
|
||||
post_id: int
|
||||
user_id: int
|
||||
|
||||
class Comments(CommentsBase):
|
||||
id: int
|
||||
post_id: int
|
||||
post: dict = None
|
||||
user: dict = None
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
@ -1,12 +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
|
||||
user_id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
@ -1,11 +1,10 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class TagsBase(BaseModel):
|
||||
name: str
|
||||
pass
|
||||
|
||||
class Tags(TagsBase):
|
||||
class Tags(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
orm_mode = True
|
||||
|
@ -1,11 +1,10 @@
|
||||
from pydantic import BaseModel, EmailStr
|
||||
from pydantic import BaseModel
|
||||
|
||||
class UserBase(BaseModel):
|
||||
email: EmailStr
|
||||
username: str
|
||||
pass
|
||||
|
||||
class User(UserBase):
|
||||
class User(BaseModel):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
orm_mode = True
|
||||
|
8
main.py
8
main.py
@ -2,11 +2,9 @@ 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()
|
||||
app.add_middleware(ActivityTrackerMiddleware)
|
||||
app.include_router(router, prefix='/v1')
|
||||
|
||||
@app.on_event("startup")
|
||||
def startup_event():
|
||||
Base.metadata.create_all(bind=engine)
|
||||
@app.on_event('startup')
|
||||
def startup():
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
Loading…
x
Reference in New Issue
Block a user