Skip to content

Commit

Permalink
Delete Endpoint added
Browse files Browse the repository at this point in the history
  • Loading branch information
arnabsen1729 committed Dec 24, 2020
1 parent 6282001 commit 135fb64
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
41 changes: 31 additions & 10 deletions app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 20 additions & 17 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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
Expand All @@ -56,41 +55,45 @@ 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
elif request.method == 'GET':
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
return make_response(jsonify(responseObject)), 401

0 comments on commit 135fb64

Please sign in to comment.