mirror of
https://codeberg.org/catask-org/catask.git
synced 2025-04-19 21:33: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:
|
||||
abort(400, "Missing 'answer' attribute or 'answer' is empty")
|
||||
|
||||
conn = func.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
|
||||
return func.addAnswer(question_id, answer, cw)
|
||||
|
||||
# -- uploaders --
|
||||
|
||||
|
|
29
functions.py
29
functions.py
|
@ -206,13 +206,36 @@ def getAnswer(question_id: int):
|
|||
conn.close()
|
||||
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):
|
||||
if os.path.exists(file):
|
||||
with open(file, 'r', encoding="utf-8") as file:
|
||||
if split == False:
|
||||
return file.read()
|
||||
if split == True:
|
||||
if split:
|
||||
return file.read().splitlines()
|
||||
else:
|
||||
return file.read()
|
||||
else:
|
||||
return []
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue