diff --git a/admin/api/team.go b/admin/api/team.go index 0451266f..3dd1c017 100644 --- a/admin/api/team.go +++ b/admin/api/team.go @@ -55,6 +55,14 @@ func init() { return fic.GetTeamsStats(nil) } }))) + router.GET("/api/teams/:tid/history.json", apiHandler(teamPublicHandler( + func(team *fic.Team, _ []byte) (interface{}, error) { + if team != nil { + return team.GetHistory() + } else { + return fic.GetTeamsStats(nil) + } + }))) router.GET("/api/teams/:tid/tries", apiHandler(teamPublicHandler( func(team *fic.Team, _ []byte) (interface{}, error) { return fic.GetTries(team, nil) }))) diff --git a/libfic/team_history.go b/libfic/team_history.go new file mode 100644 index 00000000..6a629a70 --- /dev/null +++ b/libfic/team_history.go @@ -0,0 +1,46 @@ +package fic + +import ( + "time" +) + +func (t Team) GetHistory() ([]map[string]interface{}, error) { + hist := make([]map[string]interface{}, 0) + + if rows, err := DBQuery(`SELECT id_team, "tries" AS kind, time, E.id_exercice, E.title, NULL, NULL FROM exercice_tries T INNER JOIN exercices E ON E.id_exercice = T.id_exercice WHERE id_team = ? UNION SELECT id_team, "solved" AS kind, time, E.id_exercice, E.title, coefficient, NULL FROM exercice_solved S INNER JOIN exercices E ON E.id_exercice = S.id_exercice WHERE id_team = ? UNION SELECT id_team, "hint" AS kind, time, E.id_exercice, E.title, H.id_hint, H.title FROM team_hints T INNER JOIN exercice_hints H ON H.id_hint = T.id_hint INNER JOIN exercices E ON E.id_exercice = H.id_exercice WHERE id_team = ? UNION SELECT id_team, "key_found" AS kind, time, E.id_exercice, E.title, K.id_key, K.type FROM key_found F INNER JOIN exercice_keys K ON K.id_key = F.id_key INNER JOIN exercices E ON K.id_exercice = E.id_exercice WHERE id_team = ? ORDER BY time DESC`, t.Id, t.Id, t.Id, t.Id); err != nil { + return nil, err + } else { + defer rows.Close() + + for rows.Next() { + var id_team int64 + var kind string + var time time.Time + var primary *int64 + var primary_title *string + var secondary *int64 + var secondary_title *string + + if err := rows.Scan(&id_team, &kind, &time, &primary, &primary_title, &secondary, &secondary_title); err != nil { + return nil, err + } + + h := map[string]interface{}{} + + h["kind"] = kind + h["time"] = time + if primary != nil { + h["primary"] = primary + h["primary_title"] = primary_title + } + if secondary != nil { + h["secondary"] = secondary + h["secondary_title"] = secondary_title + } + + hist = append(hist, h) + } + } + + return hist, nil +}