Rename is_anonymous to are_voters_known

This commit is contained in:
2024-12-30 15:37:23 +01:00
parent c8413eaff8
commit 9e96be5ff2
6 changed files with 23 additions and 23 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(
!request.IsAnonymous || (request.IsAnonymous && *request.MaxVoters > 0),
!request.AreVotersKnown || (request.AreVotersKnown && *request.MaxVoters > 0),
"maxVoters",
"must be greater than 0 for anonymous elections",
"must be greater than 0 when voters are known",
)
if !request.Valid() {

View File

@ -11,12 +11,12 @@ import (
var (
validCreateElectionRequest = api.CreateElectionRequest{
Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Now().Add(24 * time.Hour),
IsAnonymous: false,
MaxVoters: nil,
Name: "Guy of the year",
Tokens: 100,
Choices: []string{"Gandhi", "Buddha"},
ExpiresAt: time.Now().Add(24 * time.Hour),
AreVotersKnown: false,
MaxVoters: nil,
Name: "Guy of the year",
Tokens: 100,
} // TODO: try to find a way to generate test data
)

View File

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

View File

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

View File

@ -6,14 +6,14 @@ import (
)
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 {
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
panic("implement me")
}

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 election is anonymous
max_voters INTEGER, -- mandatory 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))
@ -23,7 +23,7 @@ CREATE TABLE choices (
);
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,
PRIMARY KEY (identity, election_id),
FOREIGN KEY (election_id) REFERENCES elections (id)