fastapi-boilerplate/app/api/endpoints/items.py

57 lines
1.1 KiB
Python
Raw Normal View History

from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
import crud
import models
from schemas.item import Item, ItemCreate, ItemUpdate
from api import deps
router = APIRouter()
@router.get("/{item_id}", response_model=Item)
async def get_item(
*,
db: Session = Depends(deps.get_db),
item_id: int
):
return crud.item.get(db, item_id)
@router.get("/", response_model=list[Item])
async def get_items(
skip: int = 0,
limit: int = 100,
db: Session = Depends(deps.get_db)
):
return crud.item.get_multi(db, skip=skip, limit=limit)
@router.post("/", response_model=Item)
async def create_item(
item: ItemCreate,
db: Session = Depends(deps.get_db)
):
return crud.item.create(db=db, item=item)
@router.put("/{item_id}", response_model=ItemUpdate)
async def update_item(
item: ItemUpdate,
db: Session = Depends(deps.get_db)
):
return crud.item.update(db=db, item=item)
@router.delete("/{item_id}")
async def delete_item(
*,
item: ItemUpdate,
db: Session = Depends(deps.get_db),
item_id: int
):
return crud.remove(item_id)