diff --git a/project_amber/const.py b/project_amber/const.py index bf5bb53..33bd46f 100644 --- a/project_amber/const.py +++ b/project_amber/const.py @@ -1,3 +1,5 @@ from json import dumps EMPTY_RESP = dumps({}) # Empty response, to be used in requests. + +TASK_MSG_NOT_FOUND = "This task does not exist." diff --git a/project_amber/helpers/task.py b/project_amber/helpers/task.py new file mode 100644 index 0000000..0bf28cc --- /dev/null +++ b/project_amber/helpers/task.py @@ -0,0 +1,39 @@ +from time import time + +from project_amber.const import TASK_MSG_NOT_FOUND +from project_amber.db import db +from project_amber.errors import NotFound +from project_amber.models.task import Task + +def addTask(text: str, uid: int) -> int: + """ + Creates a new task. Returns its ID. + """ + task = Task(owner=uid, text=text, creation_time=time(), \ + last_mod_time=time()) + db.session.add(task) + db.session.commit() + return task.id + +def updateTask(new_text: str, task_id: int, uid: int) -> int: + """ + Updates the task text. Returns its ID. + """ + task = db.session.query(Task).filter_by(id=task_id, owner=uid).first() + if task is None: + raise NotFound(TASK_MSG_NOT_FOUND) + task.text = new_text + db.session.commit() + return task_id + + +def removeTask(task_id: int, uid: int) -> int: + """ + Removes a task. Returns its ID on success. + """ + task = db.session.query(Task).filter_by(id=task_id, owner=uid).first() + if task is None: + raise NotFound(TASK_MSG_NOT_FOUND) + db.session.delete(task) + db.session.commit() + return task_id