Compare commits
2 Commits
f1a951ce81
...
9e96be5ff2
Author | SHA1 | Date | |
---|---|---|---|
9e96be5ff2
|
|||
c8413eaff8
|
@ -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() {
|
||||
|
@ -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
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"`
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -2,11 +2,11 @@ CREATE TABLE elections (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
tokens INTEGER NOT NULL,
|
||||
is_anonymous INTEGER NOT NULL,
|
||||
max_voters INTEGER, -- mandatory when election is anonymous
|
||||
are_voters_known INTEGER NOT NULL,
|
||||
max_voters INTEGER, -- mandatory when voters are known
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
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
|
||||
@ -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)
|
||||
|
Reference in New Issue
Block a user