Add some more tests and implement getElection

This commit is contained in:
2025-01-17 14:49:51 +01:00
parent 60d1bb382c
commit 410e8f39d3
6 changed files with 236 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package main
import (
api "code.dlmw.ch/dlmw/qv/internal"
"code.dlmw.ch/dlmw/qv/internal/mappers"
"code.dlmw.ch/dlmw/qv/internal/models"
"code.dlmw.ch/dlmw/qv/internal/validator"
"code.dlmw.ch/dlmw/qv/ui"
@ -312,3 +313,23 @@ func getResultsFromVotes(votes *[]models.Vote) []api.VotesForChoice {
return results
}
func (app *application) GetElection(w http.ResponseWriter, r *http.Request, id int) {
election, err := app.elections.GetById(id)
if err != nil {
if errors.Is(sql.ErrNoRows, err) {
app.clientError(w, http.StatusNotFound, fmt.Sprintf("couldn't find election with id %v", id))
return
}
app.serverError(w, r, err)
return
}
response, err := json.Marshal(mappers.ElectionResponse(election))
if err != nil {
app.serverError(w, r, err)
return
}
w.Write(response)
}