token-validator: add a new route to get computed grades
This commit is contained in:
parent
ffcccce709
commit
7682cae26c
2 changed files with 87 additions and 2 deletions
62
token-validator/grades.go
Normal file
62
token-validator/grades.go
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
router.GET("/api/grades/", apiHandler(computeGrades))
|
||||||
|
}
|
||||||
|
|
||||||
|
func computeGrades(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||||
|
if stds, err := getStudents(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
res := map[string]map[string]float32{}
|
||||||
|
|
||||||
|
for _, std := range stds {
|
||||||
|
res[std.Login] = map[string]float32{
|
||||||
|
"TP1": 0,
|
||||||
|
"TP2": 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
if states, err := std.getStatesByChallenge(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
for _, st := range states {
|
||||||
|
if st.Challenge >= 100 {
|
||||||
|
switch st.Challenge {
|
||||||
|
case 100:
|
||||||
|
res[std.Login]["TP2"] += 5
|
||||||
|
case 101:
|
||||||
|
res[std.Login]["TP2"] += 5
|
||||||
|
case 102:
|
||||||
|
res[std.Login]["TP2"] += 5
|
||||||
|
case 103:
|
||||||
|
res[std.Login]["TP2"] += 5
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch st.Challenge {
|
||||||
|
case 1:
|
||||||
|
res[std.Login]["TP1"] += 4
|
||||||
|
case 2:
|
||||||
|
res[std.Login]["TP1"] += 4
|
||||||
|
case 3:
|
||||||
|
res[std.Login]["TP1"] += 5
|
||||||
|
case 4:
|
||||||
|
res[std.Login]["TP1"] += 2
|
||||||
|
case 5:
|
||||||
|
res[std.Login]["TP1"] += 2
|
||||||
|
case 6:
|
||||||
|
res[std.Login]["TP1"] += 1
|
||||||
|
case 7:
|
||||||
|
res[std.Login]["TP1"] += 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
}
|
|
@ -191,6 +191,7 @@ type UnlockedChallenge struct {
|
||||||
IdStudent int64 `json:"id_student"`
|
IdStudent int64 `json:"id_student"`
|
||||||
Challenge int `json:"challenge"`
|
Challenge int `json:"challenge"`
|
||||||
Time time.Time `json:"time"`
|
Time time.Time `json:"time"`
|
||||||
|
Value string `json:"value,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Student) getStates() (ucs []UnlockedChallenge, err error) {
|
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) {
|
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
|
||||||
} else if utid, err := res.LastInsertId(); err != nil {
|
} else if utid, err := res.LastInsertId(); err != nil {
|
||||||
return UnlockedChallenge{}, err
|
return UnlockedChallenge{}, err
|
||||||
} else {
|
} 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 {
|
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
|
return UnlockedChallenge{}, err
|
||||||
} else {
|
} else {
|
||||||
return UnlockedChallenge{0, s.Id, challenge, time.Now()}, err
|
return UnlockedChallenge{0, s.Id, challenge, time.Now(), value}, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue