~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/checkout-demo

A demo of knotless's review agent

0
0
main
19 lines · 565 B Raw History
"""A cart's total and the order it becomes. Money is integer cents."""

import sqlite3


def subtotal(items):
    """The cart's items, each {"price": cents, "quantity": n}, summed."""
    return sum(item["price"] * item["quantity"] for item in items)


def place_order(db: sqlite3.Connection, customer_id: int, items) -> int:
    """Writes the order and returns its id."""
    total = subtotal(items)
    cur = db.execute(
        "INSERT INTO orders (customer_id, total) VALUES (?, ?)",
        (customer_id, total),
    )
    db.commit()
    return cur.lastrowid