From 135fb64b490c69b2955442b152ec46880fe35ac5 Mon Sep 17 00:00:00 2001 From: Mr-DareDevil Date: Thu, 24 Dec 2020 20:17:53 +0530 Subject: [PATCH] Delete Endpoint added --- app/db.py | 41 +++++++++++++++++++++++++++++++---------- app/routes.py | 37 ++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/app/db.py b/app/db.py index 4ad92a1..139f33e 100644 --- a/app/db.py +++ b/app/db.py @@ -105,26 +105,47 @@ def save_frame(db, token, frame): try: user_id = decode_auth_token(token) if user_id!= None: - db.child('participants').child(user_id).child('frames').push(frame) + frame_id = db.child('participants').child(user_id).child('frames').push(frame) + frame_obj={ + "frame_data": frame, + "frame_id": frame_id["name"] + } print('* Frame added successfully') - return True + return frame_obj else: print('ERROR: Token Value is None') - return False + return None except Exception as e: print('ERROR: ', e) - return False + return None @staticmethod def get_frames(db, token): try: user_id = decode_auth_token(token) - frames = db.child('participants').child( - user_id).child('frames').get().val() - frame_arr = [] - if frames!= None: - frame_arr = [frames[fid] for fid in frames] - return frame_arr + if user_id != None: + frames = db.child('participants').child(user_id).child('frames').get().val() + frame_arr = [] + if frames!= None: + frame_arr = [{"frame_id":fid, "frame_data":frames[fid]} for fid in frames] + return frame_arr + else: + print('ERROR: Token Value is None') + return None except Exception as e: print('ERROR: ', e) return None + + @staticmethod + def delete_frames(db, token, frame_id): + try: + user_id = decode_auth_token(token) + if user_id != None: + db.child('participants').child(user_id).child('frames').child(frame_id).remove() + return True + else: + print('ERROR: Token Value is None') + return False + except Exception as e: + print('ERROR: ', e) + return False \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index b2a1e37..a45b43a 100644 --- a/app/routes.py +++ b/app/routes.py @@ -29,14 +29,14 @@ def register(): def login(): user = request.json user_id = Db.authorise_participants(database, user) - user_token=Db.get_token(database, user_id) + user_token = Db.get_token(database, user_id) if (user_id != None): - return make_response(jsonify({"status": "success", "token": user_token})), 202 + return make_response(jsonify({"token": user_token})), 202 else: - return make_response(jsonify({"status": "fail", "message": "Login failed"})), 401 + return make_response(jsonify({"message": "Login failed"})), 401 -@app.route(BASE_URL+'/frames', methods=['POST', 'GET']) +@app.route(BASE_URL+'/frames', methods=['POST', 'GET', 'DELETE']) @swag_from('../docs/getframes.yml', methods=['GET']) @swag_from('../docs/postframes.yml', methods=['POST']) def frames(): @@ -46,7 +46,6 @@ def frames(): auth_token = auth_header.split(" ")[1] except IndexError: responseObject = { - 'status': 'fail', 'message': 'Bearer token malformed.' } return make_response(jsonify(responseObject)), 401 @@ -56,21 +55,16 @@ def frames(): if request.method == 'POST': frame = request.json['frame'] if frame: - if Db.save_frame(database, auth_token, frame): - responseObject = { - 'status': 'success', - 'message': 'Frame added' - } - return make_response(jsonify(responseObject)), 200 + responseObject = Db.save_frame(database, auth_token, frame) + if responseObject != None: + return make_response(jsonify(responseObject)), 201 else: responseObject = { - 'status': 'fail', 'message': 'Frame cannot be added' } return make_response(jsonify(responseObject)), 401 else: responseObject = { - 'status': 'fail', 'message': 'Provide valid frame data' } return make_response(jsonify(responseObject)), 401 @@ -78,19 +72,28 @@ def frames(): frames_arr = Db.get_frames(database, auth_token) if frames_arr != None: responseObject = { - 'status': 'success', 'frames': frames_arr } return make_response(jsonify(responseObject)), 201 else: responseObject = { - 'status': 'fail', 'message': 'Something went wrong' } return make_response(jsonify(responseObject)), 401 + elif request.method == 'DELETE': + frame_id = request.args.get('id') + if Db.delete_frames(database, auth_token, frame_id): + responseObject = { + 'message': 'Frame was deleted successfully' + } + return make_response(jsonify(responseObject)), 201 + else: + responseObject = { + 'message': 'Frame was not deleted!' + } + return make_response(jsonify(responseObject)), 401 else: responseObject = { - 'status': 'fail', 'message': 'Provide a valid auth token.' } - return make_response(jsonify(responseObject)), 401 \ No newline at end of file + return make_response(jsonify(responseObject)), 401