Overview
TheBallotRepository class handles all database operations for ballots in the Consensus e-voting platform. It implements the IBallotRepository interface and uses SQLite for data persistence.
Constructor
Optional database instance. If not provided, uses the singleton DatabaseConnection instance.
Methods
save
Persists a new ballot to the database.The ballot entity to save to the database. Preferences are stored as JSON.
No return value. Throws an error if the save operation fails.
findById
Retrieves a ballot by its unique identifier.The unique identifier of the ballot to retrieve.
Returns the Ballot entity if found, or null if no ballot exists with the given ID.
findByElectionId
Retrieves all ballots for a specific election.The unique identifier of the election.
Array of all ballots cast in the specified election. Returns empty array if no ballots found.
countByElectionId
Counts the total number of ballots cast in a specific election.The unique identifier of the election.
The total count of ballots cast in the election.
countByCandidate
Counts the number of ballots where a specific candidate is the first preference.The unique identifier of the election.
The unique identifier of the candidate.
The count of ballots where the candidate is listed as the first preference (index 0 in the preferences array).
Related Types
Ballot
The Ballot entity with the following properties:ballotID: string- Unique identifier (readonly)electionID: string- ID of the election this ballot belongs to (readonly)preferences: string[]- Array of candidate IDs in preference order (readonly)- For FPTP elections: single candidate ID
- For preferential elections: multiple candidate IDs ranked by preference
castAt: Date- Timestamp when the ballot was cast (readonly, defaults to current time)
Notes
- Ballot preferences are stored as JSON in the database and parsed when retrieved
- The
countByCandidatemethod uses SQLite’sjson_extractfunction to query the first preference - All ballot properties are readonly to ensure ballot integrity after casting