jacob-macleod / Dolphin-Flashcard-App

http://www.dolphinflashcards.com
MIT License
1 stars 3 forks source link

Develop API route for Total XP #85

Closed jacob-macleod closed 2 months ago

jacob-macleod commented 4 months ago

Dashboard Here, you can see the total XP (6093). The dashboard page is in development, and an API route needs to be written to read the total XP from the database and return it.

How the code works is:

Every API request is made with json data, which provides information needed for the request

Your code will go in backend/routes/api/statistics.py. The format of each function that already exists is as follows - below you can see a simplified existing function with some comments:.

# /api/get-heatmap is the URL the user will go to to get the data. It must begin with /api/
# "POST" in methods=["POST"] means new data will be created. Your route will only read data, so
# you'll have methods=["GET"]
@statistics_routes.route("/api/get-heatmap", methods=["POST"])
def get_heatmap() : // The unique function name
    # A comment explaining the code and how to use it
    """ Get the user's heatmap data
        Requests should have json in the following format:
    {
        "userID": "my id"
    }
    """
    # Check the request json
    # A variable storing a dictionary storing the format we should expect the request to be in. You only need
    # To take the userID, so your variable will be the same
    expected_format = {
            "userID": "",
        }
    # Next few line - check the json provided with the request is correct. request.json is the 
    # json the user provides with the request
    result = check_request_json(
        expected_format,
        request.json
    )
    if result is not True:
        return jsonify(
            {"error": result + ". The request should be in the format: " + str(expected_format)}
        ), 400

    # Get the userID
    user_id = request.json.get("userID")
    # Read the heatmap data. Your total XP data is at "/users/" + user_id + "/statistics/totalXP"
    heatmap = db.get("/users/" + user_id + "/heatmapData")
    # Return the data read in json format
    return jsonify(heatmap)
jacob-macleod commented 4 months ago

The first stage is to test the current API methods to understand them. To do this, you can use an app called insomnia - https://insomnia.rest/download. Let me know if you need more info, but you do the following:

Note that if you don't use insomnia, you can go to a URL in your browser, but I don't think you can add json data to tell it userID and things like that.

Then, open insomnia:

You can change the string values if you want, When you're ready, press the button to go to the route. A 200 OK message should be returned, telling you there were no errors. Open data.json, and you'll see that your new data has been added. Try this with a few more API routes to get a feel for how it works.

You'll need to do this to create an account so that when you read a user's totalXP, there will actually be a user. When you write the code to get a user's XP, and test it in insomnia, you will be passing it a json file that looks similar to this:

{
  "userID":"my-id"
}

Provided you tell it to accept that in the code. my-id will need to be the ID of an existing user stored in data.json

If you have any questions, please ask!

jacob-macleod commented 3 months ago

(This issue is not ready to be worked on yet)

jacob-macleod commented 2 months ago

This is now done