This application is a Spring Boot-based RESTful quiz app where users can:
- Add problems to the database
- Retrieve unanswered problems
- Submit answers
- View submission results
- Register and manage users
It uses Spring Security for authentication, H2 as the database, and follows clean architectural practices. 💡
- Add Problems: Add multiple quiz problems in JSON format.
- Retrieve All Problems: View all the problems available in the database.
- Retrieve Unanswered Problem: Get a random unanswered problem.
- Submit Answers: Submit answers to the problems and receive results.
- Register Users: Users can register with a username and a password.
- View Registered Users: Retrieve all registered users (for admin use).
- Check if the server is running.
- Clone the repository and navigate to the project directory.
- Run the application:
- In the project directory, run the following command:
mvn spring-boot:run
- In the project directory, run the following command:
- Access the H2 Console (optional):
- URL:
http://localhost:8080/h2-console
- JDBC URL:
jdbc:h2:file:./data/quizdb
- Username:
sa
- Password: (leave empty)
- URL:
Since Spring Security is enabled, you'll need to log in to access certain endpoints.
to register a new user:
- POST to
/users/register
with a JSON body:{ "email": "newuser@example.com", "password": "newpassword" }
You can use Postman or any other API testing tool to interact with the endpoints. Below are instructions for testing common operations.
- POST
/problems/add
- Request Body (Example):
[ { "problemStatement": "What is the capital of France?", "correctAnswer": "Paris", "options": ["Berlin", "Madrid", "Paris", "Lisbon"] }, { "problemStatement": "What is 5 + 7?", "correctAnswer": "12", "options": ["10", "12", "15", "14"] } ]
- POST
/quizzes/submit
- Request Body (Example):
{ "answers": [ { "problemId": 1, "selectedAnswer": "4" }, { "problemId": 2, "selectedAnswer": "8" } ] }
HTTP Method | Endpoint | Description |
---|---|---|
POST | /problems/add | Add a list of problems to the database. |
GET | /problems | Retrieve all problems. |
GET | /quizzes/problem | Get an unanswered problem. |
POST | /quizzes/submit | Submit an answer to a problem. |
POST | /users/register | Register a new user. |
GET | /users | View all registered users. |
GET | /quizzes/health-check | Check if the server is running. |
1. **Register User**
- **Endpoint**: /users/register
- **HTTP Method**: POST
- **Request Body**:
{
"email": "user@example.com",
"password": "password123"
}
- **Response**:
{
"id": 1,
"username": "user1"
}
2. **Add Problems**
- **Endpoint**: /problems/add
- **HTTP Method**: POST
- **Request Body**:
[
{
//autogenerated
"id": 1,
"problemStatement": "What is 2 + 2?",
"correctAnswer": "4"
},
{
//autogenerated
"id": 2,
"problemStatement": "What is 3 + 5?",
"correctAnswer": "8"
}
]
- **Response**:
[
{
"id": 1,
"problemStatment": "What is 2 + 2?",
"correctAnswer": "4"
},
{
"id": 2,
"problemStatment": "What is 3 + 5?",
"correctAnswer": "8"
}
]
3. **Submit Answers**
- **Endpoint**: /quizzes/submit
- **HTTP Method**: POST
- **Request Body**:
{
"answers": [
{
"problemId": 1,
"selectedAnswer": "4"
},
{
"problemId": 2,
"selectedAnswer": "8"
}
]
}
- **Response**:
{
"totalQuestions": 2,
"correctAnswers": 2,
"score": 100
}
4. **Submit Answers (Submissions Endpoint)**
- **Endpoint**: /submissions
- **HTTP Method**: POST
- **Request Body**:
{
"answers": [
{
"problemId": 1,
"selectedAnswer": "4"
},
{
"problemId": 2,
"selectedAnswer": "8"
}
]
}
- **Response**:
{
"totalQuestions": 2,
"correctAnswers": 2,
"score": 100
}
To ensure the server is running properly, GET /quizzes/health-check
to get the server status.
POST /problems/add
[
{
"problemStatement": "What is the capital of France?",
"correctAnswer": "Paris",
"options": ["Berlin", "Madrid", "Paris", "Lisbon"]
},
{
"problemStatement": "What is 5 + 7?",
"correctAnswer": "12",
"options": ["10", "12", "15", "14"]
}
]
POST /quizzes/submit
{
"quizId": 1,
"answers": [
{
"problemId": 101,
"selectedAnswer": "Paris"
},
{
"problemId": 102,
"selectedAnswer": "12"
}
]
}
- ProblemController: Manages adding and retrieving quiz problems.
- QuizController: Handles retrieving unanswered problems and submitting answers.
- UserController: Manages user registration and retrieval.
- SubmissionController: Evaluates submissions.
- ProblemService: Business logic for problems.
- QuizService: Business logic for quizzes and submissions.
- UserService: Handles user-related operations.
- Problem: Represents a quiz problem.
- SubmissionRequest: Input format for answer submissions.
- SubmissionResponse: Output format for evaluated submissions.
- User: Represents a registered user.
- Implement user authentication and authorization.
- Add leaderboards for users based on quiz scores.
- Expand the question types to include multiple-choice, true/false, and short answers.
- Enable persistent database storage.
For any questions or feedback, feel free to contact the developer:
- Piyush at @piyushg0707.03@gmail.com 🎯