mirror of
https://codeberg.org/catask-org/catask.git
synced 2025-04-20 05:43:41 -05:00
move addAnswer logic into functions
This commit is contained in:
parent
b7cb3f8247
commit
7f615bbdf1
2 changed files with 27 additions and 24 deletions
22
app.py
22
app.py
|
@ -436,27 +436,7 @@ def addAnswer():
|
||||||
if not answer:
|
if not answer:
|
||||||
abort(400, "Missing 'answer' attribute or 'answer' is empty")
|
abort(400, "Missing 'answer' attribute or 'answer' is empty")
|
||||||
|
|
||||||
conn = func.connectToDb()
|
return func.addAnswer(question_id, answer, cw)
|
||||||
try:
|
|
||||||
cursor = conn.cursor()
|
|
||||||
|
|
||||||
app.logger.debug("[CatAsk/API/add_answer] INSERT'ing an answer into database")
|
|
||||||
|
|
||||||
cursor.execute("INSERT INTO answers (question_id, content, cw) VALUES (%s, %s, %s)", (question_id, answer, cw))
|
|
||||||
answer_id = cursor.lastrowid
|
|
||||||
|
|
||||||
app.logger.debug("[CatAsk/API/add_answer] UPDATE'ing question to set answered and answer_id")
|
|
||||||
|
|
||||||
cursor.execute("UPDATE questions SET answered=%s, answer_id=%s WHERE id=%s", (True, answer_id, question_id))
|
|
||||||
conn.commit()
|
|
||||||
except Exception as e:
|
|
||||||
conn.rollback()
|
|
||||||
return jsonify({'error': str(e)}), 500
|
|
||||||
finally:
|
|
||||||
cursor.close()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return jsonify({'message': 'Answer added successfully!'}), 201
|
|
||||||
|
|
||||||
# -- uploaders --
|
# -- uploaders --
|
||||||
|
|
||||||
|
|
29
functions.py
29
functions.py
|
@ -206,13 +206,36 @@ def getAnswer(question_id: int):
|
||||||
conn.close()
|
conn.close()
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
|
def addAnswer(question_id, answer, cw):
|
||||||
|
conn = connectToDb()
|
||||||
|
try:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
app.logger.debug("[CatAsk/API/add_answer] INSERT'ing an answer into database")
|
||||||
|
|
||||||
|
cursor.execute("INSERT INTO answers (question_id, content, cw) VALUES (%s, %s, %s)", (question_id, answer, cw))
|
||||||
|
answer_id = cursor.lastrowid
|
||||||
|
|
||||||
|
app.logger.debug("[CatAsk/API/add_answer] UPDATE'ing question to set answered and answer_id")
|
||||||
|
|
||||||
|
cursor.execute("UPDATE questions SET answered=%s, answer_id=%s WHERE id=%s", (True, answer_id, question_id))
|
||||||
|
conn.commit()
|
||||||
|
except Exception as e:
|
||||||
|
conn.rollback()
|
||||||
|
return jsonify({'error': str(e)}), 500
|
||||||
|
finally:
|
||||||
|
cursor.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
return jsonify({'message': 'Answer added successfully!'}), 201
|
||||||
|
|
||||||
def readPlainFile(file, split=False):
|
def readPlainFile(file, split=False):
|
||||||
if os.path.exists(file):
|
if os.path.exists(file):
|
||||||
with open(file, 'r', encoding="utf-8") as file:
|
with open(file, 'r', encoding="utf-8") as file:
|
||||||
if split == False:
|
if split:
|
||||||
return file.read()
|
|
||||||
if split == True:
|
|
||||||
return file.read().splitlines()
|
return file.read().splitlines()
|
||||||
|
else:
|
||||||
|
return file.read()
|
||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue