Use pointer receiver more offen
This commit is contained in:
parent
6999b4e728
commit
c7569b5e54
59 changed files with 688 additions and 672 deletions
|
|
@ -36,42 +36,42 @@ func treatHintContent(h *EHint) {
|
|||
}
|
||||
|
||||
// GetHint retrieves the hint with the given id.
|
||||
func GetHint(id int64) (EHint, error) {
|
||||
var h EHint
|
||||
func GetHint(id int64) (*EHint, error) {
|
||||
h := &EHint{}
|
||||
if err := DBQueryRow("SELECT id_hint, id_exercice, title, content, cost FROM exercice_hints WHERE id_hint = ?", id).Scan(&h.Id, &h.IdExercice, &h.Title, &h.Content, &h.Cost); err != nil {
|
||||
return h, err
|
||||
return nil, err
|
||||
}
|
||||
treatHintContent(&h)
|
||||
treatHintContent(h)
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// GetHintByTitle retrieves the hint with the given id.
|
||||
func (e Exercice) GetHintByTitle(id int64) (EHint, error) {
|
||||
var h EHint
|
||||
func (e *Exercice) GetHintByTitle(id int64) (*EHint, error) {
|
||||
h := &EHint{}
|
||||
if err := DBQueryRow("SELECT id_hint, id_exercice, title, content, cost FROM exercice_hints WHERE title = ? AND id_exercice = ?", id, e.Id).Scan(&h.Id, &h.IdExercice, &h.Title, &h.Content, &h.Cost); err != nil {
|
||||
return h, err
|
||||
return nil, err
|
||||
}
|
||||
treatHintContent(&h)
|
||||
treatHintContent(h)
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// GetHints returns a list of hints comming with the challenge.
|
||||
func (e Exercice) GetHints() ([]EHint, error) {
|
||||
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)
|
||||
var hints []*EHint
|
||||
for rows.Next() {
|
||||
var h EHint
|
||||
h := &EHint{}
|
||||
h.IdExercice = e.Id
|
||||
if err := rows.Scan(&h.Id, &h.Title, &h.Content, &h.Cost); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
treatHintContent(&h)
|
||||
treatHintContent(h)
|
||||
hints = append(hints, h)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
|
|
@ -83,18 +83,18 @@ func (e Exercice) GetHints() ([]EHint, error) {
|
|||
}
|
||||
|
||||
// AddHint creates and fills a new struct EHint and registers it into the database.
|
||||
func (e Exercice) AddHint(title string, content string, cost int64) (EHint, error) {
|
||||
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
|
||||
return nil, err
|
||||
} else if hid, err := res.LastInsertId(); err != nil {
|
||||
return EHint{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return EHint{hid, e.Id, title, content, "", cost}, nil
|
||||
return &EHint{hid, e.Id, title, content, "", cost}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Update applies modifications back to the database.
|
||||
func (h EHint) Update() (int64, error) {
|
||||
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 {
|
||||
|
|
@ -105,7 +105,7 @@ func (h EHint) Update() (int64, error) {
|
|||
}
|
||||
|
||||
// Delete the hint from the database.
|
||||
func (h EHint) Delete() (int64, error) {
|
||||
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 {
|
||||
|
|
@ -116,7 +116,7 @@ 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) {
|
||||
func (e *Exercice) WipeHints() (int64, error) {
|
||||
if _, err := DBExec("DELETE FROM exercice_hints_okey_deps WHERE id_hint IN (SELECT id_hint FROM exercice_hints WHERE id_exercice = ?)", e.Id); err != nil {
|
||||
return 0, err
|
||||
} else if _, err := DBExec("DELETE FROM exercice_hints_omcq_deps WHERE id_hint IN (SELECT id_hint FROM exercice_hints WHERE id_exercice = ?)", e.Id); err != nil {
|
||||
|
|
@ -131,20 +131,20 @@ 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 {
|
||||
func (h *EHint) AddDepend(f Flag) (err error) {
|
||||
if d, ok := f.(*FlagKey); ok {
|
||||
_, err = DBExec("INSERT INTO exercice_hints_okey_deps (id_hint, id_flag_dep) VALUES (?, ?)", h.Id, d.Id)
|
||||
} else if d, ok := f.(MCQ); ok {
|
||||
} else if d, ok := f.(*MCQ); ok {
|
||||
_, err = DBExec("INSERT INTO exercice_hints_omcq_deps (id_hint, id_mcq_dep) VALUES (?, ?)", h.Id, d.Id)
|
||||
} else {
|
||||
err = fmt.Errorf("Dependancy type for key (%T) not implemented for this flag.", f)
|
||||
err = fmt.Errorf("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)
|
||||
func (h *EHint) GetDepends() ([]Flag, error) {
|
||||
var deps []Flag
|
||||
|
||||
if rows, err := DBQuery("SELECT id_flag_dep FROM exercice_hints_okey_deps WHERE id_hint = ?", h.Id); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -156,7 +156,7 @@ func (h EHint) GetDepends() ([]Flag, error) {
|
|||
if err := rows.Scan(&d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deps = append(deps, FlagKey{Id: d, IdExercice: h.IdExercice})
|
||||
deps = append(deps, &FlagKey{Id: d, IdExercice: h.IdExercice})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -173,7 +173,7 @@ func (h EHint) GetDepends() ([]Flag, error) {
|
|||
if err := rows.Scan(&d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deps = append(deps, MCQ{Id: d, IdExercice: h.IdExercice})
|
||||
deps = append(deps, &MCQ{Id: d, IdExercice: h.IdExercice})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -184,10 +184,10 @@ func (h EHint) GetDepends() ([]Flag, error) {
|
|||
}
|
||||
|
||||
// GetExercice returns the parent Exercice where this hint can be found.
|
||||
func (h EHint) GetExercice() (Exercice, error) {
|
||||
func (h *EHint) GetExercice() (*Exercice, error) {
|
||||
var eid int64
|
||||
if err := DBQueryRow("SELECT id_exercice FROM exercice_hints WHERE id_hint = ?", h.Id).Scan(&eid); err != nil {
|
||||
return Exercice{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return GetExercice(eid)
|
||||
|
|
|
|||
Reference in a new issue