Handle choices in UCQ (db, sync done)
This commit is contained in:
parent
333bb408e1
commit
488a032eba
7 changed files with 202 additions and 1 deletions
92
libfic/flag_choice.go
Normal file
92
libfic/flag_choice.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package fic
|
||||
|
||||
import ()
|
||||
|
||||
// FlagChoice represents a choice a respond to a classic flag
|
||||
type FlagChoice struct {
|
||||
Id int64 `json:"id"`
|
||||
// IdFlag is the identifier of the underlying flag
|
||||
IdFlag int64 `json:"idFlag"`
|
||||
// Label is the title of the choice as displayed to players
|
||||
Label string `json:"label"`
|
||||
// Value is the raw content that'll be written as response if this choice is selected
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// GetChoices returns a list of choices for the given Flag.
|
||||
func (f Flag) 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)
|
||||
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.
|
||||
func (f Flag) GetChoice(id int) (c FlagChoice, err error) {
|
||||
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.
|
||||
func (f Flag) AddChoice(label string, value string) (FlagChoice, error) {
|
||||
if res, err := DBExec("INSERT INTO flag_choices (id_flag, label, response) VALUES (?, ?, ?)", f.Id, label, value); err != nil {
|
||||
return FlagChoice{}, err
|
||||
} else if cid, err := res.LastInsertId(); err != nil {
|
||||
return FlagChoice{}, err
|
||||
} else {
|
||||
return FlagChoice{cid, f.Id, label, value}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Update applies modifications back to the database.
|
||||
func (c FlagChoice) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE flag_choices SET id_flag = ?, label = ?, value = ? WHERE id_choice = ?", c.IdFlag, c.Label, c.Value, c.Id); err != nil {
|
||||
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.
|
||||
func (f Flag) 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 {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
Reference in a new issue