Use pointer receiver more offen
This commit is contained in:
parent
6999b4e728
commit
c7569b5e54
59 changed files with 688 additions and 672 deletions
|
|
@ -14,15 +14,15 @@ type FlagChoice struct {
|
|||
}
|
||||
|
||||
// GetChoices returns a list of choices for the given Flag.
|
||||
func (f FlagKey) GetChoices() ([]FlagChoice, error) {
|
||||
func (f *FlagKey) GetChoices() ([]*FlagChoice, error) {
|
||||
if rows, err := DBQuery("SELECT id_choice, id_flag, label, response FROM flag_choices WHERE id_flag = ?", f.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
var choices = make([]FlagChoice, 0)
|
||||
var choices []*FlagChoice
|
||||
for rows.Next() {
|
||||
var c FlagChoice
|
||||
c := &FlagChoice{}
|
||||
c.IdFlag = f.Id
|
||||
|
||||
if err := rows.Scan(&c.Id, &c.IdFlag, &c.Label, &c.Value); err != nil {
|
||||
|
|
@ -40,7 +40,8 @@ func (f FlagKey) GetChoices() ([]FlagChoice, error) {
|
|||
}
|
||||
|
||||
// GetChoice returns a choice for the given Flag.
|
||||
func (f FlagKey) GetChoice(id int) (c FlagChoice, err error) {
|
||||
func (f *FlagKey) GetChoice(id int) (c *FlagChoice, err error) {
|
||||
c = &FlagChoice{}
|
||||
if errr := DBQueryRow("SELECT id_choice, id_flag, label, response FROM flag_choices WHERE id_choice = ?", id).Scan(&c.Id, &c.IdFlag, &c.Label, &c.Value); errr != nil {
|
||||
return c, errr
|
||||
}
|
||||
|
|
@ -48,7 +49,7 @@ func (f FlagKey) GetChoice(id int) (c FlagChoice, err error) {
|
|||
}
|
||||
|
||||
// AddChoice creates and fills a new struct FlagChoice, from a label and a value.
|
||||
func (f FlagKey) AddChoice(c FlagChoice) (FlagChoice, error) {
|
||||
func (f *FlagKey) AddChoice(c *FlagChoice) (*FlagChoice, error) {
|
||||
if res, err := DBExec("INSERT INTO flag_choices (id_flag, label, response) VALUES (?, ?, ?)", f.Id, c.Label, c.Value); err != nil {
|
||||
return c, err
|
||||
} else {
|
||||
|
|
@ -59,7 +60,7 @@ func (f FlagKey) AddChoice(c FlagChoice) (FlagChoice, error) {
|
|||
}
|
||||
|
||||
// Update applies modifications back to the database.
|
||||
func (c FlagChoice) Update() (int64, error) {
|
||||
func (c *FlagChoice) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE flag_choices SET id_flag = ?, label = ?, response = ? WHERE id_choice = ?", c.IdFlag, c.Label, c.Value, c.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
@ -70,7 +71,7 @@ func (c FlagChoice) Update() (int64, error) {
|
|||
}
|
||||
|
||||
// Delete the flag from the database.
|
||||
func (c FlagChoice) Delete() (int64, error) {
|
||||
func (c *FlagChoice) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM flag_choices WHERE id_choice = ?", c.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
@ -81,7 +82,7 @@ func (c FlagChoice) Delete() (int64, error) {
|
|||
}
|
||||
|
||||
// WipeFlags deletes flags coming with the challenge.
|
||||
func (f FlagKey) WipeChoices() (int64, error) {
|
||||
func (f *FlagKey) WipeChoices() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM flag_choices WHERE id_flag = ?", f.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
|
|||
Reference in a new issue