Handle choices in UCQ (db, sync done)

This commit is contained in:
nemunaire 2018-11-21 04:10:22 +01:00 committed by Pierre-Olivier Mercier
parent 333bb408e1
commit 488a032eba
7 changed files with 202 additions and 1 deletions

View file

@ -36,6 +36,11 @@ func init() {
router.GET("/api/exercices/:eid/flags/:kid", apiHandler(flagHandler(showExerciceFlag)))
router.PUT("/api/exercices/:eid/flags/:kid", apiHandler(flagHandler(updateExerciceFlag)))
router.DELETE("/api/exercices/:eid/flags/:kid", apiHandler(flagHandler(deleteExerciceFlag)))
router.GET("/api/exercices/:eid/flags/:kid/choices/", apiHandler(flagHandler(listFlagChoices)))
router.GET("/api/exercices/:eid/flags/:kid/choices/:cid", apiHandler(choiceHandler(showFlagChoice)))
router.POST("/api/exercices/:eid/flags/:kid/choices/", apiHandler(flagHandler(createFlagChoice)))
router.PUT("/api/exercices/:eid/flags/:kid/choices/:cid", apiHandler(choiceHandler(updateFlagChoice)))
router.DELETE("/api/exercices/:eid/flags/:kid/choices/:cid", apiHandler(choiceHandler(deleteFlagChoice)))
router.GET("/api/exercices/:eid/quiz", apiHandler(exerciceHandler(listExerciceQuiz)))
router.GET("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(showExerciceQuiz)))
@ -86,6 +91,10 @@ func listExerciceFlags(exercice fic.Exercice, body []byte) (interface{}, error)
return exercice.GetFlags()
}
func listFlagChoices(flag fic.Flag, _ fic.Exercice, body []byte) (interface{}, error) {
return flag.GetChoices()
}
func listExerciceQuiz(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetMCQ()
}
@ -310,6 +319,51 @@ func deleteExerciceFlag(flag fic.Flag, _ fic.Exercice, _ []byte) (interface{}, e
return flag.Delete()
}
type uploadedChoice struct {
Label string
Value string
}
func createFlagChoice(flag fic.Flag, exercice fic.Exercice, body []byte) (interface{}, error) {
var uc uploadedChoice
if err := json.Unmarshal(body, &uc); err != nil {
return nil, err
}
if len(uc.Label) == 0 {
uc.Label = uc.Value
}
return flag.AddChoice(uc.Label, uc.Value)
}
func showFlagChoice(choice fic.FlagChoice, _ fic.Exercice, body []byte) (interface{}, error) {
return choice, nil
}
func updateFlagChoice(choice fic.FlagChoice, _ fic.Exercice, body []byte) (interface{}, error) {
var uc uploadedChoice
if err := json.Unmarshal(body, &uc); err != nil {
return nil, err
}
if len(uc.Label) == 0 {
choice.Label = uc.Value
} else {
choice.Label = uc.Label
}
if _, err := choice.Update(); err != nil {
return nil, err
}
return choice, nil
}
func deleteFlagChoice(choice fic.FlagChoice, _ fic.Exercice, _ []byte) (interface{}, error) {
return choice.Delete()
}
func showExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, body []byte) (interface{}, error) {
return quiz, nil
}

View file

@ -188,6 +188,26 @@ func flagHandler(f func(fic.Flag, fic.Exercice, []byte) (interface{}, error)) fu
}
}
func choiceHandler(f func(fic.FlagChoice, fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
var exercice fic.Exercice
var flag fic.Flag
flagHandler(func(fl fic.Flag, ex fic.Exercice, _ []byte) (interface{}, error) {
exercice = ex
flag = fl
return nil, nil
})(ps, body)
if cid, err := strconv.Atoi(string(ps.ByName("cid"))); err != nil {
return nil, err
} else if choice, err := flag.GetChoice(cid); err != nil {
return nil, err
} else {
return f(choice, exercice, body)
}
}
}
func quizHandler(f func(fic.MCQ, fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
var exercice fic.Exercice

View file

@ -90,6 +90,26 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) {
continue
}
}
// Import choices
hasOne := false
for cid, choice := range flag.Choice {
if len(choice.Label) == 0 {
choice.Label = choice.Value
}
if _, err := k.AddChoice(choice.Label, choice.Value); err != nil {
errs = append(errs, fmt.Sprintf("%q: error in UCQ %d choice %d: %s", path.Base(exercice.Path), nline + 1, cid, err))
continue
}
if choice.Value == flag.Raw {
hasOne = true
}
}
if !hasOne {
errs = append(errs, fmt.Sprintf("%q: error in UCQ %d: no valid answer defined.", path.Base(exercice.Path), nline + 1))
}
}
}