Spaces:
Running
Running
| #!/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() |