summaryrefslogtreecommitdiff
path: root/api/routes/measures.py
blob: b435ae342dd71b8c026d2def32d84834be0fad0f (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
from fastapi import APIRouter
from sqlalchemy import select
from ..database import SessionLocal
from ..models import Measure
from pydantic import BaseModel

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

@router.get('/')
async def get_measures():
    dc = {'measures': []}
    session = SessionLocal()
    stmt = select(Measure)
    measures = session.execute(stmt)
    for measure in measures.scalars():
        mem = {
            'id': measure.id,
            'name': measure.name,
            'unit_code': measure.unit_code
        }
        dc['measures'].append(mem)
    dc['size'] = len(dc['measures'])
    session.close()
    return dc

class PostMeasure(BaseModel):
    name: str
    unit_code: str

@router.post('/')
async def post_measure(measure: PostMeasure):
    session = SessionLocal()
    session.begin()
    session.add(Measure(name=measure.name, unit_code=measure.unit_code))
    session.commit()
    session.close()
    return {'msg': 'Unidade adicionada com sucesso.'}