token-validator: add recap

This commit is contained in:
nemunaire 2018-02-22 04:47:57 +01:00 committed by Pierre-Olivier Mercier
parent 9a821276e0
commit cb0098317f

View file

@ -11,6 +11,20 @@ import (
) )
func init() { func init() {
router.GET("/api/progress/", apiHandler(
func(httprouter.Params, []byte) (interface{}, error) {
if stds, err := getStudents(); err != nil {
return nil, err
} else {
ret := map[string][]UnlockedChallenge{}
for _, std := range stds {
if ret[std.Login], err = std.getStates(); err != nil {
return nil, err
}
}
return ret, nil
}
}))
router.GET("/api/students/", apiHandler( router.GET("/api/students/", apiHandler(
func(httprouter.Params, []byte) (interface{}, error) { func(httprouter.Params, []byte) (interface{}, error) {
return getStudents() return getStudents()
@ -150,6 +164,28 @@ type UnlockedChallenge struct {
Time time.Time `json:"time"` Time time.Time `json:"time"`
} }
func (s Student) getStates() (ucs []UnlockedChallenge, err error) {
if rows, errr := DBQuery("SELECT id_st, challenge, time FROM student_challenges WHERE id_student = ?", s.Id); errr != nil {
return nil, errr
} else {
defer rows.Close()
for rows.Next() {
var u UnlockedChallenge
u.IdStudent = s.Id
if err = rows.Scan(&u.Id, &u.Challenge, &u.Time); err != nil {
return
}
ucs = append(ucs, u)
}
if err = rows.Err(); err != nil {
return
}
return
}
}
func (s Student) UnlockNewChallenge(challenge int, value string) (UnlockedChallenge, error) { func (s Student) UnlockNewChallenge(challenge int, value string) (UnlockedChallenge, error) {
if res, err := DBExec("INSERT INTO student_challenges (id_student, challenge, time, value) VALUES (?, ?, ?, ?)", s.Id, challenge, time.Now(), value); err != nil { if res, err := DBExec("INSERT INTO student_challenges (id_student, challenge, time, value) VALUES (?, ?, ?, ?)", s.Id, challenge, time.Now(), value); err != nil {
return UnlockedChallenge{}, err return UnlockedChallenge{}, err