Ajit Panday commited on
Commit
62d9331
·
1 Parent(s): 974ed50

Fix response models in customer endpoints

Browse files
Files changed (2) hide show
  1. app/auth.py +12 -12
  2. main.py +12 -10
app/auth.py CHANGED
@@ -11,7 +11,7 @@ from .settings import (
11
  ADMIN_USERNAME, ADMIN_PASSWORD
12
  )
13
  from .database import get_db
14
- from .models import Customer
15
  from .schemas import Customer as CustomerSchema
16
 
17
  # Password hashing
@@ -23,12 +23,12 @@ api_key_header = APIKeyHeader(name="api-key")
23
 
24
  router = APIRouter()
25
 
26
- def verify_api_key(api_key: str = Security(api_key_header)) -> Customer:
27
  """Verify API key and return customer"""
28
  db = next(get_db())
29
- customer = db.query(Customer).filter(
30
- Customer.api_key == api_key,
31
- Customer.is_active == True
32
  ).first()
33
 
34
  if not customer:
@@ -109,7 +109,7 @@ async def create_customer(
109
  )
110
 
111
  # Check if email already exists
112
- existing_customer = db.query(Customer).filter(Customer.email == customer_data["email"]).first()
113
  if existing_customer:
114
  raise HTTPException(
115
  status_code=status.HTTP_400_BAD_REQUEST,
@@ -117,7 +117,7 @@ async def create_customer(
117
  )
118
 
119
  # Create new customer
120
- customer = Customer(
121
  name=customer_data["name"],
122
  company_name=customer_data["company_name"],
123
  email=customer_data["email"],
@@ -145,16 +145,16 @@ async def create_customer(
145
  detail=f"Error creating customer: {str(e)}"
146
  )
147
 
148
- @router.get("/customers/", response_model=List[Customer])
149
  async def list_customers(
150
  db: Session = Depends(get_db),
151
  current_admin: str = Depends(get_current_admin)
152
  ):
153
  """List all customers"""
154
  try:
155
- customers = db.query(Customer).all()
156
  return [
157
- Customer(
158
  id=customer.id,
159
  name=customer.name,
160
  company_name=customer.company_name,
@@ -179,7 +179,7 @@ async def get_customer(
179
  current_admin: str = Depends(get_current_admin)
180
  ):
181
  """Get customer details"""
182
- customer = db.query(Customer).filter(Customer.id == customer_id).first()
183
  if not customer:
184
  raise HTTPException(status_code=404, detail="Customer not found")
185
  return customer
@@ -191,7 +191,7 @@ async def delete_customer(
191
  current_admin: str = Depends(get_current_admin)
192
  ):
193
  """Delete a customer"""
194
- customer = db.query(Customer).filter(Customer.id == customer_id).first()
195
  if not customer:
196
  raise HTTPException(status_code=404, detail="Customer not found")
197
 
 
11
  ADMIN_USERNAME, ADMIN_PASSWORD
12
  )
13
  from .database import get_db
14
+ from .models import Customer as CustomerModel
15
  from .schemas import Customer as CustomerSchema
16
 
17
  # Password hashing
 
23
 
24
  router = APIRouter()
25
 
26
+ def verify_api_key(api_key: str = Security(api_key_header)) -> CustomerModel:
27
  """Verify API key and return customer"""
28
  db = next(get_db())
29
+ customer = db.query(CustomerModel).filter(
30
+ CustomerModel.api_key == api_key,
31
+ CustomerModel.is_active == True
32
  ).first()
33
 
34
  if not customer:
 
109
  )
110
 
111
  # Check if email already exists
112
+ existing_customer = db.query(CustomerModel).filter(CustomerModel.email == customer_data["email"]).first()
113
  if existing_customer:
114
  raise HTTPException(
115
  status_code=status.HTTP_400_BAD_REQUEST,
 
117
  )
118
 
119
  # Create new customer
120
+ customer = CustomerModel(
121
  name=customer_data["name"],
122
  company_name=customer_data["company_name"],
123
  email=customer_data["email"],
 
145
  detail=f"Error creating customer: {str(e)}"
146
  )
147
 
148
+ @router.get("/customers/", response_model=List[CustomerSchema])
149
  async def list_customers(
150
  db: Session = Depends(get_db),
151
  current_admin: str = Depends(get_current_admin)
152
  ):
153
  """List all customers"""
154
  try:
155
+ customers = db.query(CustomerModel).all()
156
  return [
157
+ CustomerSchema(
158
  id=customer.id,
159
  name=customer.name,
160
  company_name=customer.company_name,
 
179
  current_admin: str = Depends(get_current_admin)
180
  ):
181
  """Get customer details"""
182
+ customer = db.query(CustomerModel).filter(CustomerModel.id == customer_id).first()
183
  if not customer:
184
  raise HTTPException(status_code=404, detail="Customer not found")
185
  return customer
 
191
  current_admin: str = Depends(get_current_admin)
192
  ):
193
  """Delete a customer"""
194
+ customer = db.query(CustomerModel).filter(CustomerModel.id == customer_id).first()
195
  if not customer:
196
  raise HTTPException(status_code=404, detail="Customer not found")
197
 
main.py CHANGED
@@ -360,16 +360,18 @@ async def list_customers(
360
  """List all customers"""
361
  try:
362
  customers = db.query(CustomerModel).all()
363
- return [Customer(
364
- id=c.id,
365
- name=c.name,
366
- company_name=c.company_name,
367
- email=c.email,
368
- api_key=c.api_key,
369
- is_active=c.is_active,
370
- created_at=c.created_at,
371
- updated_at=c.updated_at
372
- ) for c in customers]
 
 
373
  except Exception as e:
374
  raise HTTPException(
375
  status_code=500,
 
360
  """List all customers"""
361
  try:
362
  customers = db.query(CustomerModel).all()
363
+ return [
364
+ Customer(
365
+ id=customer.id,
366
+ name=customer.name,
367
+ company_name=customer.company_name,
368
+ email=customer.email,
369
+ api_key=customer.api_key,
370
+ is_active=customer.is_active,
371
+ created_at=customer.created_at,
372
+ updated_at=customer.updated_at
373
+ ) for customer in customers
374
+ ]
375
  except Exception as e:
376
  raise HTTPException(
377
  status_code=500,