-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (48 loc) · 1.78 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
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import FastAPI, HTTPException, Depends
from sqlalchemy.orm import Session
from api_app.db_manager import DbManager
from api_app import crud, models, schemas
from datetime import datetime, date
dbManager = DbManager()
app = FastAPI()
shapes = [
{"item": "Circle", "number": 1, "id": 1},
{"item": "Triangle", "number": 3, "id": 2},
{"item": "Rectangle", "number": 4, "id": 3},
]
@app.get("/")
async def root():
return {"message": "Hello FastAPI"}
@app.get("/shapes")
async def get_shapes():
return shapes
@app.get("/shapes/{shape_id}")
async def get_shape_by_id(shape_id: int):
# return [shape for shape in shapes if shape["id"] == shape_id]
for shape in shapes:
if shape["id"] == shape_id:
return shape
raise HTTPException(status_code=404, detail=f"No shape with id {shape_id} found")
def get_db():
dbManager = DbManager()
db = dbManager.SessionLocal()
try:
yield db
finally:
db.close()
## Input Model = Request Body ['publisher'] == 'GreenWay , Output Model = Response Body == 'GreenWayBase'
@app.get("/greenway/records/{id}", response_model=schemas.GreenwayBase)
def read_greenway_record_by_id(id: int, db: Session = Depends(get_db)):
gw_data = crud.get_greenway_first_record(db, id)
if gw_data is None:
raise HTTPException(status_code=404, detail=f"Record id {id} not found.")
return gw_data
@app.get("/greenway/records/", response_model=list[schemas.GreenwayBase])
async def read_greenway_records_limit(
skip: int = 0, limit: int = 50, db: Session = Depends(get_db)
):
records = crud.get_greenway_items_with_limit(db, skip=skip, limit=limit)
return records
def read_greenway_by_time(
from_date: datetime = date(2016,3,1),
to_date: datetime = date(2023, 12, 1),):