Example #1
0
    def delete(self, review_id):
        """Deletes a review."""
        review = get_review_by_id(review_id)
        response_object = {}
        # User validation before deleting
        was_successful, session_token = extract_token(request)
        response_object = {}
        if not was_successful:
            response_object["message"] = session_token
            return response_object, 400
        # Check token validity
        user = get_user_by_session_token(session_token)
        if user is None:
            response_object["message"] = "Invalid token."
            return response_object, 400
        # Create / validate Review object
        user_id = user.id
        review = get_review_by_id(review_id)
        if review is None:
            reviews_namespace.abort(404, f"Review {review_id} does not exist")
        elif user_id != review.user_id:
            reviews_namespace.abort(400, "Cannot delete other user's review")

        if not review:
            reviews_namespace.abort(404, f"Review {review_id} does not exist.")

        delete_review(review)
        response_object["message"] = f"Review {review.id} was deleted."
        return response_object, 200
Example #2
0
    def post(self):
        """Creates a new review."""
        # Extract token
        was_successful, session_token = extract_token(request)
        response_object = {}
        if not was_successful:
            response_object["message"] = session_token
            return response_object, 400
        # Check token validity
        user = get_user_by_session_token(session_token)
        if user is None:
            response_object["message"] = "Invalid Token."
            return response_object, 400
        # Create / validate Review object
        user_id = user.id
        post_data = request.get_json()
        place_id = post_data.get("place_id")
        rating = post_data.get("rating")
        text = post_data.get("text")
        response_object = {}

        if None in [user_id, place_id, rating, text]:
            response_object["message"] = "Request body malformed."
            return response_object, 400
        elif (type(rating) != int) or not (0 <= rating <= 5):
            response_object["message"] = "Request body malformed."
            return response_object, 400
        else:
            add_review(user_id, place_id, rating, text)
            response_object["message"] = "Review posted successfully!"
            return response_object, 201
Example #3
0
    def put(self, review_id):
        """Updates the star rating / text of a review."""
        # Extract token
        was_successful, session_token = extract_token(request)
        response_object = {}
        if not was_successful:
            response_object["message"] = session_token
            return response_object, 400
        # Check token validity
        user = get_user_by_session_token(session_token)
        if user is None:
            response_object["message"] = "Invalid token."
            return response_object, 400
        # Create / validate Review object
        user_id = user.id
        review = get_review_by_id(review_id)
        if review is None:
            reviews_namespace.abort(404, f"Review {review_id} does not exist")
        elif user_id != review.user_id:
            reviews_namespace.abort(400, "Cannot edit other user's review")

        post_data = request.get_json()
        rating = post_data.get("rating")
        text = post_data.get("text")
        response_object = {}
        review = get_review_by_id(review_id)
        if not review:
            reviews_namespace.abort(404, f"Review {review_id} does not exist.")
        new_review = update_review(review, rating, text)

        response_object["message"] = f"Review {review.id} was updated!"
        print(new_review)
        return marshal(new_review, review_fields), 200
Example #4
0
 def get(self):
     """Returns user based on session token."""
     was_successful, session_token = extract_token(request)
     response_object = {}
     if not was_successful:
         response_object["message"] = session_token
         return response_object, 400
     request_user = get_user_by_session_token(session_token)
     if request_user is None:
         response_object["message"] = "Unauthorized user."
         return response_object, 400
     return marshal(request_user, user_fields), 200
Example #5
0
 def get(self):
     """Returns all users."""
     was_successful, session_token = extract_token(request)
     response_object = {}
     if not was_successful:
         response_object["message"] = session_token
         return response_object, 400
     request_user = get_user_by_session_token(session_token)
     if request_user is None:
         response_object["message"] = "Unauthorized user."
         return response_object, 400
     users = get_all_users()
     # return marshal(users, user_fields), 200
     return list(map(lambda x: x.as_dict(), users)), 200
Example #6
0
    def post(self):
        """Shows the list of user favorites given the session token."""
        was_successful, session_token = extract_token(request)
        response_object = {}
        if not was_successful:
            response_object["message"] = session_token
            return response_object, 400
        request_user = get_user_by_session_token(session_token)
        if request_user is None:
            response_object["message"] = "Unauthorized user."
            return response_object, 400

        data = []
        for pl in request_user.favorites:
            data.append(pl.serialize())
        return data, 200
Example #7
0
 def delete(self, place_id):
     """Removes place from user favorites."""
     was_successful, session_token = extract_token(request)
     response_object = {}
     if not was_successful:
         response_object["message"] = session_token
         return response_object, 400
     request_user = get_user_by_session_token(session_token)
     if request_user is None:
         response_object["message"] = "Unauthorized user."
         return response_object, 400
     place = get_place_by_id(place_id)
     if place is None:
         response_object["message"] = "Invalid place id."
         return response_object, 400
     # place_id and user is valid.
     remove_favorite(request_user, place)
     response_object["message"] = "Removed location from favorite!"
     return response_object, 201