Add DB objects
This commit is contained in:
parent
abd5e2025e
commit
6ec37b83ce
7 changed files with 450 additions and 2 deletions
83
admin/exercice.go
Normal file
83
admin/exercice.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Exercice struct {
|
||||
id int64
|
||||
Title string
|
||||
Statement string
|
||||
Hint string
|
||||
depend int64
|
||||
Gain int64
|
||||
VideoURI string
|
||||
}
|
||||
|
||||
func (t Theme) GetExercices() ([]Exercice, error) {
|
||||
if rows, err := DBQuery("SELECT id_exercice, title, statement, hint, depend, gain, video_uri FROM teams WHERE id_theme = ?", t.id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
var exos = make([]Exercice, 0)
|
||||
for rows.Next() {
|
||||
var e Exercice
|
||||
if err := rows.Scan(&e.id, &e.Title, &e.Statement, &e.Hint, &e.depend, &e.Gain, &e.VideoURI); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
exos = append(exos, e)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return exos, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t Theme) AddExercice(title string, statement string, hint string, depend *Exercice, gain int, videoURI string) (Exercice, error) {
|
||||
if res, err := DBExec("INSERT INTO exercices (id_theme, title, statement, hint, depend, gain, video_uri) VALUES (?, ?, ?, ?, ?, ?, ?)", t.id, title, statement, hint, depend.id, gain, videoURI); err != nil {
|
||||
return Exercice{}, err
|
||||
} else if eid, err := res.LastInsertId(); err != nil {
|
||||
return Exercice{}, err
|
||||
} else {
|
||||
return Exercice{eid, title, statement, hint, depend.id, int64(gain), videoURI}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (e Exercice) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE exercices SET title = ?, statement = ?, hint = ?, depend = ?, gain = ?, video_uri = ? WHERE id_exercice = ?", e.Title, e.Statement, e.Hint, e.depend, e.Gain, e.VideoURI, e.id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (e Exercice) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM exercices WHERE id_exercice = ?", e.id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (e Exercice) NewTry(t Team) error {
|
||||
if _, err := DBExec("INSERT INTO exercice_tries (id_exercice, id_team, time) VALUES (?, ?, ?)", e.id, t.id, time.Now()); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (e Exercice) Solved(t Team) error {
|
||||
if _, err := DBExec("INSERT INTO exercice_solved (id_exercice, id_team, time) VALUES (?, ?, ?)", e.id, t.id, time.Now()); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
Reference in a new issue