Skip to content

Commit

Permalink
Add one more route in products
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Gopathi <[email protected]>
  • Loading branch information
RahulGopathi committed Sep 16, 2023
1 parent 605d273 commit ede8573
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
12 changes: 8 additions & 4 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from fastapi import FastAPI, HTTPException
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from api.routers import product_family, services, products, prices
from api.utils.customHTTPException import CustomHTTPException

# Create a FastAPI instance
api = FastAPI()
Expand All @@ -13,6 +14,9 @@


# Custom exception handler to return a 404 response
@api.exception_handler(HTTPException)
async def not_found_exception_handler(request, exc):
return JSONResponse(content={"detail": "Not Found"}, status_code=404)
@api.exception_handler(CustomHTTPException)
async def custom_exception_handler(request, exc):
if exc.status_code and exc.detail:
return JSONResponse(content={"detail": exc.detail}, status_code=exc.status_code)
else:
return JSONResponse(content={"detail": "Not Found"}, status_code=404)
6 changes: 4 additions & 2 deletions api/routers/prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ async def get_prices_for_service(service_code: str, skip: int = Query(0, ge=0),
# Query the Product table to retrieve prices for the specified service
with SessionLocal() as db:
query = db.query(Product.id.label("product_id"), Product.sku.label("sku"), Product.location.label("location"),
Product.region_code.label("region_code"), Price.pricePerUnit.label("price"), Price.unit.label("unit"))\
Product.region_code.label("region_code"), Price.pricePerUnit.label("price"),
Price.unit.label("unit"), Price.description.label("description"))\
.join(Price, Price.product_id == Product.id)\
.filter(Product.service_code == service_code)\
.offset(skip)\
Expand All @@ -21,6 +22,7 @@ async def get_prices_for_service(service_code: str, skip: int = Query(0, ge=0),

# Convert the query results to a list of dictionaries
prices = [{"product_id": row.product_id, "sku": row.sku, "location": row.location,
"region_code": row.region_code, "price": row.price, "unit": row.unit} for row in query]
"region_code": row.region_code, "price": row.price, "unit": row.unit,
"price_description": row.description} for row in query]

return prices
48 changes: 47 additions & 1 deletion api/routers/products.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from fastapi import APIRouter, Query
from api.database import SessionLocal
from api.models import Product
from api.models import Product, Price
from api.utils.customHTTPException import CustomHTTPException

router = APIRouter()

Expand All @@ -12,3 +13,48 @@ async def get_products(service_code: str, skip: int = Query(0, ge=0), limit: int
with SessionLocal() as db:
products = db.query(Product).filter(Product.service_code == service_code).offset(skip).limit(limit).all()
return products


# Route to get all products of a particular product attribute value
@router.get("/products/")
def get_products_by_attribute(
attribute_name: str = Query(default="", description="Name of the product attribute"),
attribute_value: str = Query(default="", description="Value of the product attribute"),
include_prices: bool = Query(default=False, description="Include prices in the response"),
):
if not attribute_name or not attribute_value:
raise CustomHTTPException(status_code=400, detail="Missing query parameters")
else:
if include_prices:
with SessionLocal() as db:

product_prices = db.query(Product.id.label("product_id"), Product.product_family_id.label("product_family_id"),
Product.service_code.label("service_code"), Product.sku.label("sku"),
Product.location.label("location"), Product.region_code.label("region_code"),
Price.pricePerUnit.label("price"), Price.unit.label("unit"),
Price.description.label("description")).\
join(Price, Product.id == Price.product_id).\
filter(Product.product_attributes[attribute_name] == attribute_value).all()

# Check if any products match the condition
if not product_prices:
raise CustomHTTPException(status_code=404, detail="No products found with the specified condition")

product_prices_result = [{"product_id": row.product_id, "product_family_id": row.product_family_id,
"sku": row.sku, "location": row.location, "service_code": row.service_code,
"region_code": row.region_code, "price": row.price, "unit": row.unit,
"price_description": row.description} for row in product_prices]

return product_prices_result
else:
with SessionLocal() as db:

products = db.query(Product).filter(Product.product_attributes[attribute_name] == attribute_value).all()

# Check if any products match the condition
if not products:
raise CustomHTTPException(status_code=404, detail="No products found with the specified condition")

product_list = [product.__dict__ for product in products]

return product_list
Empty file added api/utils/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions api/utils/customHTTPException.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from fastapi import HTTPException


class CustomHTTPException(HTTPException):
def __init__(self, status_code: int, detail: str):
super().__init__(status_code=status_code, detail=detail)
8 changes: 4 additions & 4 deletions load-data/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# Function to create the database and tables
def create_database_and_tables(cursor):
# Create the 'my_database' database if it doesn't exist
# Create the 'aws_database' database if it doesn't exist
cursor.execute("CREATE DATABASE IF NOT EXISTS aws_database")
cursor.execute("USE aws_database")

Expand Down Expand Up @@ -98,12 +98,12 @@ def process_offer(offer_name, offer_details, cursor):
product_family_id = cursor.lastrowid
product_family_ids[product_family_name] = product_family_id

# Example: Insert data into the 'product' table
# Insert data into the 'product' table
cursor.execute("""
INSERT INTO product (product_family_id, sku, service_code, location, region_code, product_attributes)
VALUES (%s, %s, %s, %s, %s, %s)
""", (
product_family_id, # Use product family ID
product_family_id,
product_details.get('sku', ''),
product_details.get('attributes', {}).get('servicecode', ''),
product_details.get('attributes', {}).get('location', product_details.get('attributes', {}).get('fromLocation', '')),
Expand All @@ -113,7 +113,7 @@ def process_offer(offer_name, offer_details, cursor):

last_added_product_id = cursor.lastrowid

# Example: Insert data into the 'price' table
# Insert data into the 'price' table
term_details = offer_data.get('terms', {}).get('OnDemand', {}).get(product_sku, {})
price_dimensions = term_details.get(list(term_details.keys())[0], {}).get('priceDimensions', {})
price_info = price_dimensions.get(list(price_dimensions.keys())[0], {})
Expand Down

0 comments on commit ede8573

Please sign in to comment.