sync: implement hint dependency on flags
This commit is contained in:
parent
20f2597248
commit
6740256a32
6 changed files with 72 additions and 2 deletions
|
|
@ -1,6 +1,8 @@
|
|||
package fic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -105,7 +107,9 @@ func (h EHint) Delete() (int64, error) {
|
|||
|
||||
// WipeHints deletes (only in the database, not on disk) hints coming with the challenge.
|
||||
func (e Exercice) WipeHints() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM exercice_hints WHERE id_exercice = ?", e.Id); err != nil {
|
||||
if _, err := DBExec("DELETE FROM exercice_hints_deps WHERE id_hint IN (SELECT id_hint FROM exercice_hints WHERE id_exercice = ?)", e.Id); err != nil {
|
||||
return 0, err
|
||||
} else if res, err := DBExec("DELETE FROM exercice_hints WHERE id_exercice = ?", e.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
|
|
@ -114,6 +118,40 @@ func (e Exercice) WipeHints() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// AddDepend insert a new dependency to a given flag.
|
||||
func (h EHint) AddDepend(f Flag) (err error) {
|
||||
if d, ok := f.(FlagKey); ok {
|
||||
_, err = DBExec("INSERT INTO exercice_hints_deps (id_hint, id_flag_dep) VALUES (?, ?)", h.Id, d.Id)
|
||||
} else {
|
||||
err = errors.New(fmt.Sprintf("Dependancy type for key (%T) not implemented for this flag.", f))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetDepends retrieve the flag's dependency list.
|
||||
func (h EHint) GetDepends() ([]Flag, error) {
|
||||
var deps = make([]Flag, 0)
|
||||
|
||||
if rows, err := DBQuery("SELECT id_flag_dep FROM exercice_hints_deps WHERE id_hint = ?", h.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var d int64
|
||||
if err := rows.Scan(&d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deps = append(deps, FlagKey{Id: d, IdExercice: h.IdExercice})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return deps, nil
|
||||
}
|
||||
|
||||
// GetExercice returns the parent Exercice where this hint can be found.
|
||||
func (h EHint) GetExercice() (Exercice, error) {
|
||||
var eid int64
|
||||
|
|
|
|||
Reference in a new issue