Add migrations and continue implementing election insertion

This commit is contained in:
2024-12-30 20:41:28 +01:00
parent 9e96be5ff2
commit 29fb5880e3
9 changed files with 138 additions and 164 deletions

View File

@ -26,7 +26,7 @@ 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.AreVotersKnown || (request.AreVotersKnown && *request.MaxVoters > 0),
!(request.AreVotersKnown && request.MaxVoters == nil),
"maxVoters",
"must be greater than 0 when voters are known",
)
@ -36,5 +36,13 @@ func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
return
}
_, err = app.elections.Insert(request.Name, request.Tokens, request.AreVotersKnown, request.MaxVoters, request.Choices, request.ExpiresAt)
if err != nil {
app.serverError(w, r, err)
return
}
//TODO: if voters are known, generate voters and write them in the response (change openapi as well)
w.Write([]byte("TODO"))
}

View File

@ -9,6 +9,7 @@ import (
)
func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) {
app.logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
var response = api.ErrorResponse{
Code: http.StatusInternalServerError,

View File

@ -3,10 +3,12 @@
package main
import (
db2 "code.dlmw.ch/dlmw/qv/internal/db"
"code.dlmw.ch/dlmw/qv/internal/models"
"database/sql"
"github.com/go-playground/form/v4"
_ "github.com/mattn/go-sqlite3"
stdlib "github.com/multiprocessio/go-sqlite3-stdlib"
"log/slog"
"net/http"
"os"
@ -22,6 +24,7 @@ type application struct {
var addr = ":8080"
func main() {
stdlib.Register("sqlite_math_functions")
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
db, err := openDb()
@ -30,6 +33,11 @@ func main() {
os.Exit(1)
}
defer db.Close()
err = db2.RunMigrations(db)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
formDecoder := form.NewDecoder()