from typing import Optional, List
from fastapi import Depends, FastAPI, HTTPException, Query
from sqlalchemy.orm import Session
import uvicorn
import crud as crud
import models as models
import schemas as schemas
from database import SessionLocal, engine
try:
models.Base.metadata.create_all(bind=engine)
except Exception as e:
print(f"Error creating database tables: {e}")
raise
app = FastAPI()
# Dependency session
# function will stop where it will see yield
# Finally will call after sending the data in response
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
"""
Food APIs are below here:
"""
# Show all food of food menu
@app.get("/food_menu/", tags=["Food Menu"])
def get_food(db: Session = Depends(get_db)):
return crud.get_food(db=db)
# Show food by selected category
@app.get("/food_menu_by_category/", tags=["Food Menu"])
def get_food_category(category: List[str] = Query(None), db: Session =
Depends(get_db)):
result = []
for each_category in category:
db_category = crud.get_food_by_category(db=db,
category=each_category)
result = result + db_category
return result
# Add new food item
@app.post("/food_menu/", tags=["Food Menu"])
def creat_food(new_food: schemas.Food_data, db: Session =
Depends(get_db)):
return crud.create_food(db=db, new_food=new_food)
# Update food details
@app.put("/food_menu/{food_id}/", tags=["Food Menu"])
def update_food(food_id: int, food: schemas.Food_data, db: Session =
Depends(get_db)):
return crud.update_food(db=db, food=food, food_id=food_id)
# Delete food
@app.delete("/food_menu/{food_id}/", tags=["Food Menu"])
def delete_food(food_id: int, db: Session = Depends(get_db)):
return crud.delete_food(db=db, food_id=food_id)
"""
Customer APIs are below here:
"""
# Show all Customers
@app.get("/customer/", tags=["Customer"])
def get_customer(db: Session = Depends(get_db)):
return crud.get_customer(db=db)
# Add new Customer
@app.post("/customer/", tags=["Customer"])
def creat_customer(new_customer: str, ref_code: Optional[str] = "0", db:
Session = Depends(get_db)):
return crud.create_customer(db=db, new_customer=new_customer,
ref_code=ref_code)
# Delete Customer
@app.delete("/customer/", tags=["Customer"])
def delete_customer(customer_id: int, db: Session = Depends(get_db)):
return crud.delete_customer(db=db, customer_id=customer_id)
# Customer relationship
@app.get("/customer_relation/", tags=["Customer"])
def get_customer_relation(db: Session = Depends(get_db)):
return crud.customer_relation(db=db)
# Manual pagination
# Page size = number of result per page
# Page number = which number page you want as a result
@app.get("/custome_pagination", tags=["Customer"])
def customer_pagination(page_number: int, page_size: int, db: Session =
Depends(get_db)):
return crud.customer_pagination(db=db, page_number=page_number,
page_size=page_size)
"""
Food order APIs are below here:
"""
# Show all Orders
@app.get("/order/", tags=["Order"])
def show_order(db: Session = Depends(get_db)):
return crud.show_order(db=db)
# Create new Order
# q = quantity
@app.post("/order/", tags=["Order"])
def creat_order(food_id: int, customer_id: int, q: Optional[int] = 1, db:
Session = Depends(get_db)):
return crud.create_order(db=db, customer_id=customer_id,
food_id=food_id, q=q)
# Update Order status
@app.post("/order/update/{order_id}", tags=["Order"])
def order_update(order_id: int, request: schemas.UpdateOrderRequest, db:
Session = Depends(get_db)):
return crud.order_update(db=db, order_id=order_id,
update_status=request.update_status)
# Delete Order
@app.delete("/order/", tags=["Order"])
def delete_order(order_id: int, db: Session = Depends(get_db)):
return crud.delete_order(db=db, order_id=order_id)
# Add feedback
@app.post("/order_feedback/", tags=["Order"])
def feedback_add(feedback_content: schemas.Feedback_data, db: Session =
Depends(get_db)):
return crud.feedback_add(db=db, feedback_content=feedback_content)
# Delete feedback
@app.delete("/order_feedback/", tags=["Order"])
def delete_feedback(feedback_id: int, db: Session = Depends(get_db)):
return crud.delete_feedback(db=db, feedback_id=feedback_id)
"""
Bill APIs are below here:
"""
# Get bill
@app.get("/bill/{customer_id}/", tags=["Bill"])
def fetch_bill(customer_id: int, coupon_code: Optional[str] = None, db:
Session = Depends(get_db)):
return crud.fetch_bill(db=db, customer_id=customer_id,
coupon_code=coupon_code)
"""
Table APIs are below here:
"""
# Show all Tables
@app.get("/tables/", tags=["Table"])
def get_table(db: Session = Depends(get_db)):
return crud.get_table(db=db)
# Add new Table
@app.post("/tables/", tags=["Table"])
def creat_table(new_table: schemas.Add_table, db: Session =
Depends(get_db)):
return crud.create_table(db=db, new_table=new_table)
"""
Reservation APIs are below here:
"""
# Show Reservation
@app.get("/table_reservation/", tags=["Table Reservation"])
def get_reservation(db: Session = Depends(get_db)):
return crud.get_reservation(db=db)
# Check available table
@app.post("/check_table/", tags=["Table Reservation"])
def check_table(check_available: schemas.Check_reservation, person: int,
db: Session = Depends(get_db)):
return crud.check_table(db=db, check_available=check_available,
person=person)
# Add Reservation
@app.post("/table_reservation/", tags=["Table Reservation"])
def create_reservation(reservation_data: schemas.Do_reservation, db:
Session = Depends(get_db)):
return crud.create_reservation(reservation_data=reservation_data,
db=db)
# Delete Reservation
# Add reservation if any customer is waiting for same table for same slot
& date.
@app.delete("/table_reservation/", tags=["Table Reservation"])
def delete_reservation(r_id: int, db: Session = Depends(get_db)):
return crud.delete_reservation(db=db, r_id=r_id)
"""
Waiting APIs are below here:
"""
# Add Waiting if table is already reserved.
@app.post("/waiting/", tags=["Waiting"])
def create_waiting(waiting_data: schemas.Waiting_data, db: Session =
Depends(get_db)):
return crud.create_waiting(waiting_data=waiting_data, db=db)
if __name__ == "__main__":
uvicorn.run("main:app")