Compare commits

..

2 Commits

Author SHA1 Message Date
9e96be5ff2 Rename is_anonymous to are_voters_known 2024-12-30 15:37:23 +01:00
c8413eaff8 Rename is_anonymous to are_voters_known 2024-12-30 15:34:29 +01:00
6 changed files with 25 additions and 25 deletions

View File

@ -26,9 +26,9 @@ func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
request.CheckField(validator.GreaterThan(len(request.Choices), 1), "choices", "there must be more than 1 choice") request.CheckField(validator.GreaterThan(len(request.Choices), 1), "choices", "there must be more than 1 choice")
request.CheckField( request.CheckField(
!request.IsAnonymous || (request.IsAnonymous && *request.MaxVoters > 0), !request.AreVotersKnown || (request.AreVotersKnown && *request.MaxVoters > 0),
"maxVoters", "maxVoters",
"must be greater than 0 for anonymous elections", "must be greater than 0 when voters are known",
) )
if !request.Valid() { if !request.Valid() {

View File

@ -13,7 +13,7 @@ var (
validCreateElectionRequest = api.CreateElectionRequest{ validCreateElectionRequest = api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha"}, Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Now().Add(24 * time.Hour), ExpiresAt: time.Now().Add(24 * time.Hour),
IsAnonymous: false, AreVotersKnown: false,
MaxVoters: nil, MaxVoters: nil,
Name: "Guy of the year", Name: "Guy of the year",
Tokens: 100, Tokens: 100,

View File

@ -49,7 +49,7 @@ components:
- id - id
- name - name
- tokens - tokens
- is_anonymous - are_voters_known
- expires_at - expires_at
properties: properties:
id: id:
@ -62,13 +62,13 @@ components:
tokens: tokens:
type: integer type: integer
minimum: 0 minimum: 0
is_anonymous: are_voters_known:
type: boolean type: boolean
max_voters: max_voters:
type: integer type: integer
minimum: 1 minimum: 1
nullable: true nullable: true
description: Required when election is anonymous description: Required when voters are known
created_at: created_at:
type: string type: string
format: date-time format: date-time
@ -107,7 +107,7 @@ components:
identity: identity:
type: string type: string
minLength: 1 minLength: 1
description: When election is anonymous, passcodes will be pre-generated description: When voters are known, passcodes will be pre-generated
election_id: election_id:
type: integer type: integer
format: int64 format: int64
@ -149,7 +149,7 @@ components:
required: required:
- name - name
- tokens - tokens
- is_anonymous - are_voters_known
- expires_at - expires_at
- choices - choices
properties: properties:
@ -159,13 +159,13 @@ components:
tokens: tokens:
type: integer type: integer
minimum: 0 minimum: 0
is_anonymous: are_voters_known:
type: boolean type: boolean
max_voters: max_voters:
type: integer type: integer
minimum: 1 minimum: 1
nullable: true nullable: true
description: Required when election is anonymous description: Required when voters are known
expires_at: expires_at:
type: string type: string
format: date-time format: date-time

View File

@ -13,11 +13,11 @@ import (
// CreateElectionRequest defines model for CreateElectionRequest. // CreateElectionRequest defines model for CreateElectionRequest.
type CreateElectionRequest struct { type CreateElectionRequest struct {
AreVotersKnown bool `json:"are_voters_known"`
Choices []string `json:"choices"` Choices []string `json:"choices"`
ExpiresAt time.Time `json:"expires_at"` ExpiresAt time.Time `json:"expires_at"`
IsAnonymous bool `json:"is_anonymous"`
// MaxVoters Required when election is anonymous // MaxVoters Required when voters are known
MaxVoters *int `json:"max_voters"` MaxVoters *int `json:"max_voters"`
Name string `json:"name"` Name string `json:"name"`
Tokens int `json:"tokens"` Tokens int `json:"tokens"`

View File

@ -6,14 +6,14 @@ import (
) )
type ElectionModelInterface interface { type ElectionModelInterface interface {
Insert(name string, tokens int, isAnonymous 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 { type ElectionModel struct {
DB *sql.DB DB *sql.DB
} }
func (e *ElectionModel) Insert(name string, tokens int, isAnonymous 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) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }

View File

@ -2,11 +2,11 @@ CREATE TABLE elections (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL, name TEXT NOT NULL,
tokens INTEGER NOT NULL, tokens INTEGER NOT NULL,
is_anonymous INTEGER NOT NULL, are_voters_known INTEGER NOT NULL,
max_voters INTEGER, -- mandatory when election is anonymous max_voters INTEGER, -- mandatory when voters are known
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at DATETIME NOT NULL, expires_at DATETIME NOT NULL,
CHECK (is_anonymous = 0 OR (is_anonymous = 1 AND max_voters IS NOT NULL AND max_voters >= 1)) CHECK (are_voters_known = 0 OR (are_voters_known = 1 AND max_voters IS NOT NULL AND max_voters >= 1))
); );
CREATE TRIGGER prevent_created_at_update_election CREATE TRIGGER prevent_created_at_update_election
@ -23,7 +23,7 @@ CREATE TABLE choices (
); );
CREATE TABLE voters ( CREATE TABLE voters (
identity TEXT NOT NULL, -- when election is anonymous, passcodes will be pre-generated identity TEXT NOT NULL, -- when voters are known, passcodes will be pre-generated
election_id INTEGER NOT NULL, election_id INTEGER NOT NULL,
PRIMARY KEY (identity, election_id), PRIMARY KEY (identity, election_id),
FOREIGN KEY (election_id) REFERENCES elections (id) FOREIGN KEY (election_id) REFERENCES elections (id)