Files
blogweb/backend/app/api/api_v1/endpoints/posts.py
T
2025-12-26 13:42:22 +08:00

143 lines
3.6 KiB
Python

"""
博客文章相关 API 端点
"""
from fastapi import APIRouter, Depends, HTTPException, status
from sqlmodel import Session, select
from typing import List
from app.db.session import get_db
from app.models.post import Post
from app.models.user import User
from app.schemas.post import Post as PostSchema, PostCreate, PostUpdate
from app.api.api_v1.endpoints.users import get_current_user
router = APIRouter()
@router.post("/", response_model=PostSchema, status_code=status.HTTP_201_CREATED)
def create_post(
post_in: PostCreate,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""创建博客文章"""
# 检查 slug 是否已存在(同一用户)
statement = select(Post).where(
Post.slug == post_in.slug,
Post.user_id == current_user.id
)
existing_post = db.exec(statement).first()
if existing_post:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="该 slug 已存在"
)
db_post = Post(
title=post_in.title,
slug=post_in.slug,
content=post_in.content,
user_id=current_user.id
)
db.add(db_post)
db.commit()
db.refresh(db_post)
return db_post
@router.get("/", response_model=List[PostSchema])
def read_posts(
skip: int = 0,
limit: int = 100,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""获取当前用户的博客文章列表"""
statement = (
select(Post)
.where(Post.user_id == current_user.id)
.offset(skip)
.limit(limit)
)
posts = db.exec(statement).all()
return posts
@router.get("/{post_id}", response_model=PostSchema)
def read_post(
post_id: int,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""获取单个博客文章"""
statement = select(Post).where(
Post.id == post_id,
Post.user_id == current_user.id
)
post = db.exec(statement).first()
if post is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="文章不存在"
)
return post
@router.put("/{post_id}", response_model=PostSchema)
def update_post(
post_id: int,
post_in: PostUpdate,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""更新博客文章"""
statement = select(Post).where(
Post.id == post_id,
Post.user_id == current_user.id
)
post = db.exec(statement).first()
if post is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="文章不存在"
)
if post_in.title is not None:
post.title = post_in.title
if post_in.slug is not None:
post.slug = post_in.slug
if post_in.content is not None:
post.content = post_in.content
from datetime import datetime
post.updated_at = datetime.now()
db.add(post)
db.commit()
db.refresh(post)
return post
@router.delete("/{post_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_post(
post_id: int,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""删除博客文章"""
statement = select(Post).where(
Post.id == post_id,
Post.user_id == current_user.id
)
post = db.exec(statement).first()
if post is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="文章不存在"
)
db.delete(post)
db.commit()
return None