25 lines
575 B
Python
25 lines
575 B
Python
|
|
from fastapi import APIRouter
|
||
|
|
import psycopg2
|
||
|
|
from psycopg2.extras import RealDictCursor
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
def get_db_connection():
|
||
|
|
return psycopg2.connect(
|
||
|
|
host="localhost",
|
||
|
|
port=5432,
|
||
|
|
database="korobka_db",
|
||
|
|
user="postgres",
|
||
|
|
password="postgres"
|
||
|
|
)
|
||
|
|
|
||
|
|
@router.get("/heroes")
|
||
|
|
def get_heroes():
|
||
|
|
conn = get_db_connection()
|
||
|
|
cursor = conn.cursor(cursor_factory=RealDictCursor)
|
||
|
|
cursor.execute("SELECT id, name FROM hero ORDER BY id")
|
||
|
|
heroes = cursor.fetchall()
|
||
|
|
cursor.close()
|
||
|
|
conn.close()
|
||
|
|
return heroes
|