fic: Add Order, Help and Type values in struct

This commit is contained in:
nemunaire 2021-08-30 18:33:14 +02:00
commit 74e8c3801a
16 changed files with 134 additions and 110 deletions

View file

@ -8,9 +8,11 @@ import (
// MCQ represents a flag's challenge, in the form of checkbox.
type MCQ struct {
Id int64 `json:"id"`
Id int `json:"id"`
// IdExercice is the identifier of the underlying challenge
IdExercice int64 `json:"idExercice"`
// Order is used to sort the flag between them
Order int8 `json:"order"`
// Title is the label of the question
Title string `json:"title"`
// Entries stores the set of proposed answers
@ -19,7 +21,7 @@ type MCQ struct {
// MCQ_entry represents a proposed response for a given MCQ.
type MCQ_entry struct {
Id int64 `json:"id"`
Id int `json:"id"`
// Label is the text displayed to players as proposed answer
Label string `json:"label"`
// Response stores if expected checked state.
@ -27,8 +29,8 @@ type MCQ_entry struct {
}
// GetMCQ returns a list of flags comming with the challenge.
func GetMCQ(id int64) (m MCQ, err error) {
err = DBQueryRow("SELECT id_mcq, id_exercice, title FROM exercice_mcq WHERE id_mcq = ?", id).Scan(&m.Id, &m.IdExercice, &m.Title)
func GetMCQ(id int) (m MCQ, err error) {
err = DBQueryRow("SELECT id_mcq, id_exercice, order, title FROM exercice_mcq WHERE id_mcq = ?", id).Scan(&m.Id, &m.IdExercice, &m.Order, &m.Title)
m.fillEntries()
return
}
@ -55,7 +57,7 @@ func (m *MCQ) fillEntries() ([]MCQ_entry, error) {
// GetMCQ returns the MCQs coming with the challenge.
func (e Exercice) GetMCQ() ([]MCQ, error) {
if rows, err := DBQuery("SELECT id_mcq, id_exercice, title FROM exercice_mcq WHERE id_exercice = ?", e.Id); err != nil {
if rows, err := DBQuery("SELECT id_mcq, id_exercice, ordre, title FROM exercice_mcq WHERE id_exercice = ?", e.Id); err != nil {
return nil, err
} else {
defer rows.Close()
@ -65,7 +67,7 @@ func (e Exercice) GetMCQ() ([]MCQ, error) {
var m MCQ
m.IdExercice = e.Id
if err := rows.Scan(&m.Id, &m.IdExercice, &m.Title); err != nil {
if err := rows.Scan(&m.Id, &m.IdExercice, &m.Order, &m.Title); err != nil {
return nil, err
}
@ -82,8 +84,8 @@ func (e Exercice) GetMCQ() ([]MCQ, error) {
}
// GetMCQbyChoice returns the MCQ corresponding to a choice ID.
func GetMCQbyChoice(cid int64) (m MCQ, c MCQ_entry, err error) {
if errr := DBQueryRow("SELECT id_mcq, id_exercice, title FROM exercice_mcq WHERE id_mcq = (SELECT id_mcq FROM mcq_entries WHERE id_mcq_entry = ?)", cid).Scan(&m.Id, &m.IdExercice, &m.Title); errr != nil {
func GetMCQbyChoice(cid int) (m MCQ, c MCQ_entry, err error) {
if errr := DBQueryRow("SELECT id_mcq, id_exercice, ordre, title FROM exercice_mcq WHERE id_mcq = (SELECT id_mcq FROM mcq_entries WHERE id_mcq_entry = ?)", cid).Scan(&m.Id, &m.IdExercice, &m.Order, &m.Title); errr != nil {
return MCQ{}, MCQ_entry{}, errr
}
@ -111,7 +113,7 @@ func GetMCQbyChoice(cid int64) (m MCQ, c MCQ_entry, err error) {
}
// GetId returns the MCQ identifier.
func (m MCQ) GetId() int64 {
func (m MCQ) GetId() int {
return m.Id
}
@ -126,12 +128,12 @@ func (m MCQ) RecoverId() (Flag, error) {
// 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 {
if res, err := DBExec("INSERT INTO exercice_mcq (id_exercice, ordre, title) VALUES (?, ?, ?)", e.Id, m.Order, m.Title); err != nil {
return m, err
} else if qid, err := res.LastInsertId(); err != nil {
return m, err
} else {
m.Id = qid
m.Id = int(qid)
m.IdExercice = e.Id
// Add entries
@ -149,7 +151,7 @@ func (m MCQ) Create(e Exercice) (Flag, error) {
// Update applies modifications back to the database.
func (m MCQ) Update() (int64, error) {
if res, err := DBExec("UPDATE exercice_mcq SET id_exercice = ?, title = ? WHERE id_mcq = ?", m.IdExercice, m.Title, m.Id); err != nil {
if res, err := DBExec("UPDATE exercice_mcq SET id_exercice = ?, ordre = ?, title = ? WHERE id_mcq = ?", m.IdExercice, m.Order, m.Title, m.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
@ -186,7 +188,7 @@ func (m MCQ) AddEntry(e MCQ_entry) (MCQ_entry, error) {
} else if nid, err := res.LastInsertId(); err != nil {
return e, err
} else {
e.Id = nid
e.Id = int(nid)
return e, nil
}
}
@ -236,6 +238,10 @@ func (e Exercice) WipeMCQs() (int64, error) {
}
}
func (m MCQ) GetOrder() int8 {
return m.Order
}
// AddDepend insert a new dependency to a given flag.
func (m MCQ) AddDepend(j Flag) (err error) {
if d, ok := j.(FlagKey); ok {
@ -258,7 +264,7 @@ func (m MCQ) GetDepends() ([]Flag, error) {
defer rows.Close()
for rows.Next() {
var d int64
var d int
if err := rows.Scan(&d); err != nil {
return nil, err
}
@ -276,7 +282,7 @@ func (m MCQ) GetDepends() ([]Flag, error) {
defer rows.Close()
for rows.Next() {
var d int64
var d int
if err := rows.Scan(&d); err != nil {
return nil, err
}
@ -298,8 +304,8 @@ func (c MCQ_entry) GetJustifiedFlag(e Exercice) (FlagKey, error) {
// Check if the given vals are the expected ones to validate this flag.
func (m MCQ) Check(v interface{}) int {
var vals map[int64]bool
if va, ok := v.(map[int64]bool); !ok {
var vals map[int]bool
if va, ok := v.(map[int]bool); !ok {
return -1
} else {
vals = va