39 lines
1.6 KiB
Python
39 lines
1.6 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.comments import get_comments, get_all_comments, create_comments, update_comments, delete_comments
|
|
from app.api.v1.schemas.comments import CommentsBase
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/comments/", response_model=list[CommentsBase])
|
|
def read_comments(db: Session = Depends(get_db)):
|
|
comments = get_all_comments(db)
|
|
if not comments:
|
|
raise HTTPException(status_code=404, detail="Comments not found")
|
|
return comments
|
|
|
|
@router.get("/comments/{id}", response_model=CommentsBase)
|
|
def read_comment(id: int, db: Session = Depends(get_db)):
|
|
comment = get_comments(db, id)
|
|
if not comment:
|
|
raise HTTPException(status_code=404, detail="Comment not found")
|
|
return comment
|
|
|
|
@router.post("/comments/", response_model=CommentsBase)
|
|
def create_comment(comment: CommentsBase, db: Session = Depends(get_db)):
|
|
return create_comments(db, comment)
|
|
|
|
@router.put("/comments/{id}", response_model=CommentsBase)
|
|
def update_comment(id: int, comment: CommentsBase, db: Session = Depends(get_db)):
|
|
updated_comment = update_comments(db, id, comment)
|
|
if not updated_comment:
|
|
raise HTTPException(status_code=404, detail="Comment not found")
|
|
return updated_comment
|
|
|
|
@router.delete("/comments/{id}", response_model=CommentsBase)
|
|
def delete_comment(id: int, db: Session = Depends(get_db)):
|
|
deleted_comment = delete_comments(db, id)
|
|
if not deleted_comment:
|
|
raise HTTPException(status_code=404, detail="Comment not found")
|
|
return deleted_comment |