Move common structs to libfic
This commit is contained in:
parent
d841542be4
commit
92b81e467f
10 changed files with 42 additions and 36 deletions
|
@ -1,111 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Exercice struct {
|
||||
Id int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Statement string `json:"statement"`
|
||||
Hint string `json:"hint"`
|
||||
Depend *int64 `json:"depend"`
|
||||
Gain int64 `json:"gain"`
|
||||
VideoURI string `json:"videoURI"`
|
||||
}
|
||||
|
||||
func GetExercice(id int) (Exercice, error) {
|
||||
var e Exercice
|
||||
if err := DBQueryRow("SELECT id_exercice, title, statement, hint, depend, gain, video_uri FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.Title, &e.Statement, &e.Hint, &e.Depend, &e.Gain, &e.VideoURI); err != nil {
|
||||
return Exercice{}, err
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (t Theme) GetExercice(id int) (Exercice, error) {
|
||||
var e Exercice
|
||||
if err := DBQueryRow("SELECT id_exercice, title, statement, hint, depend, gain, video_uri FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.Title, &e.Statement, &e.Hint, &e.Depend, &e.Gain, &e.VideoURI); err != nil {
|
||||
return Exercice{}, err
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
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) {
|
||||
var dpd interface{}
|
||||
if depend == nil {
|
||||
dpd = nil
|
||||
} else {
|
||||
dpd = depend.Id
|
||||
}
|
||||
if res, err := DBExec("INSERT INTO exercices (id_theme, title, statement, hint, depend, gain, video_uri) VALUES (?, ?, ?, ?, ?, ?, ?)", t.Id, title, statement, hint, dpd, gain, videoURI); err != nil {
|
||||
return Exercice{}, err
|
||||
} else if eid, err := res.LastInsertId(); err != nil {
|
||||
return Exercice{}, err
|
||||
} else {
|
||||
if depend == nil {
|
||||
return Exercice{eid, title, statement, hint, nil, int64(gain), videoURI}, nil
|
||||
} 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