From f211278c8bc40a325b94e834bd88a6f83481df3c Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 17 Dec 2017 02:50:01 +0100 Subject: [PATCH] admin: add route to handle quiz --- admin/api/exercice.go | 18 +++++++++++++++++- admin/api/handlers.go | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/admin/api/exercice.go b/admin/api/exercice.go index d5b9e71f..681e1e3a 100644 --- a/admin/api/exercice.go +++ b/admin/api/exercice.go @@ -36,6 +36,10 @@ func init() { router.PUT("/api/exercices/:eid/keys/:kid", apiHandler(keyHandler(updateExerciceKey))) router.DELETE("/api/exercices/:eid/keys/:kid", apiHandler(keyHandler(deleteExerciceKey))) + router.GET("/api/exercices/:eid/quiz", apiHandler(exerciceHandler(listExerciceQuiz))) + router.GET("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(showExerciceQuiz))) + router.DELETE("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(deleteExerciceQuiz))) + // Synchronize router.GET("/api/sync/exercices/:eid/files", apiHandler(exerciceHandler( @@ -65,6 +69,10 @@ func listExerciceKeys(exercice fic.Exercice, body []byte) (interface{}, error) { return exercice.GetKeys() } +func listExerciceQuiz(exercice fic.Exercice, body []byte) (interface{}, error) { + return exercice.GetMCQ() +} + func showExercice(exercice fic.Exercice, body []byte) (interface{}, error) { return exercice, nil } @@ -112,7 +120,7 @@ func createExercice(theme fic.Theme, body []byte) (interface{}, error) { } } - return theme.AddExercice(ue.Title, ue.Path, ue.Statement, depend, ue.Gain, ue.VideoURI) + return theme.AddExercice(ue.Title, ue.Path, ue.Statement, ue.Overview, depend, ue.Gain, ue.VideoURI) } @@ -217,6 +225,14 @@ func deleteExerciceKey(key fic.Key, _ fic.Exercice, _ []byte) (interface{}, erro } +func showExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, body []byte) (interface{}, error) { + return quiz, nil +} + +func deleteExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, _ []byte) (interface{}, error) { + return quiz.Delete() +} + func createExerciceFile(exercice fic.Exercice, body []byte) (interface{}, error) { var uf uploadedFile if err := json.Unmarshal(body, &uf); err != nil { diff --git a/admin/api/handlers.go b/admin/api/handlers.go index 21ee559e..b41bbcd2 100644 --- a/admin/api/handlers.go +++ b/admin/api/handlers.go @@ -187,6 +187,29 @@ func keyHandler(f func(fic.Key,fic.Exercice,[]byte) (interface{}, error)) func ( } } +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 + exerciceHandler(func (ex fic.Exercice, _[]byte) (interface{}, error) { + exercice = ex + return nil,nil + })(ps, body) + + if qid, err := strconv.Atoi(string(ps.ByName("qid"))); err != nil { + return nil, err + } else if mcqs, err := exercice.GetMCQ(); err != nil { + return nil, err + } else { + for _, mcq := range mcqs { + if (mcq.Id == int64(qid)) { + return f(mcq, exercice, body) + } + } + return nil, errors.New("Unable to find the requested key") + } + } +} + func fileHandler(f func(fic.EFile,[]byte) (interface{}, error)) func (httprouter.Params,[]byte) (interface{}, error) { return func (ps httprouter.Params, body []byte) (interface{}, error) { var exercice fic.Exercice