Skip to content

Commit 4685bb9

Browse files
Inserting customers
1 parent 436e4e0 commit 4685bb9

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

app/routers/customer.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
from typing import Any
2+
13
from fastapi import APIRouter
24

35
from app.db.database_config import database
46
from app.model.customer import Customer
57

8+
SELECT_ALL_CUSTOMERS = "select * from customers"
9+
INSERT_CUSTOMER = (
10+
"INSERT INTO customers(first_name, last_name) VALUES (:first_name, :last_name)"
11+
)
12+
613
router = APIRouter()
714

815

@@ -12,6 +19,12 @@ def to_customer(row) -> Customer:
1219

1320
@router.get("/customers/", tags=["users"])
1421
async def read_customers() -> list[Customer]:
15-
rows = await database.fetch_all(query="select * from customers")
22+
rows = await database.fetch_all(query=SELECT_ALL_CUSTOMERS)
1623
customers: list[Customer] = list(map(to_customer, rows))
1724
return customers
25+
26+
27+
@router.put("/customers/", tags=["users"])
28+
async def insert_customers(customers: list[Customer]) -> None:
29+
rows: list[dict[str, Any]] = list(map(lambda customer: vars(customer), customers))
30+
await database.execute_many(query=INSERT_CUSTOMER, values=rows)

0 commit comments

Comments
 (0)