Multiple hints
This commit is contained in:
parent
22e8937879
commit
25bf34e82c
9 changed files with 217 additions and 35 deletions
65
libfic/hint.go
Normal file
65
libfic/hint.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package fic
|
||||
|
||||
import (
|
||||
)
|
||||
|
||||
type EHint struct {
|
||||
Id int64 `json:"id"`
|
||||
IdExercice int64 `json:"idExercice"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Cost int64 `json:"cost"`
|
||||
}
|
||||
|
||||
func (e Exercice) GetHints() ([]EHint, error) {
|
||||
if rows, err := DBQuery("SELECT id_hint, title, content, cost FROM exercice_hints WHERE id_exercice = ?", e.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
var hints = make([]EHint, 0)
|
||||
for rows.Next() {
|
||||
var h EHint
|
||||
h.IdExercice = e.Id
|
||||
if err := rows.Scan(&h.Id, &h.Title, &h.Content, &h.Cost); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hints = append(hints, h)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return hints, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (e Exercice) AddHint(title string, content string, cost int64) (EHint, error) {
|
||||
if res, err := DBExec("INSERT INTO exercice_hints (id_exercice, title, content, cost) VALUES (?, ?, ?, ?)", e.Id, title, content, cost); err != nil {
|
||||
return EHint{}, err
|
||||
} else if hid, err := res.LastInsertId(); err != nil {
|
||||
return EHint{}, err
|
||||
} else {
|
||||
return EHint{hid, e.Id, title, content, cost}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (h EHint) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE exercice_hints SET id_exercice = ?, title = ?, content = ?, cost = ? WHERE id_hint = ?", h.IdExercice, h.Title, h.Content, h.Cost, h.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (h EHint) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM exercice_hints WHERE id_hint = ?", h.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
Reference in a new issue