Generate voter data for known elections and simplify MaxVoters (0 = no maximum)
This commit is contained in:
@ -13,16 +13,21 @@ import (
|
||||
|
||||
// CreateElectionRequest defines model for CreateElectionRequest.
|
||||
type CreateElectionRequest struct {
|
||||
AreVotersKnown bool `json:"are_voters_known"`
|
||||
AreVotersKnown bool `json:"areVotersKnown"`
|
||||
Choices []string `json:"choices"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
ExpiresAt time.Time `json:"expiresAt"`
|
||||
|
||||
// MaxVoters Required when voters are known
|
||||
MaxVoters *int `json:"max_voters"`
|
||||
// MaxVoters Must be greater than 0 when voters are known; 0 = no limit
|
||||
MaxVoters int `json:"maxVoters"`
|
||||
Name string `json:"name"`
|
||||
Tokens int `json:"tokens"`
|
||||
}
|
||||
|
||||
// CreateElectionResponse defines model for CreateElectionResponse.
|
||||
type CreateElectionResponse struct {
|
||||
VoterIdentities *[]string `json:"voterIdentities,omitempty"`
|
||||
}
|
||||
|
||||
// ErrorResponse defines model for ErrorResponse.
|
||||
type ErrorResponse struct {
|
||||
// Code Machine-readable error code
|
||||
|
@ -2,22 +2,21 @@ 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)
|
||||
Insert(name string, tokens int, areVotersKnown bool, maxVoters int, Choices []string, ExpiresAt time.Time) (int, error)
|
||||
}
|
||||
|
||||
type ElectionModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func (e *ElectionModel) Insert(name string, tokens int, areVotersKnown bool, maxVoters *int, choices []string, expiresAt time.Time) (int, error) {
|
||||
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 {
|
||||
return 0, fmt.Errorf("begin transaction: %w", err)
|
||||
return 0, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
|
36
internal/models/voters.go
Normal file
36
internal/models/voters.go
Normal file
@ -0,0 +1,36 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type VoterModelInterface interface {
|
||||
Insert(identity string, electionID int) (int, error)
|
||||
}
|
||||
|
||||
type VoterModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func (v *VoterModel) Insert(identity string, electionID int) (int, error) {
|
||||
tx, err := v.DB.Begin()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
result, err := tx.Exec(`
|
||||
INSERT INTO voters (identity, election_id)
|
||||
VALUES (?, ?)`,
|
||||
identity, electionID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
voterId, err := result.LastInsertId()
|
||||
return int(voterId), nil
|
||||
}
|
Reference in New Issue
Block a user