
- Create project structure with app organization - Set up FastAPI application with CORS and health endpoint - Implement database models with SQLAlchemy (User, Post, Comment) - Set up Alembic for database migrations - Implement authentication with JWT tokens - Create CRUD operations for all models - Implement REST API endpoints for users, posts, and comments - Add comprehensive documentation in README.md
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
from typing import List
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.auth.security import generate_uuid
|
|
from app.crud.base import CRUDBase
|
|
from app.models.comment import Comment
|
|
from app.schemas.comment import CommentCreate, CommentUpdate
|
|
|
|
class CRUDComment(CRUDBase[Comment, CommentCreate, CommentUpdate]):
|
|
def create_with_author(
|
|
self, db: Session, *, obj_in: CommentCreate, author_id: str
|
|
) -> Comment:
|
|
obj_in_data = obj_in.dict()
|
|
db_obj = Comment(
|
|
id=generate_uuid(),
|
|
**obj_in_data,
|
|
author_id=author_id,
|
|
)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def get_multi_by_post(
|
|
self, db: Session, *, post_id: str, skip: int = 0, limit: int = 100
|
|
) -> List[Comment]:
|
|
return (
|
|
db.query(self.model)
|
|
.filter(Comment.post_id == post_id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def get_multi_by_author(
|
|
self, db: Session, *, author_id: str, skip: int = 0, limit: int = 100
|
|
) -> List[Comment]:
|
|
return (
|
|
db.query(self.model)
|
|
.filter(Comment.author_id == author_id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
comment = CRUDComment(Comment) |