Implement getElectionResults

This commit is contained in:
2025-01-14 16:52:20 +01:00
parent 13c2693bdb
commit c095e9ae0b
4 changed files with 118 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"math/rand"
"net/http"
"slices"
@ -271,5 +272,41 @@ func (app *application) createVotesHandleUnknownVotersElection(w http.ResponseWr
}
func (app *application) GetElectionResults(w http.ResponseWriter, r *http.Request, id int) {
votes, err := app.votes.GetByElection(id)
if err != nil {
if errors.Is(sql.ErrNoRows, err) {
app.clientError(w, http.StatusNotFound, fmt.Sprintf("couldn't find votes for an election with id %v", id))
return
}
app.serverError(w, r, err)
return
}
results := getResultsFromVotes(votes)
response, err := json.Marshal(api.ElectionResultsResponse{Results: &results})
if err != nil {
app.serverError(w, r, err)
return
}
w.Write(response)
}
func getResultsFromVotes(votes *[]models.Vote) []api.VotesForChoice {
votesForChoice := make(map[string]int)
for _, v := range *votes {
votesForChoice[v.ChoiceText] += int(math.Floor(math.Sqrt(float64(v.Tokens))))
}
results := make([]api.VotesForChoice, 0)
for choice, votes := range votesForChoice {
result := api.VotesForChoice{
Choice: choice,
Votes: votes,
}
results = append(results, result)
}
return results
}

View File

@ -124,6 +124,12 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/ElectionResultsResponse"
400:
description: Request malformed
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
404:
description: Election doesn't exist
content:
@ -205,6 +211,22 @@ components:
ElectionResultsResponse:
type: object
properties:
results:
type: array
items:
$ref: "#/components/schemas/VotesForChoice"
VotesForChoice:
type: object
required:
- choice
- votes
properties:
choice:
type: string
votes:
type: integer
ErrorResponse:
type: object