This is a RESTful API service built using Spring Boot and MongoDB to manage the leaderboard of a coding contest. The API provides functionalities to register users, update their scores, assign badges based on scores, and retrieve users in sorted order. The project also includes basic validation and error handling.
- Register Users: Register new participants for the contest.
- Update Score: Update user scores.
- Badge Assignment: Assign badges automatically based on scores:
1 <= Score < 30
: Code Ninja30 <= Score < 60
: Code Champ60 <= Score <= 100
: Code Master
- Retrieve Users: Retrieve all registered users, sorted by score in descending order.
- Deregister Users: Remove users from the leaderboard.
- Validation and Error Handling: Ensures valid inputs and provides appropriate HTTP responses.
- Java 23
- Spring Boot (REST API)
- MongoDB (Database)
- Lombok (for boilerplate code reduction)
- JUnit (for unit testing)
- Gradle (build tool)
Method | Endpoint | Description | Example Request Body |
---|---|---|---|
GET | /users |
Retrieve all registered users | - |
GET | /users/{userId} |
Retrieve details of a specific user | - |
POST | /users |
Register a new user | { "userId": 123, "username": "Anirudh" } |
PUT | /users/{userId} |
Update the score of a specific user | /users/123?score=45 |
DELETE | /users/{userId} |
Deregister a user | - |
- Install Java 23, Gradle, and MongoDB.
- Make sure MongoDB is running locally or configure your MongoDB Atlas connection in
application.properties
.
- Clone the repository:
git clone <repository-url> cd leaderboard-api
- Build and run the project:
mvn clean install mvn spring-boot:run
- The API will be available at:
http://localhost:8080
Request:
POST /users
Request Body:
{
"userId": 123,
"username": "Anirudh"
}
Request:
PUT /users/123?score=45
Request:
GET /users
Request:
DELETE /users/123
HTTP Status | Description |
---|---|
400 | Invalid input data |
404 | User not found |
409 | User already exists |
500 | Internal server error |
This project includes JUnit tests to ensure the correct functionality of the leaderboard system. To run the tests:
.\gradlew test
You can use the following Postman Collection to test the API. Import the collection to Postman using this link:
Leaderboard API Postman Collection
(Replace the link with the actual shareable Postman link)
This project is open-source and available under the MIT License.
-
Open Postman and create a new Collection named Leaderboard API.
-
Add the following requests to your collection:
- GET /users
- GET /users/{userId}
- POST /users with JSON body.
- PUT /users/{userId} with
score
as a query parameter. - DELETE /users/{userId}`.
-
Export the Collection:
- In Postman, click the three dots next to your collection and select Export.
- Choose Collection v2.1 and save it as
LeaderboardAPI.postman_collection.json
.
-
Upload the Collection to a public location (like GitHub or Postman).
- If using GitHub, commit the JSON file to your repository.
- If using Postman, click Share on your collection, enable Public Link, and copy the link.
-
Add the Postman Link to your
README.md
under the Postman Collection section.
This completes the README.md
and Postman setup. Let me know if you encounter any issues!