HasSolved now returns a tuple (bool, Time, int)

This commit is contained in:
nemunaire 2016-01-18 19:40:11 +01:00
parent c33c2b8e8a
commit 381aefa597
3 changed files with 19 additions and 12 deletions

View file

@ -134,7 +134,8 @@ func (e Exercice) Solved(t Team) error {
} }
func (e Exercice) CheckResponse(response string, t Team) (bool, error) { func (e Exercice) CheckResponse(response string, t Team) (bool, error) {
if t.HasSolved(e) { s, _, _ := t.HasSolved(e)
if s {
return true, nil return true, nil
} }

View file

@ -1,6 +1,8 @@
package fic package fic
import () import (
"time"
)
type Team struct { type Team struct {
Id int64 `json:"id"` Id int64 `json:"id"`
@ -74,15 +76,19 @@ func (t Team) HasAccess(e Exercice) bool {
} else { } else {
ed := Exercice{} ed := Exercice{}
ed.Id = *e.Depend ed.Id = *e.Depend
return t.HasSolved(ed) s, _, _ := t.HasSolved(ed)
return s
} }
} }
func (t Team) HasSolved(e Exercice) bool { func (t Team) HasSolved(e Exercice) (bool, time.Time, int64) {
var nb int var nb int64
if err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&nb); err != nil { var tm time.Time
return false if err := DBQueryRow("SELECT time FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&tm); err != nil {
return false, time.Now(), 0
} else if err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_exercice = ? AND time < ?", e.Id, tm).Scan(&nb); err != nil {
return true, tm, 0
} else { } else {
return nb > 0 return true, tm, nb
} }
} }