/ justapi / demo_shop / shop / customers.py
swadhinbiswas fix: remove highlight.js CSS import causing Docker build failure · 7/20/2026
7ffe1c0
64 lines · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""shop.customers — customer registration and lookup."""
from __future__ import annotations

from justapi import HTTPException

from .config import app, db
from .db import q, q1, paginate
from .schemas import CustomerIn, validate


@app.get("/customers")
def list_customers(request):
    qp = request.query_params
    q = qp.get("q")
    state = qp.get("state")
    sort = qp.get("sort", "id")  # id | city | state
    order = qp.get("order", "asc")

    wheres, params = [], []
    if q:
        wheres.append("(c.id LIKE ? OR c.email LIKE ? OR c.city LIKE ?)")
        params += [f"%{q}%", f"%{q}%", f"%{q}%"]
    if state:
        wheres.append("c.state = ?")
        params.append(state)
    where = (" WHERE " + " AND ".join(wheres)) if wheres else ""

    allowed = {"id": "c.id", "city": "c.city", "state": "c.state"}
    sort_col = allowed.get(sort, "c.id")
    order_dir = "DESC" if order == "desc" else "ASC"

    base = (
        "SELECT c.id, c.unique_id, c.city, c.state, c.email, "
        "(SELECT COUNT(*) FROM orders o WHERE o.customer_id = c.id) AS orders "
        f"FROM customers c{where} ORDER BY {sort_col} {order_dir}"
    )
    count = f"SELECT COUNT(*) AS c FROM customers c{where}"
    return paginate(base, count, params, qp)


@app.get("/customers/{customer_id}")
def customer_detail(request, customer_id: str):
    row = q1("SELECT * FROM customers WHERE id = ?", [customer_id])
    if not row:
        raise HTTPException(status_code=404, detail="customer not found")
    row["orders"] = q(
        "SELECT id, status AS order_status, total_amount, created_at FROM orders "
        "WHERE customer_id = ? ORDER BY created_at DESC",
        [customer_id],
    )
    return row


@app.post("/customers", status_code=201, body_schema=CustomerIn)
def create_customer(request):
    d = validate(CustomerIn, request.json())
    db().execute(
        "INSERT INTO customers (id, unique_id, city, state, email) "
        "VALUES (?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET "
        "city=excluded.city, state=excluded.state, email=excluded.email",
        [d.id, d.unique_id, d.city, d.state, d.email],
    )
    return q1("SELECT * FROM customers WHERE id = ?", [d.id])