CoderHack is a Spring Boot application that gamifies user engagement by assigning scores and badges based on their performance. It leverages a MongoDB database to manage users and their achievements effectively.
- CRUD Operations: Create, Read, Update, and Delete user data.
- Score Tracking: Dynamically update user scores with validation.
- Badge System: Automatically assign badges based on user performance:
Code Ninja
: Score between 1–30.Code Champ
: Score between 30–60.Code Master
: Score above 60.
- Sorted Leaderboard: Retrieve all users sorted by scores in descending order.
- Backend: Spring Boot
- Database: MongoDB
- Testing: JUnit, Mockito
- Clone the Repository:
git clone https://github.com/geetanjaligit/CoderHack.git
cd CoderHack
- Setup MongoDB:
- Ensure MongoDB is installed and running locally on
localhost:27017
. - Default database:
Coderhackdb
.
- Configure Application Properties:
- Edit
src/main/resources/application.properties
if needed:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=Coderhackdb
spring.data.mongodb.auto-index-creation=true
- Run the Application:
./mvnw spring-boot:run
- API Endpoints:
- Get All Users:
GET /users
- Get User by ID:
GET /users/id/{userId}
- Create User:
POST /users
- Update User Score:
PUT /users/id/{userId}
- Delete User:
DELETE /users/{userId}
- Controller: Manages API endpoints.
UserController.java
- Service: Handles business logic.
UserService.java
UserScoreComparator.java
- Repository: Interfaces with MongoDB.
UserRepository.java
- Entity: Defines the data model.
User.java
- Testing: Ensures reliability with JUnit and Mockito.
UserServiceTest.java
POST /users
{
"id": "101",
"username": "John",
"score": 0,
"badges": []
}
PUT /users/id/101
Body: 40
Response:
{
"id": "101",
"username": "John",
"score": 40,
"badges": ["Code Champ"]
}
Run unit tests with:
./mvnw test
Sample Test Cases:
- Verify badge assignment logic.
- Validate CRUD operations.
- Test for invalid score inputs.