File tree Expand file tree Collapse file tree 1 file changed +14
-1
lines changed
Expand file tree Collapse file tree 1 file changed +14
-1
lines changed Original file line number Diff line number Diff line change 1+ from typing import Any
2+
13from fastapi import APIRouter
24
35from app .db .database_config import database
46from 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+
613router = APIRouter ()
714
815
@@ -12,6 +19,12 @@ def to_customer(row) -> Customer:
1219
1320@router .get ("/customers/" , tags = ["users" ])
1421async 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 )
You can’t perform that action at this time.
0 commit comments