diff --git a/hodor/controllers/challenges.py b/hodor/controllers/challenges.py index eb109c3024c9e9b97c8b5b96af652ea3570210ae..b51ed65e37ddf241d6e1a1601c9d635b9bc2f053 100644 --- a/hodor/controllers/challenges.py +++ b/hodor/controllers/challenges.py @@ -16,7 +16,7 @@ def add_new_chall(): if request.method == "POST": try: - newchall['chall_id'] = request.data.get('chall_id').strip() + newchall['cid'] = request.data.get('cid').strip() newchall['name'] = str(request.data.get('name').strip()) newchall['points'] = request.data.get('points').strip() newchall['description'] = str(request.data.get('description').strip()) @@ -26,23 +26,23 @@ def add_new_chall(): abort(500) chall = Challenges(**newchall) chall.save() - return make_response(jsonify(status=201, msg="Challenge {} successfully added".format(chall.chall_id) + + return make_response(jsonify(status=201, msg="Challenge {} successfully added ".format(chall.cid) + "to database"), 201) ########################### # Edit an existing challenge ########################### -@app.route('/challenges/edit', methods=['PUT']) -def edit_chall(): +@app.route('/challenges/edit/<id_slug>', methods=['PUT']) +def edit_chall(id_slug): """ This function enables to edit existing challenges :return: Response Code """ - chall_id=request.data.get('chall_id').strip() - get_id = Challenges.query.get(chall_id) - chall={} + check_id = id_slug.strip() + get_id = Challenges.query.filter_by(cid=check_id) if(get_id): try: + get_id.cid = check_id get_id.name = str(request.data.get('name').strip()) get_id.points = str(request.data.get('points').strip()) get_id.description = str(request.data.get('description').strip()) @@ -51,7 +51,8 @@ def edit_chall(): print(e) abort(500) get_id.commit() - return make_response(jsonify(status=200, msg="Record succesfully edited"), 200) + edited_data = _extract_required_fields(get_id) + return make_response(jsonify(status=200, data=edited_data), 200) else: return make_response(jsonify(status=404, msg="No such challenge ID found"), 404) @@ -65,9 +66,9 @@ def del_chall(id_slug): :return: Response Code """ check_id = id_slug.strip() - get_id = Challenges.query.filter_by(chall_id=check_id) + get_id = Challenges.query.filter_by(cid=check_id) if get_id: - Challenges.query.filter_by(chall_id=id_slug).delete() + Challenges.query.filter_by(cid=check_id).delete() return make_response(jsonify(status=200, msg="Record succesfully deleted"), 200) else: return make_response(jsonify(status=404, msg="No such challenge ID found"), 404) @@ -85,7 +86,7 @@ def _extract_required_fields(chall): filter_chall['description'] = chall.description filter_chall['points'] = chall.points filter_chall['hints'] = chall.hints - filter_chall['chall_id'] = chall.chall_id + filter_chall['cid'] = chall.cid return filter_chall diff --git a/hodor/models/challenges.py b/hodor/models/challenges.py index 5c1e639b6f3d0dae4bdcfe3fe6ee9b6242504f60..3f0c78f3d9bef399b95046876b709d7e0559c051 100644 --- a/hodor/models/challenges.py +++ b/hodor/models/challenges.py @@ -7,7 +7,7 @@ class Challenges(db.Model): __tablename__= 'challenges' # Data variables for each challenge - chall_id = db.Column(db.Integer, primary_key=True, unique=True, nullable=False) + cid = db.Column(db.Integer, primary_key=True, unique=True, nullable=False) name = db.Column(db.String(32), nullable=False) points = db.Column(db.Integer, nullable=False) description = db.Column(db.String(2048), nullable=False)