-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathapi.py
More file actions
27 lines (21 loc) · 805 Bytes
/
api.py
File metadata and controls
27 lines (21 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import Optional, List
from fastapi import FastAPI, Response, status
from beerlog.core import get_beers_from_database
from beerlog.serializers import BeerIn, BeerOut
from beerlog.models import Beer
from beerlog.database import get_session
api = FastAPI(title="beerlog")
@api.get("/beers", response_model=List[BeerOut])
async def list_beers(style: Optional[str] = None):
"""Lists beers from the database"""
beers = get_beers_from_database(style)
return beers
@api.post("/beers", response_model=BeerOut)
async def add_beer(beer_in: BeerIn, response: Response):
beer = Beer(**beer_in.dict())
with get_session() as session:
session.add(beer)
session.commit()
session.refresh(beer)
response.status_code = status.HTTP_201_CREATED
return beer