diff --git a/libadlin/db.go b/libadlin/db.go index 5fafcc9..cfa149b 100644 --- a/libadlin/db.go +++ b/libadlin/db.go @@ -114,8 +114,10 @@ CREATE TABLE IF NOT EXISTS student_challenge_errors( if _, err := db.Exec(` CREATE TABLE IF NOT EXISTS student_pong( id_student INTEGER NOT NULL, - time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + last TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + count INTEGER UNSIGNED DEFAULT 1, state BOOLEAN NOT NULL DEFAULT 0, + CONSTRAINT one_pong UNIQUE (id_student,state), FOREIGN KEY(id_student) REFERENCES students(id_student) ) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin; `); err != nil { diff --git a/libadlin/ping.go b/libadlin/ping.go index a93fc9c..9926a87 100644 --- a/libadlin/ping.go +++ b/libadlin/ping.go @@ -6,18 +6,19 @@ import ( type Pong struct { Date time.Time + Count uint State bool } func (s *Student) LastPongs() (pongs []*Pong, err error) { - if rows, errr := DBQuery("SELECT time, state FROM student_pong WHERE id_student = ? ORDER BY time DESC", s.Id); errr != nil { + if rows, errr := DBQuery("SELECT last, count, state FROM student_pong WHERE id_student = ? ORDER BY last DESC", s.Id); errr != nil { return nil, errr } else { defer rows.Close() for rows.Next() { p := &Pong{} - if err = rows.Scan(&p.Date, &p.State); err != nil { + if err = rows.Scan(&p.Date, &p.Count, &p.State); err != nil { return } pongs = append(pongs, p) @@ -31,6 +32,9 @@ func (s *Student) LastPongs() (pongs []*Pong, err error) { } func (s *Student) OnPong(state bool) (err error) { - _, err = DBExec("INSERT INTO student_pong (id_student, time, state) VALUES (?, ?, ?)", s.Id, time.Now(), state) + _, err = DBExec("INSERT INTO student_pong (id_student, last, state) VALUES (?, ?, ?)", s.Id, time.Now(), state) + if err != nil { + _, err = DBExec("UPDATE student_pong SET last = CURRENT_TIMESTAMP, count = count + 1 WHERE id_student = ? AND state = ?", s.Id, state) + } return }