Trivia back end server

I have now set up the back end server for the trivia app at https://cmsc106.net/trivia.

The back end server will allow you to construct trivia quizzes composed of individual questions. Users who want to answer the trivia questions will have to set up an account on the server. Registered users will be able to view all of the quizzes available on server, the questions in each quiz, and post their answers to the questions to the server. The server will keep track of how many questions each user has answered correctly, and can provide a leaderboard list of players and their scores.

Creating quizzes and posting questions

Creating quizzes and questions is a separate task from answering trivia questions, so I will leave it up to you to decide whether or not you want to add views to your app that will allow users to publish their own quizzes. If you do not wish to implement this feature in your app, you will need to use the Postman application to handle posting quizzes and questions to the back end server.

To create a new quiz on the server set up a POST request with the URL

https://cmsc106.net/trivia/quizzes?publisher=<luid>

with <luid> replaced with your Lawrence ID.

In the body of the POST request you need to put a JSON object with a single title property. This is the title for your quiz. For example,

{
    "title":"Prof. Gregg trivia"
}

In response to the POST request the server will send back an object representing the newly created quiz:

{
    "id": 2,
    "title": "Prof. Gregg trivia",
    "publisher": 1
}

The most important property in the object that the server returns is the id. This is the id number for the newly created quiz. You will need this later when you want to post questions for this quiz.

I have entered the LU id numbers for everyone in this course into the server's database, so only students who are registered for the course will be able to post quizzes and questions.

To post a question to a quiz you have created, POST to the URL

https://cmsc106.net/trivia/questions?publisher=<luid>

In the body of the post you need to provide a JSON object with properties quiz, question, options, and answer. For example:

{
    "quiz":2,
    "question":"Where did I go to college?",
    "options":"Wisconsin,Texas,California",
    "answer":2
}

The quiz property is the id number of the quiz you are posting the question to. You will only be able to post questions to quizzes that you have created. Questions you post can have any number of possible responses. To make the list of options that users will be able to select from put a comma separated list of the options in the options property of the object you post. The answer property is the number of the correct response. Note that this a 1-based number.

If you make a mistake while setting up a question for a quiz, you can delete that question by sending a DELETE request to the URL

https://cmsc106.net/trivia/questions/<id>?publisher=<luid>

where <id> is the id number of the question you want to delete. Note that the <luid> you provide for this request must match the Lawrence ID number of the user who posted the question in the first place.

To see a list of all available quizzes on the server, do a GET with URL

https://cmsc106.net/trivia/quizzes

To see a list of all the questions for a particular quiz, do a GET with the URL

https://cmsc106.net/trivia/questions?quiz=<id>

with <id> replaced by the id number of the quiz you want questions for.

Users and posting answers

The trivia back end server uses essentially the same logic as the directory example to handle registering users and logging in existing users. To create a new user on the server POST to the URL

https://cmsc106.net/trivia/users

The body of the post must contain a JSON object with properties name and password. For example,

{
    "name":"big_genius",
    "password":"hello"
}

If a user with this user name already exists, the server will respond with an error code. If the post was successful, the server will send back the key string for the newly created user.

To log an existing user in to the system do a GET with URL

https://cmsc106.net/trivia/users?user=<name>&password=<pwd>

with <name> replaced with the user name and <pwd> replaced with the password. If the user name or password are incorrect the server will respond with an error code. If the login is correct the server will send back the key for the user in the body of the reply.

To post an answer for a trivia question back to the server, do a POST with the URL

https://cmsc106.net/trivia/answers/<key>

with <key> replaced with the key of the currently logged in user. In the body of the POST put an object with properties question and response, where question is the id number of the question the user is answering and response is the option number for the option the user chose. (Remember that options are numbered starting at 1). For example,

{
    "question":1,
    "response":3
}

To prevent cheating, the server will allow users to post only one answer to each trivia question. If you try posting an answer for a question that the user has already answered the server will respond with an error code.

The server will maintain a leaderboard listing all of the users who have answered questions and how many questions they have answered correctly. To get the leaderboard list do a GET with the URL

https://cmsc106.net/trivia/leaders

Example code

Click this link to download the final version of the Directory app. In this example you will find lots of example code to show you how to implement GET, POST, and DELETE requests. You will also find code that demonstrates how to do error checking in server interactions.