[admin] Add new routes to manage hints, files and keys

This commit is contained in:
nemunaire 2016-12-27 21:08:36 +01:00
commit c64f1969d9
7 changed files with 259 additions and 43 deletions

View file

@ -18,12 +18,20 @@ func init() {
router.GET("/api/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles)))
router.POST("/api/exercices/:eid/files", apiHandler(exerciceHandler(createExerciceFile)))
router.GET("/api/exercices/:eid/files/:fid", apiHandler(fileHandler(showExerciceFile)))
router.DELETE("/api/exercices/:eid/files/:fid", apiHandler(fileHandler(deleteExerciceFile)))
router.GET("/api/exercices/:eid/hints", apiHandler(exerciceHandler(listExerciceHints)))
router.POST("/api/exercices/:eid/hints", apiHandler(exerciceHandler(createExerciceHint)))
router.GET("/api/exercices/:eid/hints/:hid", apiHandler(hintHandler(showExerciceHint)))
router.PUT("/api/exercices/:eid/hints/:hid", apiHandler(hintHandler(updateExerciceHint)))
router.DELETE("/api/exercices/:eid/hints/:hid", apiHandler(hintHandler(deleteExerciceHint)))
router.GET("/api/exercices/:eid/keys", apiHandler(exerciceHandler(listExerciceKeys)))
router.POST("/api/exercices/:eid/keys", apiHandler(exerciceHandler(createExerciceKey)))
router.GET("/api/exercices/:eid/keys/:kid", apiHandler(keyHandler(showExerciceKey)))
router.PUT("/api/exercices/:eid/keys/:kid", apiHandler(keyHandler(updateExerciceKey)))
router.DELETE("/api/exercices/:eid/keys/:kid", apiHandler(keyHandler(deleteExerciceKey)))
}
func listExercices(_ httprouter.Params, body []byte) (interface{}, error) {
@ -94,7 +102,7 @@ func createExercice(theme fic.Theme, body []byte) (interface{}, error) {
}
type uploadedKey struct {
Name string
Type string
Key string
}
@ -108,7 +116,7 @@ func createExerciceKey(exercice fic.Exercice, body []byte) (interface{}, error)
return nil, errors.New("Key not filled")
}
return exercice.AddRawKey(uk.Name, uk.Key)
return exercice.AddRawKey(uk.Type, uk.Key)
}
func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error) {
@ -123,3 +131,68 @@ func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error)
return exercice.AddHint(uh.Title, uh.Content, uh.Cost)
}
func showExerciceHint(hint fic.EHint, body []byte) (interface{}, error) {
return hint, nil
}
func updateExerciceHint(hint fic.EHint, body []byte) (interface{}, error) {
var uh fic.EHint
if err := json.Unmarshal(body, &uh); err != nil {
return nil, err
}
uh.Id = hint.Id
if len(uh.Title) == 0 {
return nil, errors.New("Hint's title not filled")
}
if _, err := uh.Update(); err != nil {
return nil, err
}
return uh, nil
}
func deleteExerciceHint(hint fic.EHint, _ []byte) (interface{}, error) {
return hint.Delete()
}
func showExerciceKey(key fic.Key, _ fic.Exercice, body []byte) (interface{}, error) {
return key, nil
}
func updateExerciceKey(key fic.Key, exercice fic.Exercice, body []byte) (interface{}, error) {
var uk fic.Key
if err := json.Unmarshal(body, &uk); err != nil {
return nil, err
}
uk.Id = key.Id
uk.IdExercice = exercice.Id
if len(uk.Type) == 0 {
uk.Type = "Flag"
}
if _, err := uk.Update(); err != nil {
return nil, err
}
return uk, nil
}
func deleteExerciceKey(key fic.Key, _ fic.Exercice, _ []byte) (interface{}, error) {
return key.Delete()
}
func showExerciceFile(file fic.EFile, body []byte) (interface{}, error) {
return file, nil
}
func deleteExerciceFile(file fic.EFile, _ []byte) (interface{}, error) {
return file.Delete()
}