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
|
@ -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
|
||||
}
|
||||
|
|
Reference in a new issue