admin: Add exercice's tags: sync, api, interface done

This commit is contained in:
nemunaire 2018-11-18 22:44:23 +01:00 committed by Pierre-Olivier Mercier
parent 665fd301c6
commit f183985982
10 changed files with 166 additions and 20 deletions

View file

@ -41,6 +41,10 @@ func init() {
router.PUT("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(updateExerciceQuiz)))
router.DELETE("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(deleteExerciceQuiz)))
router.GET("/api/exercices/:eid/tags", apiHandler(exerciceHandler(listExerciceTags)))
router.POST("/api/exercices/:eid/tags", apiHandler(exerciceHandler(addExerciceTag)))
router.PUT("/api/exercices/:eid/tags", apiHandler(exerciceHandler(updateExerciceTags)))
// Synchronize
router.POST("/api/sync/exercices/:eid/files", apiHandler(exerciceHandler(
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
@ -352,3 +356,29 @@ func showExerciceFile(file fic.EFile, body []byte) (interface{}, error) {
func deleteExerciceFile(file fic.EFile, _ []byte) (interface{}, error) {
return file.Delete()
}
func listExerciceTags(exercice fic.Exercice, _ []byte) (interface{}, error) {
return exercice.GetTags()
}
func addExerciceTag(exercice fic.Exercice, body []byte) (interface{}, error) {
var ut []string
if err := json.Unmarshal(body, &ut); err != nil {
return nil, err
}
// TODO: a DB transaction should be done here: on error we should rollback
for _, t := range ut {
if _, err := exercice.AddTag(t); err != nil {
return nil, err
}
}
return ut, nil
}
func updateExerciceTags(exercice fic.Exercice, body []byte) (interface{}, error) {
exercice.WipeTags()
return addExerciceTag(exercice, body)
}