29 lines
1.0 KiB
Python
29 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 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_commentss(db: Session = Depends(get_db)):
|
|
commentss = db.query(Comment).all()
|
|
return commentss
|
|
|
|
@router.post("/commentss", response_model=CommentResponse)
|
|
def create_comments(comments: CommentCreate, db: Session = Depends(get_db)):
|
|
db_comments = Comment(**comments.dict())
|
|
db.add(db_comments)
|
|
db.commit()
|
|
db.refresh(db_comments)
|
|
return db_comments
|
|
|
|
@router.get("/commentss/{id}", response_model=CommentResponse)
|
|
def read_comments(id: int, db: Session = Depends(get_db)):
|
|
db_comments = db.query(Comment).get(id)
|
|
if not db_comments:
|
|
raise HTTPException(status_code=404, detail="Comments not found")
|
|
return db_comments |