def getToDos(): try: toDos = ToDo.find_all() resp = [toDo.json() for toDo in toDos] return json.dumps(resp) except Exception: return InternalServerError("Something went wrong!")
def post(self, list_id): data = request.get_json() data["todoId"] = random.randint(1, 99999999999) data["listId"] = list_id post_user = ToDo(**data).save() output = post_user return generate_json_response(result=output)
def createToDo(): try: body = request.json name = body.get("name") description = body.get("description") tasks = body.get("tasks") toDo = ToDo(name=name, description=description) toDo.save_to_db() taskList = [] for task in tasks: tsk = Task(name=task.get("name"), toDoId=toDo.id, description=task.get("description")) tsk.save_to_db() taskList.append(tsk) return Response("Created ToDo with id {}".format(toDo.id), status=201) except Exception: return InternalServerError("Something went wrong")
def getToDoItem(toDoId): try: toDo = ToDo.find_by_id(int(toDoId)) if toDo is None: return BadRequest("No ToDo matches id {}".format(toDoId)) return json.dumps(toDo.json()) except Exception: return InternalServerError("Something went wrong!")
def post(self, list_id, todo_id): data = request.get_json() data["listId"] = list_id data["todoId"] = todo_id if "_id" in data: del data["_id"] post_user = ToDo.objects(todoId=todo_id, listId=list_id).update(**data) output = post_user return generate_json_response(result=output)
def deleteToDoItem(toDoId): try: toDo = ToDo.find_by_id(int(toDoId)) if toDo is None: return BadRequest("No ToDo matches id {}".format(toDoId)) toDo.delete_from_db() return Response("ToDo with id {} successfully deleted".format(toDo.id), status=201) except Exception: return InternalServerError("Something went wrong!")
def updateToDoItem(toDoId): try: toDo = ToDo.find_by_id(toDoId) if toDo is None: return BadRequest("No ToDo matches id {}".format(toDoId)) body = request.json toDo.name = body.get("name") toDo.description = body.get("description") toDo.save_to_db() toDo.delete_tasks() tasks = body.get("tasks") for task in tasks: tsk = Task(name=task.get("name"), toDoId=toDo.id, description=task.get("description")) tsk.save_to_db() return Response("ToDo with id {} successfully updated".format(toDo.id), status=201) except Exception: return InternalServerError("Something went wrong")
def post(self, list_id, todo_id): output = { 'listId': ToDo.objects(todoId=todo_id, listId=list_id).delete() } return generate_json_response(result=output)
def get(self, list_id): output = ToDo.objects(listId=list_id) return generate_json_response(result=output)