def postTodos(): # get body body = request.get_json() # instancia modelo Todos y se inicializa con el valor del body todo = Todos(body["label"], body["is_done"]) # guardar task en base datos todo.save() return jsonify(todo.serialize()), 200
def post_todos(): body = request.get_json() if 'done' not in body: return 'Invalid todo', 400 if 'label' not in body: return 'Invalid todo', 400 todo = Todos(done=body['done'], label=body['label']) db.session.add(todo) db.session.commit() return jsonify(todo.serialize()), 200
def create_todos(): response = request.get_json() if 'done' not in response: return 'Invalid', 400 if 'label' not in response: return 'Invalid', 400 row = Todos(done=response['done'], label=response['label']) db.session.add(row) db.session.commit() return jsonify(row.serialize()), 200
def handler(input1=None): if request.method == "GET": if input1 is not None: todos = Todos.query.filter_by(name=input1).first() if todos: return jsonify(todos.serialize()), 200 else: return jsonify({"msg": "Not Found"}), 404 else: todos = Todos.query.all() todos = list(map(lambda todos: todos.serialize(), todos)) return jsonify(todos), 200 if request.method == "POST": if not input1 or input1 == "": # si no llego un nombre o el nombre esta vacio return {"msg": "Field name is required"}, 400 # 422 todo = Todos() todo.name = input1 db.session.add(todo) db.session.commit() todos = Todos.query.all() todos = list(map(lambda todos: todos.serialize(), todos)) return jsonify(todos), 200 if request.method == "PUT": if not input1 or input1 == "": # si no llego un nombre o el nombre esta vacio return {"msg": "Field name is required"}, 400 # 422 todos = Todos.query.filter_by(name=input1).first() newName = request.json.get("newName") if not newName or newName == "": return {"msg": "newName missing"}, 400 todo.name = newName db.session.commit() return jsonify(todo.serialize()), 200 # 422 if request.method == "DELETE": if not input1 or input1 == "": # si no llego un nombre o el nombre esta vacio return {"msg": "Field name is required"}, 400 # 422 todos = Todos.query.filter_by(name=input1).first() if not todos: return {"msg": "Not Found"}, 400 db.session.delete(todos) db.session.commit() todos = Todos.query.all() todos = list(map(lambda todos: todos.serialize(), todos)) return jsonify(todos), 200
def handle_todo(): #GET if request == "GET": todo = Todos.query.all(todo_id) todoList = list(map(lambda x: x.serialize(), todo)) return jsonify(todo.serialize()), 200 #POST if request.method == "POST": body = request.get_json() todo = Todos(text=body['text']) db.session.add(todo) db.session.commit() response_body = { "status": "Successfully added.", "todo": todo.serialize(), "repr": repr(todo) } return jsonify(response_body), 205