-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
77 lines (59 loc) · 2.67 KB
/
views.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
71
72
73
74
75
76
77
from typing import List
from fastapi import APIRouter, Depends, HTTPException
from pydantic import PositiveInt, ValidationError
from sqlalchemy.orm import Session
from fastapi.responses import Response
import crud
import schemas
import models
from database import get_db
router = APIRouter()
def check_for_supplier(db, supp_id):
is_supp = crud.get_supplier(db, supp_id)
if is_supp is None:
raise HTTPException(status_code=404, detail="Not Okie Dokie ID")
return is_supp
@router.get("/shippers/{shipper_id}", response_model=schemas.Shipper)
async def get_shipper(shipper_id: PositiveInt, db: Session = Depends(get_db)):
db_shipper = crud.get_shipper(db, shipper_id)
if db_shipper is None:
raise HTTPException(status_code=404, detail="Shipper not found")
return db_shipper
@router.get("/shippers", response_model=List[schemas.Shipper])
async def get_shippers(db: Session = Depends(get_db)):
return crud.get_shippers(db)
@router.get("/suppliers", response_model=List[schemas.Suppliers])
async def get_supplier(db: Session = Depends(get_db)):
return crud.get_suppliers(db)
@router.get("/suppliers/{supplier_id}", response_model=schemas.Supplier)
async def get_supplier(supplier_id: PositiveInt, db: Session = Depends(get_db)):
db_supplier = check_for_supplier(db, supplier_id)
return db_supplier
@router.get("/suppliers/{id}/products")
async def get_sorted_supplier(pid: PositiveInt, db: Session = Depends(get_db)):
db_products = crud.get_products(db, pid)
if db_products:
data = [{"ProductID": prod.ProductID,
"ProductName": prod.ProductName,
"Category": {
"CategoryID": prod.CategoryID,
"CategoryName": prod.CategoryName,
},
"Discontinued": prod.Discontinued}
for prod in db_products]
return data
else:
raise HTTPException(status_code=404, detail="Not Oki Doki ID!")
@router.post("/suppliers", response_model=schemas.Supplier, status_code=201)
async def add_supplier(supp: schemas.AddSupplier, db: Session = Depends(get_db)):
return crud.add_supplier(db, supp)
@router.put("/suppliers/{sup_id}", response_model= schemas.AddSupplier, status_code=200)
async def update_supplier(sup_id: int, supp: schemas.Supplier, db: Session = Depends(get_db)):
db_supplier = check_for_supplier(db, sup_id)
crud.upd_supp(db, supp, sup_id)
return crud.get_supplier(db, sup_id)
@router.delete("/suppliers/{sup_id}")
async def del_supplier(sup_id: int, db: Session = Depends(get_db)):
db_supplier = check_for_supplier(db, sup_id)
crud.del_supp(db, sup_id)
return Response(status_code=204)