Add/remove/view themes and exercices

This commit is contained in:
nemunaire 2016-01-13 13:07:45 +01:00
parent 5bc3395b04
commit c2d26b6053
2 changed files with 132 additions and 11 deletions

View file

@ -9,11 +9,29 @@ type Exercice struct {
Title string `json:"title"`
Statement string `json:"statement"`
Hint string `json:"hint"`
Depend int64 `json:"depend"`
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
@ -37,12 +55,22 @@ func (t Theme) GetExercices() ([]Exercice, error) {
}
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 {
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 {
return Exercice{eid, title, statement, hint, depend.Id, int64(gain), videoURI}, nil
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
}
}
}