
- 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.2 KiB
Python
46 lines
1.2 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.post import Post
|
|
from app.schemas.post import PostCreate, PostUpdate
|
|
|
|
class CRUDPost(CRUDBase[Post, PostCreate, PostUpdate]):
|
|
def create_with_author(
|
|
self, db: Session, *, obj_in: PostCreate, author_id: str
|
|
) -> Post:
|
|
obj_in_data = obj_in.dict()
|
|
db_obj = Post(
|
|
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_author(
|
|
self, db: Session, *, author_id: str, skip: int = 0, limit: int = 100
|
|
) -> List[Post]:
|
|
return (
|
|
db.query(self.model)
|
|
.filter(Post.author_id == author_id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def get_multi_published(
|
|
self, db: Session, *, skip: int = 0, limit: int = 100
|
|
) -> List[Post]:
|
|
return (
|
|
db.query(self.model)
|
|
.filter(Post.is_published.is_(True))
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
post = CRUDPost(Post) |