49 lines
1001 B
Python
49 lines
1001 B
Python
from fastapi import APIRouter
|
|
from fastapi import Depends
|
|
from fastapi import HTTPException
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
import schemas
|
|
from schemas import ItemResponse
|
|
from services import crud
|
|
|
|
from app.db.session import SessionLocal
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
# GET ALL ITEMS
|
|
@router.get("/items", response_model=list[ItemResponse])
|
|
def get_items(db: Session = Depends(get_db)):
|
|
return crud.get_items(db)
|
|
|
|
|
|
# CREATE ITEM
|
|
@router.post("/items", response_model=ItemResponse)
|
|
def create_item(
|
|
item: schemas.ItemCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
return crud.create_item(db, item.title)
|
|
|
|
|
|
# DELETE ITEM
|
|
@router.delete("/items/{item_id}")
|
|
def delete_item(
|
|
item_id: int,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
item = crud.delete_item(db, item_id)
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return {"message": "Item deleted"} |