-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (35 loc) · 1.89 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 fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
import crud
from config import settings
from database import get_db
from schemas import EcoPartProject, Sample, SampleDetails
app = FastAPI(title=settings.PROJECT_NAME)
# Set all CORS enabled origins
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin)
for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/ecopart_projects/", response_model=list[EcoPartProject])
def get_ecopart_projects(pattern: str = None, skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
if pattern:
return crud.get_ecopart_projects_name_contains(db, pattern, skip=skip, limit=limit)
return crud.get_ecopart_projects(db, skip=skip, limit=limit)
@app.get("/ecopart_projects/{project_id}", response_model=EcoPartProject)
def get_ecopart_project(project_id: int, db: Session = Depends(get_db)):
return crud.get_ecopart_project(db, project_id)
@app.get("/ecopart_projects/{project_id}/samples", response_model=list[Sample])
def get_ecopart_project_samples(project_id: int, db: Session = Depends(get_db)):
return crud.get_ecopart_project(db, project_id).samples
@app.get("/samples/{sample_id}", response_model=SampleDetails)
def get_sample(sample_id: int, db: Session = Depends(get_db)):
return crud.get_sample(db, sample_id)
@app.get("/samples/{north_east_lat}/{north_east_lng}/{south_west_lat}/{south_west_lng}", response_model=list[Sample])
def get_samples_from_box(north_east_lat: float, north_east_lng: float, south_west_lat: float, south_west_lng: float, db: Session = Depends(get_db)):
return crud.get_samples_from_box(db, north_east_lat, north_east_lng, south_west_lat, south_west_lng)