48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
|
|
"""
|
||
|
|
Customers Router
|
||
|
|
API endpoints for customer management
|
||
|
|
"""
|
||
|
|
|
||
|
|
from fastapi import APIRouter, HTTPException
|
||
|
|
from typing import List
|
||
|
|
|
||
|
|
from app.models.schemas import Customer, CustomerCreate
|
||
|
|
from app.core.database import execute_query
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/customers", response_model=List[Customer])
|
||
|
|
async def list_customers():
|
||
|
|
"""List all customers"""
|
||
|
|
query = "SELECT * FROM customers ORDER BY created_at DESC"
|
||
|
|
customers = execute_query(query)
|
||
|
|
return customers
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/customers/{customer_id}", response_model=Customer)
|
||
|
|
async def get_customer(customer_id: int):
|
||
|
|
"""Get a specific customer"""
|
||
|
|
query = "SELECT * FROM customers WHERE id = %s"
|
||
|
|
customers = execute_query(query, (customer_id,))
|
||
|
|
|
||
|
|
if not customers:
|
||
|
|
raise HTTPException(status_code=404, detail="Customer not found")
|
||
|
|
|
||
|
|
return customers[0]
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/customers", response_model=Customer)
|
||
|
|
async def create_customer(customer: CustomerCreate):
|
||
|
|
"""Create a new customer"""
|
||
|
|
query = """
|
||
|
|
INSERT INTO customers (name, email, phone, address)
|
||
|
|
VALUES (%s, %s, %s, %s)
|
||
|
|
RETURNING *
|
||
|
|
"""
|
||
|
|
result = execute_query(
|
||
|
|
query,
|
||
|
|
(customer.name, customer.email, customer.phone, customer.address)
|
||
|
|
)
|
||
|
|
return result[0]
|