Add some more tests and implement getElection
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user