24 lines
921 B
Python
24 lines
921 B
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app.api.v1.models.and import And
|
|
from app.api.v1.schemas.and import AndCreate, AndResponse
|
|
from app.api.core.dependencies.dependencies import get_db
|
|
router = APIRouter()
|
|
@router.get("/ands", response_model=List[AndResponse])
|
|
def read_ands(db: Session = Depends(get_db)):
|
|
ands = db.query(And).all()
|
|
return ands
|
|
@router.post("/ands", response_model=AndResponse)
|
|
def create_and(and_data: AndCreate, db: Session = Depends(get_db)):
|
|
and_obj = And(**and_data.dict())
|
|
db.add(and_obj)
|
|
db.commit()
|
|
db.refresh(and_obj)
|
|
return and_obj
|
|
@router.get("/ands/{id}", response_model=AndResponse)
|
|
def read_and(id: int, db: Session = Depends(get_db)):
|
|
and_obj = db.query(And).get(id)
|
|
if not and_obj:
|
|
raise HTTPException(status_code=404, detail="And not found")
|
|
return and_obj |