Add JSON Rankings

This commit is contained in:
Nick Eyre
2014-06-03 22:20:10 -07:00
parent 8c22f7a9ac
commit 2ab1f6f56c
2 changed files with 25 additions and 0 deletions

View File

@@ -12,6 +12,8 @@ import (
"net/http"
"strconv"
"text/template"
"io"
"encoding/json"
)
// Generates a CSV-formatted report of the qualification rankings.
@@ -36,6 +38,28 @@ func RankingsCsvReportHandler(w http.ResponseWriter, r *http.Request) {
}
}
// Generates a JSON-formatted report of the qualification rankings.
func RankingsJSONReportHandler(w http.ResponseWriter, r *http.Request) {
rankings, err := db.GetAllRankings()
if err != nil {
handleWebErr(w, err)
return
}
w.Header().Set("Content-Type", "text/plain")
data, err := json.MarshalIndent(rankings, "", " ")
if err != nil {
handleWebErr(w, err)
return
}
_, err = io.WriteString(w, string(data))
if err != nil {
handleWebErr(w, err)
return
}
}
// Generates a PDF-formatted report of the qualification rankings.
func RankingsPdfReportHandler(w http.ResponseWriter, r *http.Request) {
rankings, err := db.GetAllRankings()

1
web.go
View File

@@ -62,6 +62,7 @@ func newHandler() http.Handler {
router := mux.NewRouter()
router.HandleFunc("/reports/csv/rankings", RankingsCsvReportHandler)
router.HandleFunc("/reports/pdf/rankings", RankingsPdfReportHandler)
router.HandleFunc("/reports/json/rankings", RankingsJSONReportHandler)
router.HandleFunc("/reports/csv/schedule/{type}", ScheduleCsvReportHandler)
router.HandleFunc("/reports/pdf/schedule/{type}", SchedulePdfReportHandler)
router.HandleFunc("/reports/csv/teams", TeamsCsvReportHandler)