summaryrefslogtreecommitdiff
path: root/api/routes/locations.py
blob: 744fa7f5a40770eff5cd46622ae96c913702c208 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from fastapi import APIRouter
from sqlalchemy import select
from ..database import SessionLocal
from ..models import Location

from pydantic import BaseModel

router = APIRouter(
    prefix='/locations',
    tags=['locations']
)

@router.get('/')
async def get_locations():
    dc = {'locations': []}
    session = SessionLocal()
    stmt = select(Location)
    locs = session.execute(stmt) # TODO: Page results
    for loc in locs.scalars():
        l = {
            'id': loc.id,
            'street': loc.street,
            'avenue': loc.avenue,
            'zip_code': loc.zip_code
        }
        dc['locations'].append(l)
    dc['size'] = len(dc['locations'])
    session.close()
    return dc
    

class PostLocation(BaseModel):
    street: str
    avenue: str
    zip_code:str

@router.post('/')
async def create_location(location: PostLocation):
    session = SessionLocal()
    session.begin()
    session.add(Location(street=location.street, avenue=location.avenue, zip_code=location.zip_code))
    session.commit()
    session.close()
    return {'msg': 'Localização adicionada com sucesso.'}