This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |