Image-Text-Search / controllers /SearchControllers.py
A7medAyman's picture
async operations
63e95b2
Raw History Blame Contribute Delete
1.1 kB
from fastapi import Request
class SearchControllers:
def __init__(self, limit: int, request: Request):
self.limit = limit
self.request = request
self.client = self.request.app.client
async def url_response(self, search_res):
urls_response = []
async with self.request.app.SQLDatabasePool.acquire() as conn:
async with conn.cursor() as cursor:
for hit in search_res:
query = """
SELECT Id, PictureUrl
FROM Furniture
WHERE Id = ?
"""
await cursor.execute(query, (hit.id,))
row = await cursor.fetchone()
if row:
# Convert tuple row to dict using cursor.description
columns = [col[0] for col in cursor.description]
result = dict(zip(columns, row))
result["Id"] = str(result["Id"])
urls_response.append(result)
return urls_response