blob: 8132c3d4d76c6d6213785394a600e6548a74ff0c [file] [log] [blame]
Philipp Schradercdb5cfc2022-02-20 14:57:07 -08001package requests
2
3import (
Philipp Schraderfae8a7e2022-03-13 22:51:54 -07004 "encoding/base64"
Philipp Schraderd3fac192022-03-02 20:35:46 -08005 "errors"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -08006 "fmt"
7 "io"
Philipp Schraderfae8a7e2022-03-13 22:51:54 -07008 "log"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -08009 "net/http"
Philipp Schraderd3fac192022-03-02 20:35:46 -080010 "strconv"
11 "strings"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080012
Philipp Schrader8747f1b2022-02-23 23:56:22 -080013 "github.com/frc971/971-Robot-Code/scouting/db"
Philipp Schraderd3fac192022-03-02 20:35:46 -080014 "github.com/frc971/971-Robot-Code/scouting/scraping"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080015 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/error_response"
Philipp Schraderd3fac192022-03-02 20:35:46 -080016 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/refresh_match_list"
17 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/refresh_match_list_response"
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080018 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches"
19 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches_response"
Philipp Schraderacf96232022-03-01 22:03:30 -080020 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_data_scouting"
21 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_data_scouting_response"
Philipp Schraderd1c4bef2022-02-28 22:51:30 -080022 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_matches_for_team"
23 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_matches_for_team_response"
Alex Perry81f96ba2022-03-13 18:26:19 -070024 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_notes_for_team"
25 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_notes_for_team_response"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080026 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting"
Philipp Schrader30005e42022-03-06 13:53:58 -080027 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting_response"
Alex Perry81f96ba2022-03-13 18:26:19 -070028 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_notes"
29 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_notes_response"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080030 "github.com/frc971/971-Robot-Code/scouting/webserver/server"
31 flatbuffers "github.com/google/flatbuffers/go"
32)
33
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080034type SubmitDataScouting = submit_data_scouting.SubmitDataScouting
Philipp Schrader30005e42022-03-06 13:53:58 -080035type SubmitDataScoutingResponseT = submit_data_scouting_response.SubmitDataScoutingResponseT
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080036type RequestAllMatches = request_all_matches.RequestAllMatches
37type RequestAllMatchesResponseT = request_all_matches_response.RequestAllMatchesResponseT
Philipp Schraderd1c4bef2022-02-28 22:51:30 -080038type RequestMatchesForTeam = request_matches_for_team.RequestMatchesForTeam
39type RequestMatchesForTeamResponseT = request_matches_for_team_response.RequestMatchesForTeamResponseT
Philipp Schraderacf96232022-03-01 22:03:30 -080040type RequestDataScouting = request_data_scouting.RequestDataScouting
41type RequestDataScoutingResponseT = request_data_scouting_response.RequestDataScoutingResponseT
Philipp Schraderd3fac192022-03-02 20:35:46 -080042type RefreshMatchList = refresh_match_list.RefreshMatchList
43type RefreshMatchListResponseT = refresh_match_list_response.RefreshMatchListResponseT
Alex Perry81f96ba2022-03-13 18:26:19 -070044type SubmitNotes = submit_notes.SubmitNotes
45type SubmitNotesResponseT = submit_notes_response.SubmitNotesResponseT
46type RequestNotesForTeam = request_notes_for_team.RequestNotesForTeam
47type RequestNotesForTeamResponseT = request_notes_for_team_response.RequestNotesForTeamResponseT
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080048
Philipp Schrader8747f1b2022-02-23 23:56:22 -080049// The interface we expect the database abstraction to conform to.
50// We use an interface here because it makes unit testing easier.
51type Database interface {
52 AddToMatch(db.Match) error
53 AddToStats(db.Stats) error
54 ReturnMatches() ([]db.Match, error)
55 ReturnStats() ([]db.Stats, error)
Philipp Schraderd1c4bef2022-02-28 22:51:30 -080056 QueryMatches(int32) ([]db.Match, error)
Philipp Schrader8747f1b2022-02-23 23:56:22 -080057 QueryStats(int) ([]db.Stats, error)
Alex Perry81f96ba2022-03-13 18:26:19 -070058 QueryNotes(int32) (db.NotesData, error)
59 AddNotes(db.NotesData) error
Philipp Schrader8747f1b2022-02-23 23:56:22 -080060}
61
Philipp Schraderd3fac192022-03-02 20:35:46 -080062type ScrapeMatchList func(int32, string) ([]scraping.Match, error)
63
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080064// Handles unknown requests. Just returns a 404.
65func unknown(w http.ResponseWriter, req *http.Request) {
66 w.WriteHeader(http.StatusNotFound)
67}
68
69func respondWithError(w http.ResponseWriter, statusCode int, errorMessage string) {
70 builder := flatbuffers.NewBuilder(1024)
71 builder.Finish((&error_response.ErrorResponseT{
72 ErrorMessage: errorMessage,
73 }).Pack(builder))
74 w.WriteHeader(statusCode)
75 w.Write(builder.FinishedBytes())
76}
77
78func respondNotImplemented(w http.ResponseWriter) {
79 respondWithError(w, http.StatusNotImplemented, "")
80}
81
Philipp Schraderb7e75932022-03-26 16:18:34 -070082func parseRequest[T interface{}](w http.ResponseWriter, buf []byte, requestName string, parser func([]byte, flatbuffers.UOffsetT) *T) (*T, bool) {
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080083 success := true
84 defer func() {
85 if r := recover(); r != nil {
Philipp Schraderb7e75932022-03-26 16:18:34 -070086 respondWithError(w, http.StatusBadRequest, fmt.Sprintf("Failed to parse %s: %v", requestName, r))
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080087 success = false
88 }
89 }()
Philipp Schraderb7e75932022-03-26 16:18:34 -070090 result := parser(buf, 0)
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080091 return result, success
92}
93
Philipp Schraderfae8a7e2022-03-13 22:51:54 -070094// Parses the authorization information that the browser inserts into the
95// headers. The authorization follows this format:
96//
97// req.Headers["Authorization"] = []string{"Basic <base64 encoded username:password>"}
98func parseUsername(req *http.Request) string {
99 auth, ok := req.Header["Authorization"]
100 if !ok {
101 return "unknown"
102 }
103
104 parts := strings.Split(auth[0], " ")
105 if !(len(parts) == 2 && parts[0] == "Basic") {
106 return "unknown"
107 }
108
109 info, err := base64.StdEncoding.DecodeString(parts[1])
110 if err != nil {
111 log.Println("ERROR: Failed to parse Basic authentication.")
112 return "unknown"
113 }
114
115 loginParts := strings.Split(string(info), ":")
116 if len(loginParts) != 2 {
117 return "unknown"
118 }
119 return loginParts[0]
120}
121
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800122// Handles a SubmitDataScouting request.
Philipp Schrader8747f1b2022-02-23 23:56:22 -0800123type submitDataScoutingHandler struct {
124 db Database
125}
126
127func (handler submitDataScoutingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
Philipp Schraderfae8a7e2022-03-13 22:51:54 -0700128 // Get the username of the person submitting the data.
129 username := parseUsername(req)
Philipp Schraderfae8a7e2022-03-13 22:51:54 -0700130
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800131 requestBytes, err := io.ReadAll(req.Body)
132 if err != nil {
133 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
134 return
135 }
136
Philipp Schraderb7e75932022-03-26 16:18:34 -0700137 request, success := parseRequest[SubmitDataScouting](w, requestBytes, "SubmitDataScouting", submit_data_scouting.GetRootAsSubmitDataScouting)
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800138 if !success {
139 return
140 }
141
Philipp Schraderd7b6eba2022-03-18 22:21:25 -0700142 log.Println("Got data scouting data for match", request.Match(), "team", request.Team(), "from", username)
143
Philipp Schrader30005e42022-03-06 13:53:58 -0800144 stats := db.Stats{
Philipp Schraderfee07e12022-03-17 22:19:47 -0700145 TeamNumber: request.Team(),
146 MatchNumber: request.Match(),
147 StartingQuadrant: request.StartingQuadrant(),
148 AutoBallPickedUp: [5]bool{
149 request.AutoBall1(), request.AutoBall2(), request.AutoBall3(),
150 request.AutoBall4(), request.AutoBall5(),
151 },
Philipp Schraderfa45d742022-03-18 19:29:05 -0700152 ShotsMissedAuto: request.MissedShotsAuto(),
153 UpperGoalAuto: request.UpperGoalAuto(),
154 LowerGoalAuto: request.LowerGoalAuto(),
155 ShotsMissed: request.MissedShotsTele(),
156 UpperGoalShots: request.UpperGoalTele(),
157 LowerGoalShots: request.LowerGoalTele(),
158 PlayedDefense: request.DefenseRating(),
159 DefenseReceivedScore: request.DefenseReceivedRating(),
160 Climbing: int32(request.ClimbLevel()),
161 CollectedBy: username,
162 Comment: string(request.Comment()),
Philipp Schrader30005e42022-03-06 13:53:58 -0800163 }
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800164
Philipp Schraderfee07e12022-03-17 22:19:47 -0700165 // Do some error checking.
166 if stats.StartingQuadrant < 1 || stats.StartingQuadrant > 4 {
167 respondWithError(w, http.StatusBadRequest, fmt.Sprint(
168 "Invalid starting_quadrant field value of ", stats.StartingQuadrant))
169 return
170 }
171
Philipp Schrader30005e42022-03-06 13:53:58 -0800172 err = handler.db.AddToStats(stats)
173 if err != nil {
174 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to submit datascouting data: ", err))
Philipp Schraderfee07e12022-03-17 22:19:47 -0700175 return
Philipp Schrader30005e42022-03-06 13:53:58 -0800176 }
177
178 builder := flatbuffers.NewBuilder(50 * 1024)
179 builder.Finish((&SubmitDataScoutingResponseT{}).Pack(builder))
180 w.Write(builder.FinishedBytes())
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800181}
182
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800183// Handles a RequestAllMaches request.
184type requestAllMatchesHandler struct {
185 db Database
186}
187
188func (handler requestAllMatchesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
189 requestBytes, err := io.ReadAll(req.Body)
190 if err != nil {
191 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
192 return
193 }
194
Philipp Schraderb7e75932022-03-26 16:18:34 -0700195 _, success := parseRequest(w, requestBytes, "RequestAllMatches", request_all_matches.GetRootAsRequestAllMatches)
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800196 if !success {
197 return
198 }
199
200 matches, err := handler.db.ReturnMatches()
201 if err != nil {
Philipp Schraderfae8a7e2022-03-13 22:51:54 -0700202 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to query database: ", err))
Philipp Schrader2e7eb0002022-03-02 22:52:39 -0800203 return
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800204 }
205
206 var response RequestAllMatchesResponseT
207 for _, match := range matches {
208 response.MatchList = append(response.MatchList, &request_all_matches_response.MatchT{
209 MatchNumber: match.MatchNumber,
210 Round: match.Round,
211 CompLevel: match.CompLevel,
212 R1: match.R1,
213 R2: match.R2,
214 R3: match.R3,
215 B1: match.B1,
216 B2: match.B2,
217 B3: match.B3,
218 })
219 }
220
221 builder := flatbuffers.NewBuilder(50 * 1024)
222 builder.Finish((&response).Pack(builder))
223 w.Write(builder.FinishedBytes())
224}
225
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800226// Handles a RequestMatchesForTeam request.
227type requestMatchesForTeamHandler struct {
228 db Database
229}
230
231func (handler requestMatchesForTeamHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
232 requestBytes, err := io.ReadAll(req.Body)
233 if err != nil {
234 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
235 return
236 }
237
Philipp Schraderb7e75932022-03-26 16:18:34 -0700238 request, success := parseRequest(w, requestBytes, "RequestMatchesForTeam", request_matches_for_team.GetRootAsRequestMatchesForTeam)
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800239 if !success {
240 return
241 }
242
243 matches, err := handler.db.QueryMatches(request.Team())
244 if err != nil {
245 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Faled to query database: ", err))
Philipp Schrader2e7eb0002022-03-02 22:52:39 -0800246 return
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800247 }
248
249 var response RequestAllMatchesResponseT
250 for _, match := range matches {
251 response.MatchList = append(response.MatchList, &request_all_matches_response.MatchT{
252 MatchNumber: match.MatchNumber,
253 Round: match.Round,
254 CompLevel: match.CompLevel,
255 R1: match.R1,
256 R2: match.R2,
257 R3: match.R3,
258 B1: match.B1,
259 B2: match.B2,
260 B3: match.B3,
261 })
262 }
263
264 builder := flatbuffers.NewBuilder(50 * 1024)
265 builder.Finish((&response).Pack(builder))
266 w.Write(builder.FinishedBytes())
267}
268
Philipp Schraderacf96232022-03-01 22:03:30 -0800269// Handles a RequestDataScouting request.
270type requestDataScoutingHandler struct {
271 db Database
272}
273
274func (handler requestDataScoutingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
275 requestBytes, err := io.ReadAll(req.Body)
276 if err != nil {
277 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
278 return
279 }
280
Philipp Schraderb7e75932022-03-26 16:18:34 -0700281 _, success := parseRequest(w, requestBytes, "RequestDataScouting", request_data_scouting.GetRootAsRequestDataScouting)
Philipp Schraderacf96232022-03-01 22:03:30 -0800282 if !success {
283 return
284 }
285
286 stats, err := handler.db.ReturnStats()
287 if err != nil {
288 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Faled to query database: ", err))
Philipp Schrader2e7eb0002022-03-02 22:52:39 -0800289 return
Philipp Schraderacf96232022-03-01 22:03:30 -0800290 }
291
292 var response RequestDataScoutingResponseT
293 for _, stat := range stats {
294 response.StatsList = append(response.StatsList, &request_data_scouting_response.StatsT{
Philipp Schraderfa45d742022-03-18 19:29:05 -0700295 Team: stat.TeamNumber,
296 Match: stat.MatchNumber,
297 StartingQuadrant: stat.StartingQuadrant,
298 AutoBall1: stat.AutoBallPickedUp[0],
299 AutoBall2: stat.AutoBallPickedUp[1],
300 AutoBall3: stat.AutoBallPickedUp[2],
301 AutoBall4: stat.AutoBallPickedUp[3],
302 AutoBall5: stat.AutoBallPickedUp[4],
303 MissedShotsAuto: stat.ShotsMissedAuto,
304 UpperGoalAuto: stat.UpperGoalAuto,
305 LowerGoalAuto: stat.LowerGoalAuto,
306 MissedShotsTele: stat.ShotsMissed,
307 UpperGoalTele: stat.UpperGoalShots,
308 LowerGoalTele: stat.LowerGoalShots,
309 DefenseRating: stat.PlayedDefense,
310 DefenseReceivedRating: stat.DefenseReceivedScore,
311 ClimbLevel: request_data_scouting_response.ClimbLevel(stat.Climbing),
312 CollectedBy: stat.CollectedBy,
313 Comment: stat.Comment,
Philipp Schraderacf96232022-03-01 22:03:30 -0800314 })
315 }
316
317 builder := flatbuffers.NewBuilder(50 * 1024)
318 builder.Finish((&response).Pack(builder))
319 w.Write(builder.FinishedBytes())
320}
321
Philipp Schraderd3fac192022-03-02 20:35:46 -0800322func parseTeamKey(teamKey string) (int, error) {
323 // TBA prefixes teams with "frc". Not sure why. Get rid of that.
324 teamKey = strings.TrimPrefix(teamKey, "frc")
325 return strconv.Atoi(teamKey)
326}
327
328// Parses the alliance data from the specified match and returns the three red
329// teams and the three blue teams.
330func parseTeamKeys(match *scraping.Match) ([3]int32, [3]int32, error) {
331 redKeys := match.Alliances.Red.TeamKeys
332 blueKeys := match.Alliances.Blue.TeamKeys
333
334 if len(redKeys) != 3 || len(blueKeys) != 3 {
335 return [3]int32{}, [3]int32{}, errors.New(fmt.Sprintf(
336 "Found %d red teams and %d blue teams.", len(redKeys), len(blueKeys)))
337 }
338
339 var red [3]int32
340 for i, key := range redKeys {
341 team, err := parseTeamKey(key)
342 if err != nil {
343 return [3]int32{}, [3]int32{}, errors.New(fmt.Sprintf(
344 "Failed to parse red %d team '%s' as integer: %v", i+1, key, err))
345 }
346 red[i] = int32(team)
347 }
348 var blue [3]int32
349 for i, key := range blueKeys {
350 team, err := parseTeamKey(key)
351 if err != nil {
352 return [3]int32{}, [3]int32{}, errors.New(fmt.Sprintf(
353 "Failed to parse blue %d team '%s' as integer: %v", i+1, key, err))
354 }
355 blue[i] = int32(team)
356 }
357 return red, blue, nil
358}
359
360type refreshMatchListHandler struct {
361 db Database
362 scrape ScrapeMatchList
363}
364
365func (handler refreshMatchListHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
366 requestBytes, err := io.ReadAll(req.Body)
367 if err != nil {
368 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
369 return
370 }
371
Philipp Schraderb7e75932022-03-26 16:18:34 -0700372 request, success := parseRequest(w, requestBytes, "RefreshMatchList", refresh_match_list.GetRootAsRefreshMatchList)
Philipp Schraderd3fac192022-03-02 20:35:46 -0800373 if !success {
374 return
375 }
376
377 matches, err := handler.scrape(request.Year(), string(request.EventCode()))
378 if err != nil {
379 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Faled to scrape match list: ", err))
380 return
381 }
382
383 for _, match := range matches {
384 // Make sure the data is valid.
385 red, blue, err := parseTeamKeys(&match)
386 if err != nil {
387 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf(
388 "TheBlueAlliance data for match %d is malformed: %v", match.MatchNumber, err))
389 return
390 }
391 // Add the match to the database.
Philipp Schrader7365d322022-03-06 16:40:08 -0800392 err = handler.db.AddToMatch(db.Match{
Philipp Schraderd3fac192022-03-02 20:35:46 -0800393 MatchNumber: int32(match.MatchNumber),
Philipp Schrader45befdd2022-04-08 19:12:44 -0700394 Round: int32(match.SetNumber),
395 CompLevel: match.CompLevel,
396 R1: red[0],
397 R2: red[1],
398 R3: red[2],
399 B1: blue[0],
400 B2: blue[1],
401 B3: blue[2],
Philipp Schraderd3fac192022-03-02 20:35:46 -0800402 })
Philipp Schrader7365d322022-03-06 16:40:08 -0800403 if err != nil {
404 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf(
405 "Failed to add match %d to the database: %v", match.MatchNumber, err))
406 return
407 }
Philipp Schraderd3fac192022-03-02 20:35:46 -0800408 }
409
410 var response RefreshMatchListResponseT
411 builder := flatbuffers.NewBuilder(1024)
412 builder.Finish((&response).Pack(builder))
413 w.Write(builder.FinishedBytes())
414}
415
Alex Perry81f96ba2022-03-13 18:26:19 -0700416type submitNoteScoutingHandler struct {
417 db Database
418}
419
420func (handler submitNoteScoutingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
421 requestBytes, err := io.ReadAll(req.Body)
422 if err != nil {
423 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
424 return
425 }
426
Philipp Schraderb7e75932022-03-26 16:18:34 -0700427 request, success := parseRequest(w, requestBytes, "SubmitNotes", submit_notes.GetRootAsSubmitNotes)
Alex Perry81f96ba2022-03-13 18:26:19 -0700428 if !success {
429 return
430 }
431
432 err = handler.db.AddNotes(db.NotesData{
433 TeamNumber: request.Team(),
434 Notes: []string{string(request.Notes())},
435 })
436 if err != nil {
437 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to insert notes: %v", err))
438 return
439 }
440
441 var response SubmitNotesResponseT
442 builder := flatbuffers.NewBuilder(10)
443 builder.Finish((&response).Pack(builder))
444 w.Write(builder.FinishedBytes())
445}
446
Alex Perry81f96ba2022-03-13 18:26:19 -0700447type requestNotesForTeamHandler struct {
448 db Database
449}
450
451func (handler requestNotesForTeamHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
452 requestBytes, err := io.ReadAll(req.Body)
453 if err != nil {
454 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
455 return
456 }
457
Philipp Schraderb7e75932022-03-26 16:18:34 -0700458 request, success := parseRequest(w, requestBytes, "RequestNotesForTeam", request_notes_for_team.GetRootAsRequestNotesForTeam)
Alex Perry81f96ba2022-03-13 18:26:19 -0700459 if !success {
460 return
461 }
462
463 notesData, err := handler.db.QueryNotes(request.Team())
464 if err != nil {
465 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to query notes: %v", err))
466 return
467 }
468
469 var response RequestNotesForTeamResponseT
470 for _, data := range notesData.Notes {
471 response.Notes = append(response.Notes, &request_notes_for_team_response.NoteT{data})
472 }
473
474 builder := flatbuffers.NewBuilder(1024)
475 builder.Finish((&response).Pack(builder))
476 w.Write(builder.FinishedBytes())
477}
478
Philipp Schraderd3fac192022-03-02 20:35:46 -0800479func HandleRequests(db Database, scrape ScrapeMatchList, scoutingServer server.ScoutingServer) {
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800480 scoutingServer.HandleFunc("/requests", unknown)
Philipp Schrader8747f1b2022-02-23 23:56:22 -0800481 scoutingServer.Handle("/requests/submit/data_scouting", submitDataScoutingHandler{db})
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800482 scoutingServer.Handle("/requests/request/all_matches", requestAllMatchesHandler{db})
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800483 scoutingServer.Handle("/requests/request/matches_for_team", requestMatchesForTeamHandler{db})
Philipp Schraderacf96232022-03-01 22:03:30 -0800484 scoutingServer.Handle("/requests/request/data_scouting", requestDataScoutingHandler{db})
Philipp Schraderd3fac192022-03-02 20:35:46 -0800485 scoutingServer.Handle("/requests/refresh_match_list", refreshMatchListHandler{db, scrape})
Alex Perry81f96ba2022-03-13 18:26:19 -0700486 scoutingServer.Handle("/requests/submit/submit_notes", submitNoteScoutingHandler{db})
487 scoutingServer.Handle("/requests/request/notes_for_team", requestNotesForTeamHandler{db})
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800488}