-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
70 lines (54 loc) · 2.41 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
64
65
66
67
68
69
70
from typing import List
from fastapi import FastAPI, Query, HTTPException
from fastapi.param_functions import Depends
from sqlalchemy.orm.session import Session
from sql_app import crud, models, schemas
from sql_app.database import SessionLocal, engine
models.Base.metadata.create_all(bind=engine) #creates all tables
app = FastAPI(
title="databricks-table-crud-fastapi",
description="This demo application demonstrates an implementation for basic CRUD operations on a databricks table via a REST Api.",
version="0.1.0"
)
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.get("/customer", tags=["customer"], response_model=List[schemas.Customer])
def get_customers(
country: models.Country = None,
car: models.Car = None,
active: bool = None,
age: int = None,
db: Session = Depends(get_db)
):
db_customers = crud.get_customers(db, country, car, active, age)
return db_customers
@app.get("/customer/{id}", tags=["customer"], response_model= schemas.Customer)
def get_customer(id: int, db: Session = Depends(get_db)):
db_customer = crud.get_customer(db, id)
return db_customer
@app.post("/customer", tags=["customer"], response_model= schemas.Customer)
def create_customer(customer: schemas.Customer, db: Session = Depends(get_db)):
db_customer = crud.get_customer(db, customer.Id)
if db_customer:
raise HTTPException(status_code=400, detail=f"A customer with the id '{customer.Id}' already exists.")
db_customer = crud.create_customer(db, customer)
return db_customer
@app.patch("/customer/{id}", tags=["customer"], response_model= schemas.Customer)
def update_customer(id: int, customer: schemas.UpdateCustomer, db: Session = Depends(get_db)):
db_customer = crud.get_customer(db, id)
if db_customer is None:
raise HTTPException(status_code=404, detail=f"A customer with the id '{id}' does not exist.")
db_customer = crud.update_customer(db, id, customer)
return db_customer
@app.delete("/customer/{id}", tags=["customer"], response_model= schemas.Customer)
def delete_customer(id: int, db: Session = Depends(get_db)):
db_customer = crud.get_customer(db, id)
if db_customer is None:
raise HTTPException(status_code=404, detail=f"A customer with the id '{id}' does not exist.")
db_customer = crud.delete_customer(db, id)
return db_customer