130 lines
4.1 KiB
Python
130 lines
4.1 KiB
Python
from fastapi import Depends, FastAPI, HTTPException, Response, status
|
|
from logging import getLogger
|
|
from ormantic.exceptions import NoMatch
|
|
from pizzatool.model import Ingredient, Pizza, PizzaIngredient
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get('/ingredients')
|
|
async def read_ingredients():
|
|
return await Ingredient.objects.all()
|
|
|
|
|
|
@app.post('/ingredients', status_code=status.HTTP_201_CREATED)
|
|
async def create_ingredient(name: str, response: Response):
|
|
ingredient = await Ingredient.objects.create(name=name)
|
|
response.headers['Location'] = 'ingredients/%d' % ingredient.id
|
|
return ingredient
|
|
|
|
|
|
async def get_ingredient(ingredient_id: int):
|
|
try:
|
|
return await Ingredient.objects.get(id=ingredient_id)
|
|
except NoMatch:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
@app.get('/ingredients/{ingredient_id}')
|
|
async def read_ingredient(ingredient: Ingredient = Depends(get_ingredient)):
|
|
return ingredient
|
|
|
|
|
|
@app.put('/ingredients/{ingredient_id}',
|
|
status_code=status.HTTP_204_NO_CONTENT)
|
|
async def update_ingredient(
|
|
name: str,
|
|
ingredient: Ingredient = Depends(get_ingredient)):
|
|
await ingredient.update(name=name)
|
|
|
|
|
|
@app.delete('/ingredients/{ingredient_id}',
|
|
status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_ingredient(ingredient: Ingredient = Depends(get_ingredient)):
|
|
await ingredient.delete()
|
|
|
|
|
|
@app.get('/pizzas')
|
|
async def read_pizzas():
|
|
return await Pizza.objects.all()
|
|
|
|
|
|
@app.post('/pizzas', status_code=status.HTTP_201_CREATED)
|
|
async def create_pizza(name: str, response: Response):
|
|
pizza = await Pizza.objects.create(name=name)
|
|
response.headers['Location'] = 'pizzas/%d' % pizza.id
|
|
return pizza
|
|
|
|
|
|
async def get_pizza(pizza_id: int):
|
|
try:
|
|
return await Pizza.objects.get(id=pizza_id)
|
|
except NoMatch:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
@app.get('/pizzas/{pizza_id}')
|
|
async def read_pizza(pizza: Pizza = Depends(get_pizza)):
|
|
return pizza
|
|
|
|
|
|
@app.put('/pizzas/{pizza_id}', status_code=status.HTTP_204_NO_CONTENT)
|
|
async def update_pizza(name: str, pizza: Pizza = Depends(get_pizza)):
|
|
await pizza.update(name=name)
|
|
|
|
|
|
@app.delete('/pizzas/{pizza_id}', status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_pizza(pizza: Pizza = Depends(get_pizza)):
|
|
await pizza.delete()
|
|
|
|
|
|
@app.get('/pizzas/{pizza_id}/ingredients')
|
|
async def read_pizza_ingredients(pizza: Pizza = Depends(get_pizza)):
|
|
return await PizzaIngredient.objects.select_related('ingredient') \
|
|
.filter(pizza=pizza).all()
|
|
|
|
|
|
@app.post('/pizza/{pizza_id}/ingredients', status_code=status.HTTP_201_CREATED)
|
|
async def create_pizza_ingredient(
|
|
response: Response,
|
|
pizza: Pizza = Depends(get_pizza),
|
|
ingredient: Ingredient = Depends(get_ingredient)):
|
|
pizza_ingredient = await PizzaIngredient.objects.create(
|
|
pizza=pizza,
|
|
ingredient=ingredient)
|
|
response.headers['Location'] = 'ingredients/%d' % pizza_ingredient.id
|
|
return pizza_ingredient
|
|
|
|
|
|
async def get_pizza_ingredient(
|
|
pizza_ingredient_id: int,
|
|
pizza: Pizza = Depends(get_pizza)):
|
|
try:
|
|
return await PizzaIngredient.objects.select_related('ingredient') \
|
|
.get(id=pizza_ingredient_id, pizza=pizza)
|
|
except NoMatch:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
@app.get('/pizzas/{pizza_id}/ingredients/{pizza_ingredient_id}')
|
|
async def read_pizza_ingredient(
|
|
pizza_ingredient: PizzaIngredient = Depends(get_pizza_ingredient)):
|
|
return pizza_ingredient
|
|
|
|
|
|
@app.put('/pizzas/{pizza_id}/ingredients/{pizza_ingredient_id}',
|
|
status_code=status.HTTP_204_NO_CONTENT)
|
|
async def update_pizza_ingredient(
|
|
pizza_ingredient: PizzaIngredient = Depends(get_pizza_ingredient),
|
|
ingredient: Ingredient = Depends(get_ingredient)):
|
|
await pizza_ingredient.update(ingredient=ingredient.id)
|
|
|
|
|
|
@app.delete('/pizzas/{pizza_id}/ingredients/{pizza_ingredient_id}',
|
|
status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_pizza_ingredient(
|
|
pizza_ingredient: PizzaIngredient = Depends(get_pizza_ingredient)):
|
|
await pizza_ingredient.delete()
|