фильтрация
This commit is contained in:
75
main.py
75
main.py
@@ -186,7 +186,7 @@ def get_records(offset: int = Query(0, ge=0), limit: int = Query(10, ge=1, le=10
|
||||
conn.close()
|
||||
|
||||
@app.get("/records_all", summary="Получить все записи из БД + сортирует", response_model=List[ParsedData])
|
||||
def get_records():
|
||||
def get_records(item: str = "default"):
|
||||
"""
|
||||
Возвращает записи из таблицы url с учетом.
|
||||
"""
|
||||
@@ -194,43 +194,32 @@ def get_records():
|
||||
try:
|
||||
conn = get_connection()
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
print(item)
|
||||
if item == "time":
|
||||
cur.execute("""
|
||||
SELECT url, parsed_at, title, original_text, article_date, status, viewed, other, category, translation_text, short_text
|
||||
FROM url
|
||||
ORDER BY (viewed = true) ASC, parsed_at DESC
|
||||
SELECT * FROM url
|
||||
ORDER BY viewed ASC, parsed_at DESC
|
||||
""")
|
||||
rows = cur.fetchall()
|
||||
results = [dict(row) for row in rows]
|
||||
urls = [item['parsed_at'] for item in results]
|
||||
|
||||
print(urls)
|
||||
return results
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Ошибка при получении записей из БД: {e}")
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
@app.get("/records_all_status", summary="Получить все записи избранного", response_model=List[ParsedData])
|
||||
def get_records():
|
||||
"""
|
||||
Возвращает записи из таблицы url с избранным.
|
||||
"""
|
||||
conn = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
elif item == "viewed":
|
||||
cur.execute("""
|
||||
SELECT url, parsed_at, title, original_text, article_date, status, viewed, other, category, translation_text, short_text
|
||||
FROM url
|
||||
SELECT * FROM url
|
||||
WHERE viewed = true
|
||||
ORDER BY article_date DESC
|
||||
""")
|
||||
elif item == "status":
|
||||
cur.execute("""
|
||||
SELECT * FROM url
|
||||
WHERE status = true
|
||||
ORDER BY parsed_at DESC
|
||||
""")
|
||||
else:
|
||||
cur.execute("""
|
||||
SELECT * FROM url
|
||||
ORDER BY viewed ASC, article_date DESC
|
||||
""")
|
||||
rows = cur.fetchall()
|
||||
results = [dict(row) for row in rows]
|
||||
urls = [item['parsed_at'] for item in results]
|
||||
|
||||
print(urls)
|
||||
# urls = [item['parsed_at'] for item in results]
|
||||
return results
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Ошибка при получении записей из БД: {e}")
|
||||
@@ -238,6 +227,32 @@ def get_records():
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
# @app.get("/records_all_status", summary="Получить все записи избранного", response_model=List[ParsedData])
|
||||
# def get_records():
|
||||
# """
|
||||
# Возвращает записи из таблицы url с избранным.
|
||||
# """
|
||||
# conn = None
|
||||
# try:
|
||||
# conn = get_connection()
|
||||
# with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
# cur.execute("""
|
||||
# SELECT * FROM url
|
||||
# WHERE status
|
||||
# ORDER BY parsed_at DESC
|
||||
# """)
|
||||
# rows = cur.fetchall()
|
||||
# results = [dict(row) for row in rows]
|
||||
# urls = [item['parsed_at'] for item in results]
|
||||
|
||||
# print(urls)
|
||||
# return results
|
||||
# except Exception as e:
|
||||
# raise HTTPException(status_code=500, detail=f"Ошибка при получении записей из БД: {e}")
|
||||
# finally:
|
||||
# if conn:
|
||||
# conn.close()
|
||||
|
||||
# get_records()
|
||||
|
||||
# Запуск сервера для теста
|
||||
|
||||
Reference in New Issue
Block a user