|  | 
|  | 1 | +import uuid | 
|  | 2 | +from typing import Any | 
|  | 3 | + | 
|  | 4 | +from fastapi import APIRouter, Depends, HTTPException | 
|  | 5 | +from sqlmodel import col, delete, func, select | 
|  | 6 | + | 
|  | 7 | +from app import crud | 
|  | 8 | +from app.api.deps import CurrentUser, SessionDep | 
|  | 9 | +from app.models import ( | 
|  | 10 | +    Post, | 
|  | 11 | +    PostCreate, | 
|  | 12 | +    PostPublic, | 
|  | 13 | +    PostUpdate, | 
|  | 14 | +    PostsPublic, | 
|  | 15 | +    Message, | 
|  | 16 | +) | 
|  | 17 | + | 
|  | 18 | +router = APIRouter(prefix="/posts", tags=["posts"]) | 
|  | 19 | + | 
|  | 20 | + | 
|  | 21 | +@router.get("/", response_model=PostsPublic) | 
|  | 22 | +def read_posts(session: SessionDep, skip: int = 0, limit: int = 100) -> Any: | 
|  | 23 | +    """ | 
|  | 24 | +    Retrieve all posts. | 
|  | 25 | +    """ | 
|  | 26 | +    count_statement = select(func.count()).select_from(Post) | 
|  | 27 | +    count = session.exec(count_statement).one() | 
|  | 28 | + | 
|  | 29 | +    statement = select(Post).offset(skip).limit(limit) | 
|  | 30 | +    posts = session.exec(statement).all() | 
|  | 31 | + | 
|  | 32 | +    return PostsPublic(data=posts, count=count) | 
|  | 33 | + | 
|  | 34 | + | 
|  | 35 | +@router.post("/", response_model=PostPublic) | 
|  | 36 | +def create_post( | 
|  | 37 | +    *, | 
|  | 38 | +    session: SessionDep, | 
|  | 39 | +    post_in: PostCreate, | 
|  | 40 | +    current_user: CurrentUser | 
|  | 41 | +) -> Any: | 
|  | 42 | +    """ | 
|  | 43 | +    Create a new post. | 
|  | 44 | +    """ | 
|  | 45 | +    new_post = Post( | 
|  | 46 | +        user_id=current_user.id, | 
|  | 47 | +        content=post_in.content, | 
|  | 48 | +        image1_url=post_in.image1_url, | 
|  | 49 | +        image2_url=post_in.image2_url, | 
|  | 50 | +        image3_url=post_in.image3_url, | 
|  | 51 | +    ) | 
|  | 52 | +    session.add(new_post) | 
|  | 53 | +    session.commit() | 
|  | 54 | +    session.refresh(new_post) | 
|  | 55 | +    return new_post | 
|  | 56 | + | 
|  | 57 | + | 
|  | 58 | +@router.get("/{post_id}", response_model=PostPublic) | 
|  | 59 | +def read_post_by_id(post_id: uuid.UUID, session: SessionDep) -> Any: | 
|  | 60 | +    """ | 
|  | 61 | +    Get a specific post by ID. | 
|  | 62 | +    """ | 
|  | 63 | +    post = session.get(Post, post_id) | 
|  | 64 | +    if not post: | 
|  | 65 | +        raise HTTPException(status_code=404, detail="Post not found") | 
|  | 66 | +    return post | 
|  | 67 | + | 
|  | 68 | + | 
|  | 69 | +@router.patch("/{post_id}", response_model=PostPublic) | 
|  | 70 | +def update_post( | 
|  | 71 | +    *, | 
|  | 72 | +    session: SessionDep, | 
|  | 73 | +    post_id: uuid.UUID, | 
|  | 74 | +    post_in: PostUpdate, | 
|  | 75 | +    current_user: CurrentUser | 
|  | 76 | +) -> Any: | 
|  | 77 | +    """ | 
|  | 78 | +    Update a post. | 
|  | 79 | +    """ | 
|  | 80 | +    db_post = session.get(Post, post_id) | 
|  | 81 | +    if not db_post: | 
|  | 82 | +        raise HTTPException(status_code=404, detail="Post not found") | 
|  | 83 | +    if db_post.user_id != current_user.id: | 
|  | 84 | +        raise HTTPException(status_code=403, detail="Not authorized to update this post") | 
|  | 85 | + | 
|  | 86 | +    post_data = post_in.model_dump(exclude_unset=True) | 
|  | 87 | +    db_post.sqlmodel_update(post_data) | 
|  | 88 | +    session.add(db_post) | 
|  | 89 | +    session.commit() | 
|  | 90 | +    session.refresh(db_post) | 
|  | 91 | +    return db_post | 
|  | 92 | + | 
|  | 93 | + | 
|  | 94 | +@router.delete("/{post_id}", response_model=Message) | 
|  | 95 | +def delete_post( | 
|  | 96 | +    session: SessionDep, post_id: uuid.UUID, current_user: CurrentUser | 
|  | 97 | +) -> Any: | 
|  | 98 | +    """ | 
|  | 99 | +    Delete a post. | 
|  | 100 | +    """ | 
|  | 101 | +    post = session.get(Post, post_id) | 
|  | 102 | +    if not post: | 
|  | 103 | +        raise HTTPException(status_code=404, detail="Post not found") | 
|  | 104 | +    if post.user_id != current_user.id: | 
|  | 105 | +        raise HTTPException(status_code=403, detail="Not authorized to delete this post") | 
|  | 106 | + | 
|  | 107 | +    session.delete(post) | 
|  | 108 | +    session.commit() | 
|  | 109 | +    return Message(message="Post deleted successfully") | 
0 commit comments