def update_customer(cid: int, customer: schemas.CustomerRequestUpdate, dbb: Session = Depends(get_db)): prev_customer = get_customer_by_id_if_exists(cid, dbb) customer_update = schemas.CustomerUpdate( customer_id=cid, phone_number=customer.phone_number if customer.phone_number else prev_customer.phone_number, address=customer.address if customer.address else prev_customer.address, ) return Crud.update_customer(dbb=dbb, customer=customer_update)
def update_drink( did: int, drink: schemas.DrinkRequestUpdate, dbb: Session = Depends(get_db), ): prev_drink = get_drink_by_id_if_exists(did, dbb) drink_update = schemas.DrinkUpdate( drink_id=did, name=drink.name if drink.name else prev_drink.name, price=drink.price if drink.price else prev_drink.price, ) return Crud.update_drink(dbb, drink_update)
def update_topping( tid: int, topping: schemas.ToppingRequestUpdate, dbb: Session = Depends(get_db), ): prev_top = get_topping_by_id_if_exists(tid, dbb) topping_update = schemas.ToppingUpdate( topping_id=tid, name=topping.name if topping.name else prev_top.name, price=topping.price if topping.price else prev_top.price, ) return Crud.update_topping(dbb, topping_update)
def update_pizza( pid: int, pizza: schemas.PizzaRequestUpdate, dbb: Session = Depends(get_db), ): prev_pizza = get_pizza_by_id_if_exists(pid, dbb) toppings = [] for tid in pizza.toppings: ftop = get_topping_by_id_if_exists(tid, dbb) toppings.append(ftop) pizza_update = schemas.PizzaUpdate( pizza_id=pid, name=pizza.name if pizza.name else prev_pizza.name, size=pizza.size if pizza.size else prev_pizza.size, base_price=pizza.base_price if pizza.base_price else prev_pizza.base_price, toppings=toppings if toppings else prev_pizza.toppings, ) return Crud.update_pizza(dbb, pizza_update)
def create_drink(drink: schemas.DrinkCreate, dbb: Session = Depends(get_db)): return Crud.create_drink(dbb, drink)
def read_drinks(dbb: Session = Depends(get_db)): return Crud.get_drinks(dbb=dbb)
def delete_pizza(pid: int, dbb: Session = Depends(get_db)): get_pizza_by_id_if_exists(pid, dbb) return Crud.delete_pizza(dbb, pid)
def create_topping(topping: schemas.ToppingCreate, dbb: Session = Depends(get_db)): return Crud.create_topping(dbb, topping)
def get_customer_by_id_if_exists(cid: int, dbb: Session): customer = Crud.get_customer_by_id(dbb, cid) if not customer: raise Exception(f"No such customer with {cid} found.") return customer
def get_drink_by_id_if_exists(did: int, dbb: Session): drink = Crud.get_drink_by_id(dbb, did) if not drink: raise Exception(f"No such drink with {did} found.") return drink
def remove_order_for_customer(cid: int, oid: int, dbb: Session = Depends(get_db)): get_customer_by_id_if_exists(cid, dbb) get_order_by_id_if_exists(oid, dbb) return Crud.remove_order_for_customer(dbb, cid, oid)
def add_order_to_customer(cid: int, oid: int, dbb: Session = Depends(get_db)): get_customer_by_id_if_exists(cid, dbb) get_order_by_id_if_exists(oid, dbb) return Crud.add_order_to_customer(dbb, cid, oid)
def read_customers(dbb: Session = Depends(get_db)): return Crud.get_customers(dbb=dbb)
def create_customer(customer: schemas.CustomerCreate, dbb: Session = Depends(get_db)): return Crud.create_customer(dbb, customer)
def delete_topping(tid: int, dbb: Session = Depends(get_db)): get_topping_by_id_if_exists(tid, dbb) return Crud.delete_topping(dbb, tid)
def delete_drink(did: int, dbb: Session = Depends(get_db)): get_drink_by_id_if_exists(did, dbb) return Crud.delete_drink(dbb, did)
def get_topping_by_id_if_exists(tid: int, dbb: Session): topping = Crud.get_topping_by_id(dbb, tid) if not topping: raise Exception(f"No such topping with {tid} found.") return topping
def read_pizzas(dbb: Session = Depends(get_db)): return Crud.get_pizzas(dbb=dbb)
def get_order_by_id_if_exists(oid: int, dbb: Session): order = Crud.get_order_by_id(dbb, oid) if not order: raise Exception(f"No such order with {oid} found.") return order
def create_pizza(pizza: schemas.PizzaCreate, dbb: Session = Depends(get_db)): return Crud.create_pizza(dbb, pizza)
def get_pizza_by_id_if_exists(pid: int, dbb: Session): pizza = Crud.get_pizza_by_id(dbb, pid) if not pizza: raise Exception(f"No such pizza with {pid} found.") return pizza
def read_toppings(dbb: Session = Depends(get_db)): return Crud.get_toppings(dbb=dbb)