libfic: add new functions to retrieve the Id of some contents from Title, Label, ...

This commit is contained in:
Pierre-Olivier Mercier 2019-11-25 14:19:29 +01:00
parent fbae34ee4f
commit 698c2f1a47
4 changed files with 30 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import ()
type Flag interface {
GetId() int64
RecoverId() (Flag, error)
Create(e Exercice) (Flag, error)
Update() (int64, error)
Delete() (int64, error)

View File

@ -127,6 +127,15 @@ func (k FlagKey) GetId() int64 {
return k.Id
}
// RecoverId returns the Flag identifier as register in DB.
func (k FlagKey) RecoverId() (Flag, error) {
if err := DBQueryRow("SELECT id_flag FROM exercice_flags WHERE type LIKE ? AND id_exercice = ?", k.Label, k.IdExercice).Scan(&k.Id); err != nil {
return FlagKey{}, err
} else {
return k, err
}
}
// AddFlagKey creates and fills a new struct Flag, from a hashed flag, and registers it into the database.
func (k FlagKey) Create(e Exercice) (Flag, error) {
// Check the regexp compile

View File

@ -47,6 +47,17 @@ func GetHint(id int64) (EHint, error) {
return h, nil
}
// GetHintByTitle retrieves the hint with the given id.
func (e Exercice) GetHintByTitle(id int64) (EHint, error) {
var 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
}
treatHintContent(&h)
return h, nil
}
// GetHints returns a list of hints comming with the challenge.
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 {

View File

@ -102,6 +102,15 @@ func (m MCQ) GetId() int64 {
return m.Id
}
// RecoverId returns the MCQ identifier as register in DB.
func (m MCQ) RecoverId() (Flag, error) {
if err := DBQueryRow("SELECT id_mcq FROM exercice_mcq WHERE title LIKE ? AND id_exercice = ?", m.Title, m.IdExercice).Scan(&m.Id); err != nil {
return MCQ{}, err
} else {
return m, err
}
}
// Create registers a MCQ into the database and recursively add its entries.
func (m MCQ) Create(e Exercice) (Flag, error) {
if res, err := DBExec("INSERT INTO exercice_mcq (id_exercice, title) VALUES (?, ?)", e.Id, m.Title); err != nil {