-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathadmin.py
More file actions
33 lines (26 loc) · 959 Bytes
/
admin.py
File metadata and controls
33 lines (26 loc) · 959 Bytes
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
import asyncpg
from fastapi import APIRouter
from fastapi.responses import JSONResponse
router = APIRouter(prefix="/admin", tags=["admin"])
async def get_db_connection():
return await asyncpg.connect(
host="localhost",
database="db",
user="user",
password="password",
)
@router.get("/dogs")
async def list_admin_dogs():
conn = await get_db_connection()
dogs = await conn.fetch("SELECT * FROM dogs WHERE isAdmin = TRUE")
await conn.close()
return JSONResponse({"admin_dogs": [dict(d) for d in dogs]})
@router.delete("/dogs/{dog_id}")
async def delete_dog(dog_id: int):
conn = await get_db_connection()
result = await conn.execute("DELETE FROM dogs WHERE id = $1", dog_id)
await conn.close()
deleted = int(result.split()[-1])
if deleted == 0:
return JSONResponse({"error": "Dog not found"}, status_code=404)
return JSONResponse({"message": f"Dog {dog_id} deleted"})