-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
50 lines (41 loc) · 1.4 KB
/
main.py
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
46
47
48
49
50
from db.record_db import RecordInDB
from db.record_db import save_record, get_record, get_records, delete_record
from models.record_models import RecordIn, RecordOut
import datetime
from fastapi import FastAPI
from fastapi import HTTPException
api = FastAPI()
from fastapi.middleware.cors import CORSMiddleware
api.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@api.post("/record")
async def create_record(record_in: RecordIn):
record_in_db = RecordInDB(**record_in.dict())
record_in_db = save_record(record_in_db)
record_out = RecordOut(**record_in_db.dict())
return record_out
@api.get("/record/{id}")
async def get_balance(id: int):
record_in_db = get_record(id)
if record_in_db == None:
raise HTTPException(status_code=404, detail="El registro no existe")
record_out = RecordOut(**record_in_db.dict())
record_out.fecha_registro = record_in_db.fecha_registro.strftime("%Y-%m-%d")
return record_out
@api.get("/records")
async def get_all_records():
results = []
records = get_records()
for record in records.values():
result = RecordOut(**record.dict())
result.fecha_registro = record.fecha_registro.strftime("%Y-%m-%d")
results.append(result)
return results
@api.delete("/record/{id}")
async def delete_a_record(id: int):
get_record(id)
return delete_record(id)