scouting: Allow the requests handler access to the database
This patch makes it so the requests handlers actually have access to
the database. In the unit tests we use a mock database, but in the
`webserver` binary we use the real one.
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: Iaf5b2c039a275dd6ddfbff173aa6ad59cf3988b5
diff --git a/scouting/webserver/requests/requests.go b/scouting/webserver/requests/requests.go
index 4839675..2462ccb 100644
--- a/scouting/webserver/requests/requests.go
+++ b/scouting/webserver/requests/requests.go
@@ -5,6 +5,7 @@
"io"
"net/http"
+ "github.com/frc971/971-Robot-Code/scouting/db"
"github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/error_response"
"github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting"
_ "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting_response"
@@ -12,6 +13,17 @@
flatbuffers "github.com/google/flatbuffers/go"
)
+// The interface we expect the database abstraction to conform to.
+// We use an interface here because it makes unit testing easier.
+type Database interface {
+ AddToMatch(db.Match) error
+ AddToStats(db.Stats) error
+ ReturnMatches() ([]db.Match, error)
+ ReturnStats() ([]db.Stats, error)
+ QueryMatches(int) ([]db.Match, error)
+ QueryStats(int) ([]db.Stats, error)
+}
+
// Handles unknown requests. Just returns a 404.
func unknown(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNotFound)
@@ -44,7 +56,11 @@
}
// Handles a SubmitDataScouting request.
-func submitDataScouting(w http.ResponseWriter, req *http.Request) {
+type submitDataScoutingHandler struct {
+ db Database
+}
+
+func (handler submitDataScoutingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
requestBytes, err := io.ReadAll(req.Body)
if err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
@@ -57,11 +73,13 @@
}
// TODO(phil): Actually handle the request.
+ // We have access to the database via "handler.db" here. For example:
+ // stats := handler.db.ReturnStats()
respondNotImplemented(w)
}
-func HandleRequests(scoutingServer server.ScoutingServer) {
+func HandleRequests(db Database, scoutingServer server.ScoutingServer) {
scoutingServer.HandleFunc("/requests", unknown)
- scoutingServer.HandleFunc("/requests/submit/data_scouting", submitDataScouting)
+ scoutingServer.Handle("/requests/submit/data_scouting", submitDataScoutingHandler{db})
}