28 lines
885 B
Python
28 lines
885 B
Python
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, AndOut
|
|
from app.api.core.dependencies.dependencies import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/ands", response_model=list[AndOut])
|
|
def read_ands(db: Session = Depends(get_db)):
|
|
ands = db.query(And).all()
|
|
return ands
|
|
|
|
@router.post("/ands", response_model=AndOut)
|
|
def create_and(and_in: AndCreate, db: Session = Depends(get_db)):
|
|
and_data = And(**and_in.dict())
|
|
db.add(and_data)
|
|
db.commit()
|
|
db.refresh(and_data)
|
|
return and_data
|
|
|
|
@router.get("/ands/{id}", response_model=AndOut)
|
|
def read_and(id: int, db: Session = Depends(get_db)):
|
|
and_data = db.query(And).get(id)
|
|
if not and_data:
|
|
raise HTTPException(status_code=404, detail="And not found")
|
|
return and_data |