📝 README and inline docs

This commit is contained in:
Brian Wiborg 2024-05-14 10:26:48 +02:00
parent d533a879ef
commit c61300c6b8
No known key found for this signature in database
GPG key ID: BE53FA9286B719D6
14 changed files with 139 additions and 24 deletions

View file

@ -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)
}

View file

@ -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

View file

@ -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)