Incomplete implementation of createVotes

This commit is contained in:
2025-01-02 19:24:32 +01:00
parent 9efe9a3537
commit 0229f78976
9 changed files with 136 additions and 13 deletions

View File

@ -30,11 +30,13 @@ type CreateElectionResponse struct {
// CreateVotesRequest defines model for CreateVotesRequest.
type CreateVotesRequest struct {
Choices *[]struct {
ChoiceText *string `json:"choiceText,omitempty"`
Tokens *int `json:"tokens,omitempty"`
} `json:"choices,omitempty"`
ElectionId *int `json:"electionId,omitempty"`
Choices []struct {
ChoiceText string `json:"choiceText"`
Tokens int `json:"tokens"`
} `json:"choices"`
ElectionId int `json:"electionId"`
// VoterIdentity Must be filled if election has known voters
VoterIdentity *string `json:"voterIdentity,omitempty"`
}

View File

@ -3,7 +3,7 @@ CREATE TABLE elections (
name TEXT NOT NULL,
tokens INTEGER NOT NULL,
are_voters_known INTEGER NOT NULL,
max_voters INTEGER, -- mandatory when voters are known
max_voters INTEGER NOT NULL, -- must be greater than 0 when voters are known
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at DATETIME NOT NULL,
CHECK (are_voters_known = 0 OR (are_voters_known = 1 AND max_voters IS NOT NULL AND max_voters >= 1))

View File

@ -2,17 +2,30 @@ package models
import (
"database/sql"
"fmt"
"time"
)
type ElectionModelInterface interface {
Insert(name string, tokens int, areVotersKnown bool, maxVoters int, Choices []string, ExpiresAt time.Time) (int, error)
GetById(id int) (*Election, error)
}
type ElectionModel struct {
DB *sql.DB
}
type Election struct {
ID int
Name string
Tokens int
AreVotersKnown bool
MaxVoters int
CreatedAt string
ExpiresAt string
Choices []string
}
func (e *ElectionModel) Insert(name string, tokens int, areVotersKnown bool, maxVoters int, choices []string, expiresAt time.Time) (int, error) {
tx, err := e.DB.Begin()
if err != nil {
@ -53,3 +66,50 @@ func (e *ElectionModel) Insert(name string, tokens int, areVotersKnown bool, max
return int(electionID), nil
}
func (e *ElectionModel) GetById(id int) (*Election, error) {
query := `
SELECT id, name, tokens, are_voters_known, max_voters, created_at, expires_at
FROM elections
WHERE id = ?
`
row := e.DB.QueryRow(query, id)
election := &Election{}
var areVotersKnown int
err := row.Scan(&election.ID, &election.Name, &election.Tokens, &areVotersKnown, &election.MaxVoters, &election.CreatedAt, &election.ExpiresAt)
if err != nil {
return nil, err
}
election.AreVotersKnown = areVotersKnown == 1
// Retrieve choices for the election
queryChoices := `
SELECT text
FROM choices
WHERE election_id = ?
`
rows, err := e.DB.Query(queryChoices, id)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var choice string
if err := rows.Scan(&choice); err != nil {
return nil, err
}
election.Choices = append(election.Choices, choice)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over choices for election ID %d: %v", id, err)
}
return election, nil
}

View File

@ -36,6 +36,10 @@ func (v *Validator) CheckField(ok bool, key, message string) {
}
}
func Blank(value string) bool {
return strings.TrimSpace(value) == ""
}
func NotBlank(value string) bool {
return strings.TrimSpace(value) != ""
}