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

24 lines
1.0 KiB
Python

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