-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
46 lines (35 loc) · 1.22 KB
/
main.py
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
from fastapi import FastAPI, HTTPException
import asyncpg
import os
app = FastAPI()
DB_URL = os.getenv("DB_URL")
async def init_db():
conn = await asyncpg.connect(DB_URL)
try:
await conn.execute('''CREATE TABLE IF NOT EXISTS products (
name TEXT,
price NUMERIC,
description TEXT,
article VARCHAR(255)
)''')
await conn.execute('''INSERT INTO products (name, price, description, article) VALUES
('Google Pixel', 37000, 'Описание Google Pixel', 'GPX123'),
('Redmi', 10000, 'Описание Redmi', 'RDM456'),
('Samsung Galaxy', 60000, 'Описание Samsung Galaxy', 'SGZ789');
''')
finally:
await conn.close()
async def get_products():
conn = await asyncpg.connect(DB_URL)
rows = await conn.fetch('SELECT * FROM products')
await conn.close()
return rows
@app.on_event("startup")
async def startup_event():
await init_db()
@app.get("/products", response_model=list[dict])
async def read_products():
products = await get_products()
if not products:
raise HTTPException(status_code=404, detail="No products found")
return [dict(p) for p in products]