
- 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
106 lines
2.8 KiB
Python
106 lines
2.8 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.api import deps
|
|
from app.models.user import User
|
|
from app.schemas.post import Post, PostCreate, PostUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Post])
|
|
def read_posts(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
author_id: Optional[int] = None,
|
|
) -> Any:
|
|
"""
|
|
Retrieve posts.
|
|
"""
|
|
if author_id:
|
|
posts = crud.post.get_multi_by_author(
|
|
db, author_id=author_id, skip=skip, limit=limit
|
|
)
|
|
else:
|
|
posts = crud.post.get_multi_with_details(db, skip=skip, limit=limit)
|
|
return posts
|
|
|
|
|
|
@router.post("/", response_model=Post)
|
|
def create_post(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
post_in: PostCreate,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new post.
|
|
"""
|
|
post = crud.post.create_with_author(
|
|
db=db, obj_in=post_in, author_id=current_user.id
|
|
)
|
|
return crud.post.get_with_details(db=db, id=post.id)
|
|
|
|
|
|
@router.get("/{id}", response_model=Post)
|
|
def read_post(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Get post by ID.
|
|
"""
|
|
post = crud.post.get_with_details(db=db, id=id)
|
|
if not post:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Post not found")
|
|
return post
|
|
|
|
|
|
@router.put("/{id}", response_model=Post)
|
|
def update_post(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
post_in: PostUpdate,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a post.
|
|
"""
|
|
post = crud.post.get(db=db, id=id)
|
|
if not post:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Post not found")
|
|
if post.author_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions to update this post",
|
|
)
|
|
post = crud.post.update_with_tags(db=db, db_obj=post, obj_in=post_in)
|
|
return crud.post.get_with_details(db=db, id=post.id)
|
|
|
|
|
|
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_post(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a post.
|
|
"""
|
|
post = crud.post.get(db=db, id=id)
|
|
if not post:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Post not found")
|
|
if post.author_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions to delete this post",
|
|
)
|
|
crud.post.remove(db=db, id=id)
|
|
return None |