Input

const newRating = prompt("Enter a new rating (1-10):");

The update feature takes input as an integer, asking for a new rating value on the frontend from the user, and storing it as a variable called newRating. This is the rating that a user enters assoicated with their experience with a particular attraction on the activity planner feature.

Use of a List

data = request.get_json()
body: JSON.stringify({
    rating_id: index.rating_id,  // Use the correct ID for the rating to update
    rating: newRating  // Pass the new rating value
})

The PUT request on the frontend, in the body, passes a javascript object(list-type data) with a rating_id and puts in the JSON data type, and rating to the backend, which is accessed through the data variable in the backend to store information regarding ratings.

Procedure/Algorithm

@token_required()
def put(self):
    """
    Update a rating (1-10 scale) for a post.
    """
    # Get current user from the token
    current_user = g.current_user
    # Get the request data
    data = request.get_json()
    rating_id = data.get('rating_id')
    new_rating_value = data.get('rating')

    # Validate rating_id and new_rating_value
    if rating_id is None or new_rating_value is None:
        return jsonify({"message": "rating_id and new rating value are required"}), 400

    try:
        new_rating_value = int(new_rating_value)
    except ValueError:
        return jsonify({"message": "Rating must be an integer"}), 400

    if not (1 <= new_rating_value <= 10):
        return jsonify({"message": "Rating must be between 1 and 10"}), 400

    # Find the rating by ID and join with User to get the user's role
    rating = db.session.query(Rate, User).join(User, Rate.user_id == User.id).filter(Rate.id == rating_id).first()

    # Check if the current user is the owner of the rating or an admin
    if rating.Rate.user_id == current_user.id or current_user.role == 'Admin':
        # Update the rating value
        rating.Rate.value = new_rating_value
        db.session.commit()

        return jsonify({"message": "Rating updated successfully"})
    else:
        return jsonify({"message": "Not authorized to update this rating"}), 403

This is a procedure called PUT in the backend apart of the rate API, which updates the rating value for a given/existing rate.

Sequencing

  • Overall shows sequencing by first defining the vairables, then storing them in lists, and then adding them to the database table, showing an algorithm with a particular sequence allowing it to function properly. If the procedure were to be executed in another order, and the variables were defined later, there would be no data to add to the table, therefore this procedure/algorithm shows a strong sense of sequencing and order.

Selection

if rating_id is None or new_rating_value is None:
    return jsonify({"message": "rating_id and new rating value are required"}), 400

This shows selection because the if statement is a conditional which goes through selection and error handles in order to validate if the data is suitable to add to the database table, so it is selecting whether or not the data meets the criteria.

Iteration

rating = db.session.query(Rate, User).join(User, Rate.user_id == User.id).filter(Rate.id == rating_id).first()

This code snippet within the algorithm shows iteration, because through a query the code iterates through all of the rows of the rate table systematically and searches for ratings with user_ids and rating_ids which match with that of the current user.

Call to Algorithm

const response = await fetch(`${pythonURI}/api/rate`, {
    method: 'PUT',
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        rating_id: index.rating_id,  // Use the correct ID for the rating to update
        rating: newRating  // Pass the new rating value
    }),
    credentials: 'include',  // This ensures that cookies and session info are included

In the frontend I am sending a request to the backend rate API using the PUT method, and this passed data like the rating_id, and the rating to the put/update procedure/algorithm in the backend in order to update a current rating with a new rating in the database.

Output / Display

data.forEach((rating, index) => {
    const row = document.createElement("tr");

    row.innerHTML = `
        <td>${rating.rating}</td>
        <td>${rating.username}</td>
        <td>
            <button class="action-btn" id="update-btn-${index}">Update</button>
            <button class="action-btn" id="delete-btn-${index}">Delete</button>
        </td>
    `;

    body.appendChild(row);
});

This code on the frontend displays the new and updated rating along with a username in a distinct row of a table, creating a new row with the attributes of rating username and rating.