2025-09-20 18:13:00 +03:30

45 lines
1.3 KiB
Python

from typing import List
from fastapi import APIRouter, HTTPException
from app.schemas.post import PostCreate, PostRead, PostUpdate
from app.services import posts as posts_service
router = APIRouter(prefix="/posts", tags=["posts"])
@router.get("/", response_model=List[PostRead])
def read_posts():
posts = posts_service.list_posts()
return posts
@router.post("/", response_model=PostRead, status_code=201)
def create_post(payload: PostCreate):
post = posts_service.create_post(payload.name, payload.description, payload.price)
return post
@router.get("/{post_id}", response_model=PostRead)
def read_post(post_id: int):
post = posts_service.get_post(post_id)
if not post:
raise HTTPException(status_code=404, detail="Post not found")
return post
@router.patch("/{post_id}", response_model=PostRead)
def update_post(post_id: int, payload: PostUpdate):
post = posts_service.update_post(post_id, **payload.dict())
if not post:
raise HTTPException(status_code=404, detail="Post not found")
return post
@router.delete("/{post_id}", status_code=204)
def delete_post(post_id: int):
deleted = posts_service.delete_post(post_id)
if not deleted:
raise HTTPException(status_code=404, detail="Post not found")
return None