2018-11-21 03:10:22 +00:00
|
|
|
package fic
|
|
|
|
|
|
|
|
import ()
|
|
|
|
|
|
|
|
// FlagChoice represents a choice a respond to a classic flag
|
|
|
|
type FlagChoice struct {
|
2021-08-30 16:33:14 +00:00
|
|
|
Id int `json:"id"`
|
2018-11-21 03:10:22 +00:00
|
|
|
// IdFlag is the identifier of the underlying flag
|
2021-08-30 16:33:14 +00:00
|
|
|
IdFlag int `json:"idFlag"`
|
2018-11-21 03:10:22 +00:00
|
|
|
// Label is the title of the choice as displayed to players
|
2019-07-05 20:28:56 +00:00
|
|
|
Label string `json:"label"`
|
2018-11-21 03:10:22 +00:00
|
|
|
// Value is the raw content that'll be written as response if this choice is selected
|
2019-07-05 20:28:56 +00:00
|
|
|
Value string `json:"value"`
|
2018-11-21 03:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetChoices returns a list of choices for the given Flag.
|
2021-11-22 14:35:07 +00:00
|
|
|
func (f *FlagKey) GetChoices() ([]*FlagChoice, error) {
|
2018-11-21 03:10:22 +00:00
|
|
|
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()
|
|
|
|
|
2021-11-22 14:35:07 +00:00
|
|
|
var choices []*FlagChoice
|
2018-11-21 03:10:22 +00:00
|
|
|
for rows.Next() {
|
2021-11-22 14:35:07 +00:00
|
|
|
c := &FlagChoice{}
|
2018-11-21 03:10:22 +00:00
|
|
|
c.IdFlag = f.Id
|
|
|
|
|
|
|
|
if err := rows.Scan(&c.Id, &c.IdFlag, &c.Label, &c.Value); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
choices = append(choices, c)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return choices, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChoice returns a choice for the given Flag.
|
2021-11-22 14:35:07 +00:00
|
|
|
func (f *FlagKey) GetChoice(id int) (c *FlagChoice, err error) {
|
|
|
|
c = &FlagChoice{}
|
2018-11-21 03:10:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddChoice creates and fills a new struct FlagChoice, from a label and a value.
|
2021-11-22 14:35:07 +00:00
|
|
|
func (f *FlagKey) AddChoice(c *FlagChoice) (*FlagChoice, error) {
|
2019-07-05 20:28:56 +00:00
|
|
|
if res, err := DBExec("INSERT INTO flag_choices (id_flag, label, response) VALUES (?, ?, ?)", f.Id, c.Label, c.Value); err != nil {
|
|
|
|
return c, err
|
2018-11-21 03:10:22 +00:00
|
|
|
} else {
|
2021-08-30 16:33:14 +00:00
|
|
|
cid, err := res.LastInsertId()
|
|
|
|
c.Id = int(cid)
|
2019-07-05 20:28:56 +00:00
|
|
|
return c, err
|
2018-11-21 03:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update applies modifications back to the database.
|
2021-11-22 14:35:07 +00:00
|
|
|
func (c *FlagChoice) Update() (int64, error) {
|
2020-01-28 09:49:12 +00:00
|
|
|
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 {
|
2018-11-21 03:10:22 +00:00
|
|
|
return 0, err
|
|
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
return nb, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the flag from the database.
|
2021-11-22 14:35:07 +00:00
|
|
|
func (c *FlagChoice) Delete() (int64, error) {
|
2018-11-21 03:10:22 +00:00
|
|
|
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 {
|
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
return nb, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WipeFlags deletes flags coming with the challenge.
|
2021-11-22 14:35:07 +00:00
|
|
|
func (f *FlagKey) WipeChoices() (int64, error) {
|
2018-11-21 03:10:22 +00:00
|
|
|
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 {
|
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
return nb, err
|
|
|
|
}
|
|
|
|
}
|