Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Add task manipulation helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tdemin committed Jun 8, 2019
1 parent 55ae1b4 commit d292534
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 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."
39 changes: 39 additions & 0 deletions 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

0 comments on commit d292534

Please sign in to comment.