Implement CRUD operations for luggage items: add, update, delete, and fetch functionalities; enhance logging and error handling
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
import logging
|
||||
import db
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
#Logging
|
||||
|
||||
logging.basicConfig(
|
||||
filename='api.log', # Log to a file
|
||||
level=logging.DEBUG, # Log everything (DEBUG and above)
|
||||
format='%(asctime)s %(levelname)s %(message)s'
|
||||
)
|
||||
|
||||
logging.info('Application started')
|
||||
try:
|
||||
logging.debug('Debug info here')
|
||||
except Exception as e:
|
||||
logging.exception('An error occurred')
|
||||
|
||||
router = APIRouter(prefix="/query", tags=["query"])
|
||||
|
||||
@router.get("/help", status_code=200)
|
||||
def get_help():
|
||||
return {"name of clothes": "example"}
|
||||
|
||||
@router.get("/{clothe_name}/weight", status_code=206)
|
||||
def get_weight(clothe_name: str):
|
||||
name = clothe_name.upper()
|
||||
if db.check_clothe(name) != None:
|
||||
weight = db.fetch_weight(name)
|
||||
return {"weight": weight}
|
||||
else:
|
||||
logging.error(f"Item {name} not found")
|
||||
raise HTTPException(status_code=404, detail=f"Item {name} not found")
|
||||
|
||||
@router.get("/weight", status_code=200)
|
||||
def get_luggage_weight():
|
||||
weight = db.fetch_luggage_weight()
|
||||
return {"weight": weight}
|
||||
|
||||
@router.get("/{clothe_name}/pieces",status_code=206)
|
||||
def get_pieces(clothe_name: str):
|
||||
name = clothe_name.upper()
|
||||
if db.check_clothe(name) != None:
|
||||
pieces = db.fetch_piece(name)
|
||||
return {"pieces": pieces}
|
||||
else:
|
||||
logging.error(f"Item {name} not found")
|
||||
raise HTTPException(status_code=404, detail=f"Item {name} not found")
|
||||
|
||||
@router.get("/pieces", status_code=200)
|
||||
def get_all_pieces():
|
||||
pieces = db.fetch_all_pieces()
|
||||
return {"pieces": pieces}
|
||||
|
||||
@router.get("/clothes")
|
||||
def get_clothes():
|
||||
clothes = db.fetch_clothes()
|
||||
return {"clothes": clothes}
|
||||
|
||||
@router.get("/{clothe_name}/all", status_code=102)
|
||||
def get_all(clothe_name: str):
|
||||
name = clothe_name.upper()
|
||||
if db.check_clothe(name) != None:
|
||||
weight = db.fetch_weight(name)
|
||||
pieces = db.fetch_piece(name)
|
||||
return{
|
||||
"clothe": name,
|
||||
"weight": weight,
|
||||
"pieces": pieces
|
||||
}
|
||||
else:
|
||||
logging.error(f"Item {name} not found")
|
||||
raise HTTPException(status_code=404, detail=f"Item {name} not found")
|
||||
Reference in New Issue
Block a user