Write docs!

This commit is contained in:
nemunaire 2018-03-09 19:07:08 +01:00
commit bcc598ebd5
37 changed files with 478 additions and 188 deletions

View file

@ -4,19 +4,27 @@ import (
"time"
)
// MCQ represents a flag's challenge, in the form of checkbox.
type MCQ struct {
Id int64 `json:"id"`
// IdExercice is the identifier of the underlying challenge
IdExercice int64 `json:"idExercice"`
// Title is the label of the question
Title string `json:"title"`
// Entries stores the set of proposed answers
Entries []MCQ_entry `json:"entries"`
}
// MCQ_entry represents a proposed response for a given MCQ.
type MCQ_entry struct {
Id int64 `json:"id"`
// Label is the text displayed to players as proposed answer
Label string `json:"label"`
// Response stores if expected checked state.
Response bool `json:"response"`
}
// 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 {
return nil, err
@ -58,6 +66,7 @@ func (e Exercice) GetMCQ() ([]MCQ, error) {
}
}
// AddMCQ creates and fills a new struct MCQ and registers it into the database.
func (e Exercice) AddMCQ(title string) (MCQ, error) {
if res, err := DBExec("INSERT INTO exercice_mcq (id_exercice, title) VALUES (?, ?)", e.Id, title); err != nil {
return MCQ{}, err
@ -68,6 +77,7 @@ func (e Exercice) AddMCQ(title string) (MCQ, 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 {
return 0, err
@ -78,6 +88,7 @@ func (m MCQ) Update() (int64, error) {
}
}
// Delete the MCQ from the database.
func (m MCQ) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM exercice_mcq WHERE id_mcq = ?", m.Id); err != nil {
return 0, err
@ -88,6 +99,7 @@ func (m MCQ) Delete() (int64, error) {
}
}
// AddEntry creates and fills a new struct MCQ_entry and registers it into the database.
func (m MCQ) AddEntry(label string, response bool) (MCQ_entry, error) {
if res, err := DBExec("INSERT INTO mcq_entries (id_mcq, label, response) VALUES (?, ?, ?)", m.Id, label, response); err != nil {
return MCQ_entry{}, err
@ -98,6 +110,7 @@ func (m MCQ) AddEntry(label string, response bool) (MCQ_entry, error) {
}
}
// Update applies modifications back to the database.
func (n MCQ_entry) Update() (int64, error) {
if res, err := DBExec("UPDATE mcq_entries SET label = ?, response = ? WHERE id_mcq = ?", n.Label, n.Response, n.Id); err != nil {
return 0, err
@ -108,6 +121,7 @@ func (n MCQ_entry) Update() (int64, error) {
}
}
// Delete the MCQ entry from the database.
func (n MCQ_entry) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM mcq_entries WHERE id_mcq_entry = ?", n.Id); err != nil {
return 0, err
@ -118,6 +132,7 @@ func (n MCQ_entry) Delete() (int64, error) {
}
}
// WipeMCQs deletes MCQs coming with the challenge.
func (e Exercice) WipeMCQs() (int64, error) {
if res, err := DBExec("DELETE FROM exercice_mcq, mcq_found, mcq_entries USING exercice_mcq NATURAL JOIN mcq_entries NATURAL JOIN mcq_found WHERE exercice_mcq.id_exercice = ?", e.Id); err != nil {
return 0, err
@ -128,6 +143,7 @@ func (e Exercice) WipeMCQs() (int64, error) {
}
}
// Check if the given vals are the expected ones to validate this flag.
func (m MCQ) Check(vals map[int64]bool) int {
diff := 0
@ -141,6 +157,7 @@ func (m MCQ) Check(vals map[int64]bool) int {
return diff
}
// FoundBy registers in the database that the given Team solved the MCQ.
func (m MCQ) FoundBy(t Team) {
DBExec("INSERT INTO mcq_found (id_mcq, id_team, time) VALUES (?, ?, ?)", m.Id, t.Id, time.Now())
}