def show_like(like_id): try: # tries to get the like by its id like = Like.get(Like.id == like_id) # convert to dictionary and remove users passwords like_dict = model_to_dict(like) del like_dict['user']['password'] del like_dict['post']['user']['password'] return jsonify(data=like_dict, status={ 'code': 200, 'message': 'Successfully got like.' }) # exception thrown if the like doesnt exist except DoesNotExist: return jsonify(data={}, status={ 'code': 404, 'message': 'Failure to get resource.' })
def create_like(): try: # gets data from the client - contains a post id data = request.get_json() # tries to get the post by its id post_id = Post.get(Post.id == data['postId']) try: # checks if a like for that post and user already exists like = Like.get(Like.post == post_id, Like.user == current_user.id) # if the user has not already likes the post except DoesNotExist: # creates a new like for the post like = Like.create(post=post_id, user=current_user.id) # convert like to dictionary and remove both users password like_dict = model_to_dict(like) del like_dict['user']['password'] del like_dict['post']['user']['password'] return jsonify(data=like_dict, status={ 'code': 201, 'message': 'User successfully liked post.' }) # exception thrown if the post doesnt exist except DoesNotExist: return jsonify(data={}, status={ 'code': 404, 'message': 'Failure to get resource.' })