From 78cdbac2cf50285548491ba6886bf36ad3ba024d Mon Sep 17 00:00:00 2001 From: Backend IM Bot <> Date: Fri, 21 Mar 2025 12:27:43 +0100 Subject: [PATCH] Update generated backend for blog_app with entities: posts, comments, tags, user --- app/api/core/middleware/activity_tracker.py | 10 ++++----- app/api/v1/models/comments.py | 4 ++-- app/api/v1/models/posts.py | 4 +--- app/api/v1/models/tags.py | 8 +++---- app/api/v1/models/user.py | 23 +++++++++++++-------- app/api/v1/schemas/comments.py | 7 ++++--- app/api/v1/schemas/posts.py | 5 ++++- app/api/v1/schemas/tags.py | 7 +++---- app/api/v1/schemas/user.py | 9 ++++---- main.py | 8 +++---- 10 files changed, 44 insertions(+), 41 deletions(-) diff --git a/app/api/core/middleware/activity_tracker.py b/app/api/core/middleware/activity_tracker.py index 2779748..7fe5fc8 100644 --- a/app/api/core/middleware/activity_tracker.py +++ b/app/api/core/middleware/activity_tracker.py @@ -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 \ No newline at end of file diff --git a/app/api/v1/models/comments.py b/app/api/v1/models/comments.py index 07b33ec..bd737a7 100644 --- a/app/api/v1/models/comments.py +++ b/app/api/v1/models/comments.py @@ -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]}...")' \ No newline at end of file + 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}")' \ No newline at end of file diff --git a/app/api/v1/models/posts.py b/app/api/v1/models/posts.py index 6c5478e..563285b 100644 --- a/app/api/v1/models/posts.py +++ b/app/api/v1/models/posts.py @@ -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') diff --git a/app/api/v1/models/tags.py b/app/api/v1/models/tags.py index 1d693f6..b5a6506 100644 --- a/app/api/v1/models/tags.py +++ b/app/api/v1/models/tags.py @@ -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})' \ No newline at end of file + return f'Tag(id={self.id}, name="{self.name}")' \ No newline at end of file diff --git a/app/api/v1/models/user.py b/app/api/v1/models/user.py index 7b86a0c..39f0704 100644 --- a/app/api/v1/models/user.py +++ b/app/api/v1/models/user.py @@ -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}')" \ No newline at end of file + # Optional __repr__ method + # def __repr__(self): + # return f"User(id={self.id}, name={self.name}, 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 f23b92e..de1cdcf 100644 --- a/app/api/v1/schemas/comments.py +++ b/app/api/v1/schemas/comments.py @@ -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 \ No newline at end of file diff --git a/app/api/v1/schemas/posts.py b/app/api/v1/schemas/posts.py index 5962b00..14cb1e0 100644 --- a/app/api/v1/schemas/posts.py +++ b/app/api/v1/schemas/posts.py @@ -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 \ No newline at end of file diff --git a/app/api/v1/schemas/tags.py b/app/api/v1/schemas/tags.py index 92f9fd2..e212f4d 100644 --- a/app/api/v1/schemas/tags.py +++ b/app/api/v1/schemas/tags.py @@ -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 \ No newline at end of file + orm_mode = True diff --git a/app/api/v1/schemas/user.py b/app/api/v1/schemas/user.py index 7ef2cc4..819e527 100644 --- a/app/api/v1/schemas/user.py +++ b/app/api/v1/schemas/user.py @@ -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 \ No newline at end of file + orm_mode = True diff --git a/main.py b/main.py index c59cecf..1024a94 100644 --- a/main.py +++ b/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) \ No newline at end of file +@app.on_event('startup') +def startup(): + Base.metadata.create_all(bind=engine)