📝 README and inline docs
This commit is contained in:
parent
d533a879ef
commit
c61300c6b8
14 changed files with 139 additions and 24 deletions
|
|
@ -5,16 +5,22 @@ import (
|
|||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// file points the the sqlite file.
|
||||
const file = "./govote.db"
|
||||
|
||||
// db references the initialized sqlite connection.
|
||||
var db *sql.DB
|
||||
|
||||
// init initialized the database layer
|
||||
func init() {
|
||||
var err error
|
||||
if db, err = sql.Open("sqlite3", file); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// initialize the sql schema
|
||||
initCreateTables(db)
|
||||
|
||||
// initialize all prepared statements
|
||||
initStmts(db)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"code.c-base.org/baccenfutter/govote/voting/vote"
|
||||
)
|
||||
|
||||
// NewVoting writes a new voting into the store.
|
||||
func NewVoting(
|
||||
id string,
|
||||
r string,
|
||||
|
|
@ -27,6 +28,7 @@ func NewVoting(
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetVoting takes an id and reads and returns the voting with that ID from the store.
|
||||
func GetVoting(id string) (*voting.Voting, error) {
|
||||
result := votingSelect.QueryRow(id)
|
||||
if result == nil {
|
||||
|
|
@ -73,6 +75,7 @@ func GetVoting(id string) (*voting.Voting, error) {
|
|||
return v, nil
|
||||
}
|
||||
|
||||
// PlaceVote writes an individual vote to the store.
|
||||
func PlaceVote(id, votingID, elector string, choice vote.Choice) error {
|
||||
if _, err := voteInsert.Exec(id, votingID, elector, choice.String()); err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package store
|
|||
|
||||
import "database/sql"
|
||||
|
||||
// References to all available prepared statements.
|
||||
var (
|
||||
votingInsert *sql.Stmt
|
||||
votingSelect *sql.Stmt
|
||||
|
|
@ -10,6 +11,8 @@ var (
|
|||
voteSelect *sql.Stmt
|
||||
)
|
||||
|
||||
// initCreateTables takes an sql connection and creates all tables if they
|
||||
// don't yet exist.
|
||||
func initCreateTables(db *sql.DB) {
|
||||
var err error
|
||||
createTables := `
|
||||
|
|
@ -41,6 +44,8 @@ func initCreateTables(db *sql.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
// initStmts takes an initialized sql connection and initializes all prepared
|
||||
// statements.
|
||||
func initStmts(db *sql.DB) {
|
||||
initStmtVotingInsert(db)
|
||||
initStmtVotingSelect(db)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue