token-validator: add a new route to get computed grades

This commit is contained in:
nemunaire 2019-09-16 15:36:19 +02:00
commit 7682cae26c
2 changed files with 87 additions and 2 deletions

View file

@ -191,6 +191,7 @@ type UnlockedChallenge struct {
IdStudent int64 `json:"id_student"`
Challenge int `json:"challenge"`
Time time.Time `json:"time"`
Value string `json:"value,omitempty"`
}
func (s Student) getStates() (ucs []UnlockedChallenge, err error) {
@ -215,13 +216,35 @@ func (s Student) getStates() (ucs []UnlockedChallenge, err error) {
}
}
func (s Student) getStatesByChallenge() (ucs []UnlockedChallenge, err error) {
if rows, errr := DBQuery("SELECT id_st, challenge, MIN(time), value FROM student_challenges WHERE id_student = ? GROUP BY challenge, 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, &u.Value); 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) {
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
} else if utid, err := res.LastInsertId(); err != nil {
return UnlockedChallenge{}, err
} else {
return UnlockedChallenge{utid, s.Id, challenge, time.Now()}, err
return UnlockedChallenge{utid, s.Id, challenge, time.Now(), value}, err
}
}
@ -229,7 +252,7 @@ func (s Student) UpdateUnlockedChallenge(challenge int, value string) (UnlockedC
if _, err := DBExec("UPDATE student_challenges SET time = ?, value = ? WHERE id_student = ? AND challenge = ?", time.Now(), value, s.Id, challenge); err != nil {
return UnlockedChallenge{}, err
} else {
return UnlockedChallenge{0, s.Id, challenge, time.Now()}, err
return UnlockedChallenge{0, s.Id, challenge, time.Now(), value}, err
}
}