From 09af172fa51c47ab71fdfac4adc719e9c49ffbfa Mon Sep 17 00:00:00 2001 From: Backend IM Bot <> Date: Sun, 23 Mar 2025 18:37:27 +0100 Subject: [PATCH] Update generated backend for blog_app with entities: posts, comments, tags, user --- app/api/core/dependencies/dependencies.py | 8 ++++---- app/api/core/middleware/activity_tracker.py | 2 +- app/api/v1/models/comments.py | 6 ++---- app/api/v1/models/tags.py | 11 ++++------- app/api/v1/models/user.py | 6 +++++- app/api/v1/schemas/comments.py | 10 +++++++--- app/api/v1/schemas/posts.py | 11 +++-------- 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/app/api/core/dependencies/dependencies.py b/app/api/core/dependencies/dependencies.py index 965f5ff..4b07c80 100644 --- a/app/api/core/dependencies/dependencies.py +++ b/app/api/core/dependencies/dependencies.py @@ -1,8 +1,8 @@ -from sqlalchemy.orm import Session -from app.api.db.database import SessionLocal +import sqlalchemy.orm as _orm +import app.api.db.database as _database -def get_db(): - db = SessionLocal() +def get_db() -> _orm.Session: + db = _database.SessionLocal() try: yield db finally: diff --git a/app/api/core/middleware/activity_tracker.py b/app/api/core/middleware/activity_tracker.py index 81144b4..d833f23 100644 --- a/app/api/core/middleware/activity_tracker.py +++ b/app/api/core/middleware/activity_tracker.py @@ -9,5 +9,5 @@ class ActivityTrackerMiddleware(BaseHTTPMiddleware): 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") + logger.info(f"Request to {request.url.path} took {process_time:.4f} seconds") return response \ No newline at end of file diff --git a/app/api/v1/models/comments.py b/app/api/v1/models/comments.py index 1abe285..a254cb2 100644 --- a/app/api/v1/models/comments.py +++ b/app/api/v1/models/comments.py @@ -8,14 +8,12 @@ class Comments(Base): __tablename__ = 'comments' id = Column(Integer, primary_key=True, index=True) + content = Column(Text) 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}")' \ No newline at end of file + return f'Comment(id={self.id}, content="{self.content[:20]}...")' \ No newline at end of file diff --git a/app/api/v1/models/tags.py b/app/api/v1/models/tags.py index 64074a9..25d01e3 100644 --- a/app/api/v1/models/tags.py +++ b/app/api/v1/models/tags.py @@ -1,6 +1,3 @@ -# 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 @@ -9,11 +6,11 @@ class Tags(Base): __tablename__ = 'tags' id = Column(Integer, primary_key=True, index=True) - name = Column(String, nullable=False) + tag_name = Column(String(50), nullable=False, unique=True) description = Column(Text, nullable=True) - post_id = Column(Integer, ForeignKey('posts.id'), nullable=False) + # Additional columns and relationships as needed - post = relationship('Post', back_populates='tags') + posts = relationship('Post', back_populates='tags', secondary='post_tags') def __repr__(self): - return f'Tag(id={self.id}, name={self.name}, description={self.description})' \ No newline at end of file + return f'Tag(id={self.id}, tag_name="{self.tag_name}")' \ No newline at end of file diff --git a/app/api/v1/models/user.py b/app/api/v1/models/user.py index 90e171e..4fb8262 100644 --- a/app/api/v1/models/user.py +++ b/app/api/v1/models/user.py @@ -1,3 +1,4 @@ +from typing import Optional from sqlalchemy import Column, ForeignKey, Integer, String, Text from sqlalchemy.orm import relationship from app.api.db.database import Base @@ -9,10 +10,13 @@ class User(Base): 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) + is_active = Column(Integer, default=1) + is_admin = Column(Integer, default=0) + # Relationships posts = relationship("Post", back_populates="author") + comments = relationship("Comment", back_populates="author") def __repr__(self): return f"User(id={self.id}, username='{self.username}', email='{self.email}')" \ No newline at end of file diff --git a/app/api/v1/schemas/comments.py b/app/api/v1/schemas/comments.py index a04f3ef..f439966 100644 --- a/app/api/v1/schemas/comments.py +++ b/app/api/v1/schemas/comments.py @@ -1,10 +1,14 @@ from pydantic import BaseModel class CommentsBase(BaseModel): - pass + body: str + post_id: int + user_id: int -class Comments(BaseModel): +class Comments(CommentsBase): id: int + post: dict = None + user: dict = None class Config: - orm_mode = True + orm_mode = True \ No newline at end of file diff --git a/app/api/v1/schemas/posts.py b/app/api/v1/schemas/posts.py index 08bd7a6..7536ecf 100644 --- a/app/api/v1/schemas/posts.py +++ b/app/api/v1/schemas/posts.py @@ -1,15 +1,10 @@ from pydantic import BaseModel -from typing import Optional class PostsBase(BaseModel): - title: str - content: str - published: bool = True - rating: Optional[int] = None + pass -class Posts(PostsBase): +class Posts(BaseModel): id: int - owner_id: int class Config: - orm_mode = True \ No newline at end of file + orm_mode = True