Update code in endpoints/customer.post.py
This commit is contained in:
parent
03c73bfd0d
commit
5c95eb9e63
@ -1,36 +1,69 @@
|
|||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
import uuid
|
from sqlalchemy import create_engine, Column, String, Integer
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
customers = [] # In-memory storage
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
import os
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
# Database configuration
|
||||||
|
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://user:password@localhost:5432/dbname")
|
||||||
|
engine = create_engine(DATABASE_URL)
|
||||||
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
class Customer(Base):
|
||||||
|
__tablename__ = "customers"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String, index=True)
|
||||||
|
email = Column(String, unique=True, index=True)
|
||||||
|
phone = Column(String)
|
||||||
|
address = Column(String)
|
||||||
|
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
@router.post("/customer")
|
@router.post("/customer")
|
||||||
async def create_customer(
|
async def create_customer(
|
||||||
email: str = "customer@example.com",
|
name: str,
|
||||||
name: str = "John Doe",
|
email: str,
|
||||||
location: str = "New York",
|
phone: str,
|
||||||
gender: str = "male"
|
address: str
|
||||||
):
|
):
|
||||||
"""Create new customer endpoint"""
|
"""Create new customer endpoint"""
|
||||||
if any(c["email"] == email for c in customers):
|
db = SessionLocal()
|
||||||
raise HTTPException(status_code=400, detail="Email already exists")
|
|
||||||
|
# Check if email already exists
|
||||||
customer_id = str(uuid.uuid4())
|
if db.query(Customer).filter(Customer.email == email).first():
|
||||||
customers.append({
|
raise HTTPException(status_code=400, detail="Email already registered")
|
||||||
"id": customer_id,
|
|
||||||
"email": email,
|
# Create new customer
|
||||||
"name": name,
|
customer = Customer(
|
||||||
"location": location,
|
name=name,
|
||||||
"gender": gender
|
email=email,
|
||||||
})
|
phone=phone,
|
||||||
|
address=address
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db.add(customer)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(customer)
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=400, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"message": "Customer created successfully",
|
"message": "Customer created successfully",
|
||||||
"customer_id": customer_id,
|
"customer_id": customer.id,
|
||||||
"email": email,
|
"name": customer.name,
|
||||||
|
"method": "POST",
|
||||||
|
"_verb": "post",
|
||||||
"next_steps": [
|
"next_steps": [
|
||||||
"Complete customer profile",
|
"Customer verification",
|
||||||
"Add payment method"
|
"Complete profile"
|
||||||
]
|
]
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user