aquabarrier / demo_barrier_insertion_api.py
rajeshbms's picture
Add validation report for AquaBarrier project implementation
d5f727d
#!/usr/bin/env python3
"""
Demo script showing how to use the barrier size insertion functionality
that implements the SQL INSERT approach as requested.
"""
import requests
import json
# API endpoint
BASE_URL = "http://localhost:8000" # Adjust as needed
ENDPOINT = f"{BASE_URL}/bidders/barrier-sizes-sql"
def demo_barrier_insertion():
"""
Demonstrate the barrier size insertion that mimics SQL INSERT statements:
INSERT INTO BarrierSizes (height,width,length,cableunits,price,isstandard) VALUES (...)
INSERT INTO BiddersBarrierSizes (id,inventoryid,bidderid,barriersizeid,isstandard) VALUES (...)
"""
print("πŸš€ Demonstrating SQL-like barrier size insertion")
print("=" * 60)
# Test data
test_data = {
"height": 6.0,
"width": 4.0,
"length": 12.0,
"cable_units": 10,
"price": 1500.00,
"is_standard": True,
"inventory_id": 12345,
"bidder_id": 1
}
print("πŸ“Š Test Data:")
for key, value in test_data.items():
print(f" {key}: {value}")
print(f"\n🌐 Making POST request to: {ENDPOINT}")
try:
# Make the API request
response = requests.post(
ENDPOINT,
params=test_data, # Using query parameters as defined in endpoint
timeout=30
)
print(f"πŸ“ˆ Response Status: {response.status_code}")
if response.status_code == 201:
result = response.json()
print("βœ… SUCCESS! Barrier size and association created:")
print(f" Barrier Size ID: {result.get('barrier_size_id')} (auto-generated)")
print(f" Bidder-Barrier Association ID: {result.get('bidder_barrier_size_id')} (auto-generated)")
print(f" Message: {result.get('message')}")
print("\n🎯 This demonstrates the implementation:")
print(" 1. INSERT INTO BarrierSizes - ID auto-incremented")
print(" 2. INSERT INTO BiddersBarrierSizes - uses generated BarrierSizeId")
else:
print(f"❌ Error: {response.status_code}")
try:
error_detail = response.json()
print(f" Details: {json.dumps(error_detail, indent=2)}")
except:
print(f" Response: {response.text}")
except requests.exceptions.ConnectionError:
print("❌ Connection Error: Could not connect to the API server")
print(" Make sure the FastAPI server is running on the correct port")
print(" Try: uvicorn app.app:app --reload --port 8000")
except Exception as e:
print(f"❌ Unexpected error: {e}")
def show_usage_instructions():
"""Show how to use the functionality in code"""
print("\nπŸ“‹ Code Usage Instructions")
print("=" * 60)
print("1. Direct Repository Usage:")
print("""
from app.db.repositories.bidders_barrier_sizes_repo import BiddersBarrierSizesRepository
from app.db.session import SessionLocal
db = SessionLocal()
repo = BiddersBarrierSizesRepository(db)
# Create barrier size and association
barrier_size_id, association_id = repo.create_barrier_size_with_association(
height=6.0,
width=4.0,
length=12.0,
cable_units=10,
price=1500.00,
is_standard=True,
inventory_id=12345,
bidder_id=1
)
print(f"Created BarrierSize ID: {barrier_size_id}")
print(f"Created Association ID: {association_id}")
""")
print("2. Service Layer Usage:")
print("""
from app.services.barrier_size_service import create_barrier_with_association
from app.db.session import SessionLocal
db = SessionLocal()
result = create_barrier_with_association(
db=db,
height=6.0,
width=4.0,
length=12.0,
cable_units=10,
price=1500.00,
is_standard=True,
inventory_id=12345,
bidder_id=1
)
print(result) # Returns dict with IDs and message
""")
print("3. API Usage (curl):")
print(f"""
curl -X POST "{ENDPOINT}" \\
-G \\
-d "height=6.0" \\
-d "width=4.0" \\
-d "length=12.0" \\
-d "cable_units=10" \\
-d "price=1500.00" \\
-d "is_standard=true" \\
-d "inventory_id=12345" \\
-d "bidder_id=1"
""")
if __name__ == "__main__":
demo_barrier_insertion()
show_usage_instructions()