mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 13:46:44 -04:00
Added matches JSON dump to API.
This commit is contained in:
40
api.go
40
api.go
@@ -7,14 +7,54 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type MatchWithResult struct {
|
||||
Match
|
||||
Result *MatchResult
|
||||
}
|
||||
|
||||
type RankingWithNickname struct {
|
||||
Ranking
|
||||
Nickname string
|
||||
}
|
||||
|
||||
// Generates a JSON dump of the matches and results.
|
||||
func MatchesApiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
matches, err := db.GetMatchesByType(vars["type"])
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
matchesWithResults := make([]MatchWithResult, len(matches))
|
||||
for i, match := range matches {
|
||||
matchesWithResults[i].Match = match
|
||||
matchResult, err := db.GetMatchResultForMatch(match.Id)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
matchesWithResults[i].Result = matchResult
|
||||
}
|
||||
|
||||
jsonData, err := json.MarshalIndent(matchesWithResults, "", " ")
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = w.Write(jsonData)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Generates a JSON dump of the qualification rankings.
|
||||
func RankingsApiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
rankings, err := db.GetAllRankings()
|
||||
|
||||
31
api_test.go
31
api_test.go
@@ -7,8 +7,39 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMatchesApi(t *testing.T) {
|
||||
clearDb()
|
||||
defer clearDb()
|
||||
db, _ = OpenDatabase(testDbPath)
|
||||
match1 := Match{Type: "qualification", DisplayName: "1", Time: time.Unix(0, 0), Red1: 1, Red2: 2, Red3: 3,
|
||||
Blue1: 4, Blue2: 5, Blue3: 6, Blue1IsSurrogate: true, Blue2IsSurrogate: true, Blue3IsSurrogate: true}
|
||||
match2 := Match{Type: "qualification", DisplayName: "2", Time: time.Unix(600, 0), Red1: 7, Red2: 8, Red3: 9,
|
||||
Blue1: 10, Blue2: 11, Blue3: 12, Red1IsSurrogate: true, Red2IsSurrogate: true, Red3IsSurrogate: true}
|
||||
match3 := Match{Type: "practice", DisplayName: "1", Time: time.Now(), Red1: 6, Red2: 5, Red3: 4,
|
||||
Blue1: 3, Blue2: 2, Blue3: 1}
|
||||
db.CreateMatch(&match1)
|
||||
db.CreateMatch(&match2)
|
||||
db.CreateMatch(&match3)
|
||||
matchResult1 := buildTestMatchResult(match1.Id, 1)
|
||||
db.CreateMatchResult(&matchResult1)
|
||||
|
||||
recorder := getHttpResponse("/api/matches/qualification")
|
||||
assert.Equal(t, 200, recorder.Code)
|
||||
assert.Equal(t, "application/json", recorder.HeaderMap["Content-Type"][0])
|
||||
var matchesData []MatchWithResult
|
||||
err := json.Unmarshal([]byte(recorder.Body.String()), &matchesData)
|
||||
assert.Nil(t, err)
|
||||
if assert.Equal(t, 2, len(matchesData)) {
|
||||
assert.Equal(t, match1.Id, matchesData[0].Match.Id)
|
||||
assert.Equal(t, matchResult1, *(matchesData[0].Result))
|
||||
assert.Equal(t, match2.Id, matchesData[1].Match.Id)
|
||||
assert.Nil(t, matchesData[1].Result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRankingsApi(t *testing.T) {
|
||||
clearDb()
|
||||
defer clearDb()
|
||||
|
||||
1
web.go
1
web.go
@@ -137,6 +137,7 @@ func newHandler() http.Handler {
|
||||
router.HandleFunc("/displays/announcer/websocket", AnnouncerDisplayWebsocketHandler).Methods("GET")
|
||||
router.HandleFunc("/displays/scoring/{alliance}", ScoringDisplayHandler).Methods("GET")
|
||||
router.HandleFunc("/displays/scoring/{alliance}/websocket", ScoringDisplayWebsocketHandler).Methods("GET")
|
||||
router.HandleFunc("/api/matches/{type}", MatchesApiHandler).Methods("GET")
|
||||
router.HandleFunc("/api/rankings", RankingsApiHandler).Methods("GET")
|
||||
router.HandleFunc("/", IndexHandler).Methods("GET")
|
||||
return router
|
||||
|
||||
Reference in New Issue
Block a user