A full-stack voting application built with Java (Spring Boot) for the backend and JavaScript for the frontend. The application allows users to manage candidates, voters, and votes in a simple and intuitive way. It provides a RESTful API for backend operations and a dynamic frontend for user interaction.
This project is ideal for learning how to build a CRUD application with a focus on REST APIs, database integration, and frontend-backend communication.
- Project Overview
- Features
- Project Structure
- Database
- API Endpoints
- User Interface
- How to Run
- Screenshots
- License
- Author
This application provides the following functionalities:
- Candidate Management: Add, view, and delete candidates. Each candidate has a name and a vote count.
- Voter Management: Add, view, and filter voters based on their voting status.
- Voting System: Allow voters to cast votes for candidates. Each voter can vote only once.
- Dynamic Frontend: A user-friendly interface built with HTML and JavaScript for interacting with the system.
The backend is powered by Spring Boot, using H2 Database for data storage. The frontend communicates with the backend via RESTful APIs.
- Add new candidates and voters.
- Cast votes for candidates.
- View the list of candidates with their vote counts.
- Filter voters by those who have voted or not voted.
- RESTful API for managing candidates and voters.
- Dynamic frontend with real-time updates.
Voting_app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ ├── org.pejpero.voting_app/
│ │ │ │ ├── controller/ # REST controllers
│ │ │ │ ├── model/ # JPA entities
│ │ │ │ ├── repository/ # Spring Data JPA repositories
│ │ │ │ ├── service/ # Business logic
│ │ ├── resources/
│ │ │ ├── static/ # Frontend files (HTML, CSS, JS)
│ │ │ ├── application.properties
├── README.md # Project documentation
The application uses an embedded H2 database for simplicity. The database contains two main tables:
-
Candidates:
id: Unique identifier.name: Name of the candidate.votesCount: Number of votes received.
-
Voters:
id: Unique identifier.name: Name of the voter.hasVoted: Boolean indicating if the voter has voted.
- POST
/candidates: Add a new candidate. - GET
/candidates: Retrieve all candidates. - GET
/candidates/{candidateId}: Retrieve a specific candidate. - GET
/candidates/votes/{candidateId}: Get the vote count for a specific candidate. - DELETE
/candidates/{candidateId}: Delete a candidate.
@RestController
@RequestMapping("/candidates")
public class CandidateController {
@Autowired
private CandidateRepository repo;
@PostMapping
public ResponseEntity<Candidate> add(@RequestBody Candidate candidate) {
Candidate saved = repo.save(candidate);
return ResponseEntity.ok(saved);
}
@GetMapping("/{candidateId}")
public Candidate getCandidate(@PathVariable Long candidateId) {
return repo.findById(candidateId).orElseThrow(() -> new RuntimeException("Candidate not found"));
}
@DeleteMapping("/{candidateId}")
public ResponseEntity<Void> delete(@PathVariable Long candidateId) {
repo.deleteById(candidateId);
return ResponseEntity.noContent().build();
}
}- POST
/voters: Add a new voter. - GET
/voters: Retrieve all voters. - GET
/voters/{voterId}: Retrieve a specific voter. - POST
/voters/{voterId}/vote/{candidateId}: Cast a vote for a candidate.
fetch("http://localhost:8080/candidates", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "John Doe" })
}).then(response => response.json())
.then(data => console.log(data));fetch("http://localhost:8080/candidates")
.then(response => response.json())
.then(data => console.log(data));The frontend is a simple HTML page with JavaScript for dynamic updates. Key features include:
- Dropdowns for selecting candidates and voters.
- Buttons for adding new candidates and voters.
- Lists displaying candidates and voters with their statuses.
async function loadCandidates() {
const res = await fetch("http://localhost:8080/candidates");
const data = await res.json();
console.log(data);
}-
Clone the repository:
git clone https://github.com/PejperO/Voting_app.git cd Voting_app -
Build the project using Maven:
mvn clean install
-
Run the application:
mvn spring-boot:run
-
Open the application in your browser:
http://localhost:8080
This project is licensed under the MIT License. See the LICENSE file for details.