admin: add history route in API

This commit is contained in:
nemunaire 2017-11-12 22:14:41 +01:00 committed by Pierre-Olivier Mercier
parent e362700031
commit 41400a8710
2 changed files with 54 additions and 0 deletions

View File

@ -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) })))

46
libfic/team_history.go Normal file
View File

@ -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
}