Skip to content

Web Quiz Engine

Karthik Chennupati edited this page Jul 28, 2020 · 5 revisions

About

On the Internet, you can often find sites where you need to answer some questions. It can be educational sites, sites with psychological tests, job search services, or just entertaining sites like web quests. The common thing for them is the ability to answer questions (or quizzes) and then see some results. In this project, we developed a multi-user web service for creating and solving such quizzes. The following sections describe the service.

Description

Web Quiz Engine is a RESTful quiz service. A user can:

  1. Register to the service
  2. Get all quizzes in the service (using pagination)
  3. Get a specific quiz
  4. Post a new quiz to the service
  5. Solve a quiz
  6. Get all completed quizzes (using pagination)
  7. Update a self-created quiz
  8. Delete a self-created quiz

  2 to 8 requires the user to be authenticated.
Authentication is implemented using HTTP BasicAuth. All of the data is communicated using JSON only.
Passwords are encrypted using BCrypt encoder before storing into database.

API

1. Register

Registers email and password with the service.
POST email and password to /api/register

Response
Status Code Response data
200
400 Email is already taken
400 Password must be at least 5 characters long
400 Given string is not a valid email
Example

POST to /api/register with content:

{
    "email": "[email protected]",
    "password": "something"
}
Response

Responds with status code 200 without any content.

2. Get all quizzes

Retrieves quizzes from the service

GET to /api/quizzes with optional query parameters page and pageSize.
If query parameters are not provided, default values page = 0 and pageSize = 10 are used.

Example

GET to /api/quizzes?page=0&pageSize=5 with auth credentials

Response

Paginated response with given parameters and status code 200

{
    "content": [
        {
            "id": 1,
            "title": "Quadrupedal animals",
            "text": "Which of the following animals have 4 legs?",
            "options": [
                "cat",
                "snake",
                "hen",
                "dog",
                "human"
            ]
        },
        {
            "id": 2,
            "title": "Indian cities",
            "text": "Which of the following cities are located in India?",
            "options": [
                "California",
                "Bangalore",
                "Hyderabad",
                "Tokyo"
            ]
        }
    ],
    "pageable": {
        "sort": {
            "sorted": false,
            "unsorted": true,
            "empty": true
        },
        "offset": 0,
        "pageSize": 5,
        "pageNumber": 0,
        "paged": true,
        "unpaged": false
    },
    "last": true,
    "totalPages": 1,
    "totalElements": 2,
    "size": 5,
    "sort": {
        "sorted": false,
        "unsorted": true,
        "empty": true
    },
    "number": 0,
    "numberOfElements": 2,
    "first": true,
    "empty": false
}

3. Get a specific quiz

Retrieves a specific quiz from the service.
GET to /api/quizzes/{id}

Status Code Response data
200 Quiz with id
404 Requested quiz with id: {id} is not present
Example

GET to /api/quizzes/1 with auth credentials

Response

Quiz with id = 1 and status code 200

{
    "id": 1,
    "title": "Quadrupedal animals",
    "text": "Which of the following animals have 4 legs?",
    "options": [
        "cat",
        "snake",
        "hen",
        "dog",
        "human"
    ]
}

4. Post a quiz

Posts a quiz to the service
POST quiz to /api/quizzes

title and text must not be blank and options must contain more than one string. 400 will be returned upon failing these constraints. answer can be empty or not provided at all, in which case it is considered to be empty ([], i.e., all options are wrong).

Example

POST to /api/quizzes with auth credentials and content:

{
    "title": "Indian cities",
    "text": "Which of the following cities are located in India?",
    "options": ["California", "Bangalore", "Hyderabad", "Tokyo"],
    "answer": [1, 2]
}
Response

Created quiz with id and status code 200

{
    "id": 2,
    "title": "Indian cities",
    "text": "Which of the following cities are located in India?",
    "options": [
        "California",
        "Bangalore",
        "Hyderabad",
        "Tokyo"
    ]
}

5. Solve a quiz

Solves a quiz
POST answer to /api/quizzes/{id}/solve

Status Code Response data
200 Quiz result
404 Requested quiz with id: {id} is not present
Quiz result

For correct answer:

{
    "success": true,
    "feedback": "Congratulations, you're right!"
}

For wrong answer:

{
    "success": false,
    "feedback": "Wrong answer! Please, try again."
}
Example

POST to /api/quizzes/1/solve with auth credentials and content:

{
    "answer": [0, 3, 4]
}
Response

Quiz result with status code 200

{
    "success": true,
    "feedback": "Congratulations, you're right!"
}

6. Completed quizzes

Retrieves all id's of quizzes completed by this user (email).
GET to /api/quizzes/completed with query parameters page and pageSize.

If query parameters are not provided, default values page = 0 and pageSize = 10 are used.
Response includes multiple successful submissions for the same quiz.

Example

GET to /api/quizzes/completed?page=0&pageSize=10 with auth credentials

Response

Paginated response with given parameters and status code 200

{
    "content": [
        {
            "id": 1,
            "completedAt": "2020-07-28T06:44:07.361+0000"
        },
        {
            "id": 1,
            "completedAt": "2020-07-28T06:40:11.535+0000"
        }
    ],
    "pageable": {
        "sort": {
            "sorted": true,
            "unsorted": false,
            "empty": false
        },
        "offset": 0,
        "pageSize": 10,
        "pageNumber": 0,
        "paged": true,
        "unpaged": false
    },
    "last": true,
    "totalPages": 1,
    "totalElements": 2,
    "size": 10,
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "number": 0,
    "numberOfElements": 2,
    "first": true,
    "empty": false
}

7. Update a quiz

Updates the specified quiz
PUT updated quiz to /api/quizzes/{id}

As with Posting a quiz, title and text must not be blank and options must contain more than one string. 400 will be returned upon failing these constraints. answer can be empty or not provided at all, in which case it is considered to be empty ([], i.e., all options are wrong).

If this user (email) is not the creator of the quiz, status code 403 will be returned. If the quiz with id is not found, a new quiz will be created and that quiz will be returned with status code 201

Status Code Response data
200 Updated Quiz with id
201 Created Quiz with id
403
Example

PUT to /api/quizzes/2 with auth credentials and content:

{
    "title": "Indian cities (Updated)",
    "text": "Which of the following cities are located in India?",
    "options": ["California", "Bangalore", "Hyderabad", "Tokyo", "Vijayawada"],
    "answer": [1, 2, 4]
}
Response

Updated quiz with id and status code 200

{
    "id": 2,
    "title": "Indian cities (Updated)",
    "text": "Which of the following cities are located in India?",
    "options": [
        "California",
        "Bangalore",
        "Hyderabad",
        "Tokyo",
        "Vijayawada"
    ]
}

8. Delete a quiz

Deletes the specified quiz
DELETE to /api/quizzes/{id}

Returns 204 if the quiz is deleted successfully.
If this user (email) is not the creator of the quiz, status code 403 will be returned.

Example

DELETE to /api/quizzes/1 with auth credentials

Response

Responds with status code 204 without any content



2 to 8 requires the user to be authenticated. If the user is not authenticated successfully, either because auth credentials are not sent or because they do not match, the service will respond with 401

Configuration

application.properties

server.port=8080
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
spring.datasource.url=jdbc:h2:file:./quizdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=admin
spring.datasource.password=admin

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

This project was developed as part of Hyperskill's Java Developer Track. Visit hyperskill for more details.