From dd27fc8e095a22146adc5966eff81e4bfae13c57 Mon Sep 17 00:00:00 2001 From: mystie Date: Fri, 28 Mar 2025 19:51:08 +0300 Subject: [PATCH] abort if question is not found --- functions.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/functions.py b/functions.py index d03b18c..84691ed 100644 --- a/functions.py +++ b/functions.py @@ -137,14 +137,22 @@ def connectToDb(): return psycopg.connect(f"postgresql://{dbUser}:{dbPass}@{dbHost}/{dbName}", row_factory=dict_row) def getQuestion(question_id: int) -> dict: - conn = connectToDb() - cursor = conn.cursor() - cursor.execute("SELECT * FROM questions WHERE id=%s", (question_id,)) - question = cursor.fetchone() - question['creation_date'] = question['creation_date'].replace(microsecond=0).replace(tzinfo=None) - cursor.close() - conn.close() - return question + try: + conn = connectToDb() + cursor = conn.cursor() + cursor.execute("SELECT * FROM questions WHERE id=%s", (question_id,)) + question = cursor.fetchone() + + if not question: + return abort(404) + + question['creation_date'] = question['creation_date'].replace(microsecond=0).replace(tzinfo=None) + + return question + + finally: + cursor.close() + conn.close() def getAllQuestions(limit: int = None, offset: int = None) -> dict: conn = connectToDb()