37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app.api.core.dependencies import get_db
|
|
from app.api.v1.services.posts import get_posts, get_all_posts, create_posts, update_posts, delete_posts
|
|
from app.api.v1.schemas.posts import PostsBase
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/posts/", response_model=list[PostsBase])
|
|
def read_posts(db: Session = Depends(get_db)):
|
|
posts = get_all_posts(db)
|
|
return posts
|
|
|
|
@router.get("/posts/{id}", response_model=PostsBase)
|
|
def read_post(id: int, db: Session = Depends(get_db)):
|
|
post = get_posts(db, id)
|
|
if not post:
|
|
raise HTTPException(status_code=404, detail="Post not found")
|
|
return post
|
|
|
|
@router.post("/posts/", response_model=PostsBase)
|
|
def create_post(post: PostsBase, db: Session = Depends(get_db)):
|
|
return create_posts(db, post)
|
|
|
|
@router.put("/posts/{id}", response_model=PostsBase)
|
|
def update_post(id: int, post: PostsBase, db: Session = Depends(get_db)):
|
|
updated_post = update_posts(db, id, post)
|
|
if not updated_post:
|
|
raise HTTPException(status_code=404, detail="Post not found")
|
|
return updated_post
|
|
|
|
@router.delete("/posts/{id}", response_model=PostsBase)
|
|
def delete_post(id: int, db: Session = Depends(get_db)):
|
|
deleted_post = delete_posts(db, id)
|
|
if not deleted_post:
|
|
raise HTTPException(status_code=404, detail="Post not found")
|
|
return deleted_post |