diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 4a15ff3..463b8dd 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -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() { diff --git a/cmd/web/handlers_test.go b/cmd/web/handlers_test.go index f7176c5..0af302d 100644 --- a/cmd/web/handlers_test.go +++ b/cmd/web/handlers_test.go @@ -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 ) diff --git a/cmd/web/openapi.yml b/cmd/web/openapi.yml index dbe238a..f2ab383 100644 --- a/cmd/web/openapi.yml +++ b/cmd/web/openapi.yml @@ -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 diff --git a/internal/generated.go b/internal/generated.go index 0cd2266..a772d92 100644 --- a/internal/generated.go +++ b/internal/generated.go @@ -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"` diff --git a/internal/models/elections.go b/internal/models/elections.go index 335c65e..127e5ca 100644 --- a/internal/models/elections.go +++ b/internal/models/elections.go @@ -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") } diff --git a/sql/init.sql b/sql/init.sql index 124069c..3c4d1de 100644 --- a/sql/init.sql +++ b/sql/init.sql @@ -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)