2018-11-21 03:10:22 +00:00
|
|
|
package fic
|
|
|
|
|
|
|
|
import ()
|
|
|
|
|
|
|
|
// FlagChoice represents a choice a respond to a classic flag
|
|
|
|
type FlagChoice struct {
|
2019-07-05 20:28:56 +00:00
|
|
|
Id int64 `json:"id"`
|
2018-11-21 03:10:22 +00:00
|
|
|
// IdFlag is the identifier of the underlying flag
|
2019-07-05 20:28:56 +00:00
|
|
|
IdFlag int64 `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.
|
2019-01-02 20:51:09 +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()
|
|
|
|
|
|
|
|
var choices = make([]FlagChoice, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var c FlagChoice
|
|
|
|
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.
|
2019-01-02 20:51:09 +00:00
|
|
|
func (f FlagKey) GetChoice(id int64) (c FlagChoice, err error) {
|
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.
|
2019-07-05 20:28:56 +00:00
|
|
|
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
|
2018-11-21 03:10:22 +00:00
|
|
|
} else {
|
2019-07-05 20:28:56 +00:00
|
|
|
c.Id, err = res.LastInsertId()
|
|
|
|
return c, err
|
2018-11-21 03:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update applies modifications back to the database.
|
|
|
|
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.
|
|
|
|
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 {
|
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
return nb, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WipeFlags deletes flags coming with the challenge.
|
2019-01-02 20:51:09 +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
|
|
|
|
}
|
|
|
|
}
|