3/app/api/v1/routes/comments.py
2025-03-20 12:49:08 +01:00

24 lines
1020 B
Python

from typing import List
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.api.v1.models.comments import Comment
from app.api.v1.schemas.comments import CommentCreate, CommentResponse
from app.api.core.dependencies.dependencies import get_db
router = APIRouter()
@router.get("/commentss", response_model=List[CommentResponse])
def read_comments(db: Session = Depends(get_db)):
comments = db.query(Comment).all()
return comments
@router.post("/commentss", response_model=CommentResponse)
def create_comment(comment: CommentCreate, db: Session = Depends(get_db)):
db_comment = Comment(**comment.dict())
db.add(db_comment)
db.commit()
db.refresh(db_comment)
return db_comment
@router.get("/commentss/{id}", response_model=CommentResponse)
def read_comment(id: int, db: Session = Depends(get_db)):
comment = db.query(Comment).get(id)
if not comment:
raise HTTPException(status_code=404, detail="Comment not found")
return comment