Automated Action 06df0285b1 Implement Blogging Platform API with FastAPI and SQLite
- Set up project structure and dependencies
- Create database models for users, posts, comments, and tags
- Set up Alembic for database migrations
- Implement user authentication (register, login)
- Create CRUD endpoints for blog posts, comments, and tags
- Add health check endpoint
- Set up proper error handling
- Update README with project details and setup instructions
2025-06-02 21:58:50 +00:00

120 lines
3.3 KiB
Python

from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlalchemy.orm import Session
from app import crud
from app.api import deps
from app.models.user import User
from app.schemas.comment import Comment, CommentCreate, CommentUpdate
router = APIRouter()
@router.get("/", response_model=List[Comment])
def read_comments(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
post_id: int = Query(None, description="Filter comments by post_id"),
) -> Any:
"""
Retrieve comments.
"""
if post_id:
comments = crud.comment.get_multi_by_post(
db, post_id=post_id, skip=skip, limit=limit
)
else:
comments = crud.comment.get_multi_with_author(db, skip=skip, limit=limit)
return comments
@router.post("/", response_model=Comment)
def create_comment(
*,
db: Session = Depends(deps.get_db),
comment_in: CommentCreate,
current_user: User = Depends(deps.get_current_active_user),
) -> Any:
"""
Create new comment.
"""
# Check if post exists
post = crud.post.get(db=db, id=comment_in.post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Post not found",
)
comment = crud.comment.create_with_author(
db=db, obj_in=comment_in, author_id=current_user.id
)
return crud.comment.get_with_author(db=db, id=comment.id)
@router.get("/{id}", response_model=Comment)
def read_comment(
*,
db: Session = Depends(deps.get_db),
id: int,
) -> Any:
"""
Get comment by ID.
"""
comment = crud.comment.get_with_author(db=db, id=id)
if not comment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Comment not found"
)
return comment
@router.put("/{id}", response_model=Comment)
def update_comment(
*,
db: Session = Depends(deps.get_db),
id: int,
comment_in: CommentUpdate,
current_user: User = Depends(deps.get_current_active_user),
) -> Any:
"""
Update a comment.
"""
comment = crud.comment.get(db=db, id=id)
if not comment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Comment not found"
)
if comment.author_id != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions to update this comment",
)
comment = crud.comment.update(db=db, db_obj=comment, obj_in=comment_in)
return crud.comment.get_with_author(db=db, id=comment.id)
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
def delete_comment(
*,
db: Session = Depends(deps.get_db),
id: int,
current_user: User = Depends(deps.get_current_active_user),
) -> Any:
"""
Delete a comment.
"""
comment = crud.comment.get(db=db, id=id)
if not comment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Comment not found"
)
if comment.author_id != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions to delete this comment",
)
crud.comment.remove(db=db, id=id)
return None