A comprehensive RESTful API for managing elections, built with Spring Boot and MongoDB. This system enables organizations to create, manage, and conduct elections with user registration, voting, and result tracking capabilities.
- User Management: Register users and retrieve user information with email validation
- Election Management: Create, start, end, and delete elections with date validation
- Voting System: Secure vote casting with duplicate vote prevention
- Results & Analytics: View election results and determine winners with tie handling
- Error Handling: Comprehensive exception handling for various scenarios (invalid dates, unauthorized access, etc.)
- RESTful API: Well-structured REST endpoints with proper HTTP status codes
- Data Persistence: MongoDB integration for scalable data storage
- Java 17+
- Spring Boot 4.1.0-M3
- Spring Web (REST controllers)
- Spring Data MongoDB (data persistence)
- MongoDB (NoSQL database)
- Lombok (boilerplate code reduction)
- JUnit 5 and Mockito (unit testing)
- Maven (build tool)
- Java 17 or higher
- Maven 3.6+
- MongoDB 7.0+ (running locally or accessible via connection string)
-
Clone the repository:
git clone <repository-url> cd Election
-
Build the project:
mvn clean compile
-
Ensure MongoDB is running:
# For local MongoDB installation mongodOr configure connection to a remote MongoDB instance.
-
Run the application:
mvn spring-boot:run
The application will start on
http://localhost:8080by default.
POST /api/users- Register a new userGET /api/users/{id}- Get user by IDGET /api/users/email/{email}- Get user by email
POST /api/elections- Create a new electionPATCH /api/elections/{id}/start?userId={userId}- Start an electionPATCH /api/elections/{id}/end?userId={userId}- End an electionDELETE /api/elections/{id}?userId={userId}- Delete an electionGET /api/elections- Get all elections
POST /api/votes- Cast a vote
GET /api/results/{electionId}- Get election resultsGET /api/results/{electionId}/winner- Get election winner
Run the tests using Maven:
mvn testsrc/
├── main/java/election/
│ ├── controllers/ # REST controllers (@RestController)
│ ├── data/
│ │ ├── models/ # Domain entities (@Document)
│ │ └── repositories/ # Data access (@Repository)
│ ├── dtos/
│ │ ├── requests/ # Request DTOs
│ │ └── responses/ # Response DTOs
│ ├── exceptions/ # Custom exceptions
│ ├── services/ # Business logic (@Service)
│ └── utils/ # Utility classes
├── test/java/services/ # Unit tests
└── main/resources/ # Configuration files (optional)
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run tests and ensure they pass
- Submit a pull request
This project is licensed under the MIT License.