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

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor task API
  • Loading branch information
tdemin committed Jun 12, 2019
1 parent 774a301 commit d74f592
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 7 additions & 3 deletions project_amber/handlers/task.py
Expand Up @@ -61,7 +61,11 @@ def handle_task_request():
if not "text" in request.json:
raise BadRequest("No text specified")
text = request.json["text"]
new_id = addTask(text, user.id)
status = request.json.get("status")
# if only I could `get("status", d=0)` like we do that with dicts
if status is None:
status = 0
new_id = addTask(text, status, user.id)
return dumps({ "id": new_id })

def handle_task_id_request(task_id: int):
Expand Down Expand Up @@ -95,7 +99,7 @@ def handle_task_id_request(task_id: int):
"status": task.status,
"last_mod": task.last_mod_time
})
elif request.method == "PATCH":
if request.method == "PATCH":
text = None
status = None
if "text" in request.json:
Expand All @@ -104,6 +108,6 @@ def handle_task_id_request(task_id: int):
status = request.json["status"]
updateTask(task_id, user.id, text=text, status=status)
return EMPTY_RESP
elif request.method == "DELETE":
if request.method == "DELETE":
removeTask(task_id, user.id)
return EMPTY_RESP
10 changes: 4 additions & 6 deletions project_amber/helpers/task.py
Expand Up @@ -5,15 +5,13 @@
from project_amber.errors import NotFound
from project_amber.models.task import Task

def addTask(text: str, uid: int, **kwargs) -> int:
def addTask(text: str, status: int, uid: int) -> int:
"""
Creates a new task. Returns its ID.
"""
status = 0
if "status" in kwargs:
status = kwargs["status"]
task = Task(owner=uid, text=text, creation_time=time(), \
last_mod_time=time(), status=status)
task_time = time()
task = Task(owner=uid, text=text, creation_time=task_time, \
last_mod_time=task_time, status=status)
db.session.add(task)
db.session.commit()
return task.id
Expand Down

0 comments on commit d74f592

Please sign in to comment.