fastapi-boilerplate/app/api/endpoints/items.py
Finn Christiansen d8607212da
Some checks failed
Python formatting PEP8 / Python-PEP8 (push) Failing after 18s
added structured api endpoints and done some refactoring
2023-08-12 00:20:51 +02:00

56 lines
1.1 KiB
Python

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)