I'm coding a simple todo list app in Flask for study. My todo model has title, description and a check attribute. When false is a "to do" task, and when the user click on a checkbox it will change to "true", so that task was done. That said, my current PATCH route can handle the title, description and check change, all in one.
So now I was thinking: in my frontend, I'll create the checkbox and when it's clicked the client will send a PATCH request with just the check value; for description and title, I'll create a "edit", so the user can edit those two and when its done he can click in a "save" button, when its clicked the client will send a PATCH request with title and/or description changes.
I realized that there's no way to a request with changes on all the three (title, description and check) could be made, check will be always working alone.
This is my PATCH code:
@app.route("/todo/update/<id>", methods=["PATCH"])def update_todo(id): updated_todo = db.session.execute(db.select(Todo).filter_by(id=id)).scalar_one() if "check" in request.json: updated_todo.check = request.json["check"] if "title" in request.json: updated_todo.title = request.json["title"] if "description" in request.json: updated_todo.description = request.json["description"] db.session.commit() return "", 204
Since I'm using if to know what's changing, could I create a new PATCH route just for check and let it work alone? Since I don't need to verify if check is in a title/description PATCH request (it never will be anyway), it'll be a little faster in theory, right? Is there any conceptual or convention problem around it?
It would be something like this:
@app.route("/todo/update/<id>", methods=["PATCH"])def update_todo(id): updated_todo = db.session.execute(db.select(Todo).filter_by(id=id)).scalar_one() if "title" in request.json: updated_todo.title = request.json["title"] if "description" in request.json: updated_todo.description = request.json["description"] db.session.commit() return "", 204@app.route("/todo/update/check/<id>", methods=["PATCH"])def update_todo(id): updated_todo = db.session.execute(db.select(Todo).filter_by(id=id)).scalar_one() updated_todo.check = request.json["check"] db.session.commit() return "", 204