2016-12-08 08:12:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-11-27 01:45:33 +00:00
|
|
|
"encoding/hex"
|
2016-12-08 08:12:18 +00:00
|
|
|
"errors"
|
2017-01-05 01:21:32 +00:00
|
|
|
"strings"
|
2016-12-08 08:12:18 +00:00
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
2017-11-27 01:45:33 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/sync"
|
2016-12-15 23:51:56 +00:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2016-12-08 08:12:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-12-15 23:51:56 +00:00
|
|
|
router.GET("/api/exercices/", apiHandler(listExercices))
|
|
|
|
|
|
|
|
router.GET("/api/exercices/:eid", apiHandler(exerciceHandler(showExercice)))
|
2016-12-26 00:14:46 +00:00
|
|
|
router.PUT("/api/exercices/:eid", apiHandler(exerciceHandler(updateExercice)))
|
|
|
|
router.DELETE("/api/exercices/:eid", apiHandler(exerciceHandler(deleteExercice)))
|
|
|
|
|
|
|
|
router.GET("/api/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles)))
|
|
|
|
router.POST("/api/exercices/:eid/files", apiHandler(exerciceHandler(createExerciceFile)))
|
2017-12-27 00:53:01 +00:00
|
|
|
router.GET("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(showExerciceFile)))
|
|
|
|
router.DELETE("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(deleteExerciceFile)))
|
2016-12-26 00:14:46 +00:00
|
|
|
|
|
|
|
router.GET("/api/exercices/:eid/hints", apiHandler(exerciceHandler(listExerciceHints)))
|
|
|
|
router.POST("/api/exercices/:eid/hints", apiHandler(exerciceHandler(createExerciceHint)))
|
2016-12-27 20:08:36 +00:00
|
|
|
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)))
|
2016-12-26 00:14:46 +00:00
|
|
|
|
|
|
|
router.GET("/api/exercices/:eid/keys", apiHandler(exerciceHandler(listExerciceKeys)))
|
|
|
|
router.POST("/api/exercices/:eid/keys", apiHandler(exerciceHandler(createExerciceKey)))
|
2016-12-27 20:08:36 +00:00
|
|
|
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)))
|
2017-12-09 01:58:39 +00:00
|
|
|
|
2017-12-17 01:50:01 +00:00
|
|
|
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)))
|
|
|
|
|
2017-12-09 01:58:39 +00:00
|
|
|
|
|
|
|
// Synchronize
|
2018-01-06 14:08:05 +00:00
|
|
|
router.POST("/api/sync/exercices/:eid/files", apiHandler(exerciceHandler(
|
2017-12-09 01:58:39 +00:00
|
|
|
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceFiles(sync.GlobalImporter, exercice), nil })))
|
2018-01-06 14:08:05 +00:00
|
|
|
router.POST("/api/sync/exercices/:eid/hints", apiHandler(exerciceHandler(
|
2017-12-09 01:58:39 +00:00
|
|
|
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceHints(sync.GlobalImporter, exercice), nil })))
|
2018-01-06 14:08:05 +00:00
|
|
|
router.POST("/api/sync/exercices/:eid/keys", apiHandler(exerciceHandler(
|
2017-12-09 01:58:39 +00:00
|
|
|
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceKeys(sync.GlobalImporter, exercice), nil })))
|
2018-01-06 14:08:05 +00:00
|
|
|
router.POST("/api/sync/exercices/:eid/quiz", apiHandler(exerciceHandler(
|
2017-12-16 02:39:57 +00:00
|
|
|
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceMCQ(sync.GlobalImporter, exercice), nil })))
|
2016-12-08 08:12:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 23:51:56 +00:00
|
|
|
func listExercices(_ httprouter.Params, body []byte) (interface{}, error) {
|
2016-12-08 08:12:18 +00:00
|
|
|
// List all exercices
|
|
|
|
return fic.GetExercices()
|
|
|
|
}
|
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
func listExerciceFiles(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
|
|
return exercice.GetFiles()
|
2016-12-08 08:12:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
func listExerciceHints(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
|
|
return exercice.GetHints()
|
2016-12-08 08:12:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
func listExerciceKeys(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
|
|
return exercice.GetKeys()
|
2016-12-08 08:12:18 +00:00
|
|
|
}
|
|
|
|
|
2017-12-17 01:50:01 +00:00
|
|
|
func listExerciceQuiz(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
|
|
return exercice.GetMCQ()
|
|
|
|
}
|
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
func showExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
|
|
return exercice, nil
|
|
|
|
}
|
2016-12-08 08:12:18 +00:00
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
func deleteExercice(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
|
|
|
return exercice.Delete()
|
|
|
|
}
|
2016-12-08 08:12:18 +00:00
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
func updateExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
|
|
var ue fic.Exercice
|
|
|
|
if err := json.Unmarshal(body, &ue); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-08 08:12:18 +00:00
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
ue.Id = exercice.Id
|
2016-12-08 08:12:18 +00:00
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
if len(ue.Title) == 0 {
|
|
|
|
return nil, errors.New("Exercice's title not filled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ue.Update(); err != nil {
|
|
|
|
return nil, err
|
2016-12-08 08:12:18 +00:00
|
|
|
}
|
2016-12-26 00:14:46 +00:00
|
|
|
|
|
|
|
return ue, nil
|
2016-12-08 08:12:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 23:51:56 +00:00
|
|
|
func createExercice(theme fic.Theme, body []byte) (interface{}, error) {
|
2016-12-08 08:12:18 +00:00
|
|
|
// Create a new exercice
|
2016-12-26 00:14:46 +00:00
|
|
|
var ue fic.Exercice
|
2016-12-08 08:12:18 +00:00
|
|
|
if err := json.Unmarshal(body, &ue); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ue.Title) == 0 {
|
|
|
|
return nil, errors.New("Title not filled")
|
|
|
|
}
|
|
|
|
|
|
|
|
var depend *fic.Exercice = nil
|
|
|
|
if ue.Depend != nil {
|
|
|
|
if d, err := fic.GetExercice(*ue.Depend); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
depend = &d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 10:07:50 +00:00
|
|
|
return theme.AddExercice(ue.Title, ue.URLId, ue.Path, ue.Statement, ue.Overview, depend, ue.Gain, ue.VideoURI)
|
2016-12-08 08:12:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:15:39 +00:00
|
|
|
|
2017-01-05 01:21:32 +00:00
|
|
|
type uploadedHint struct {
|
|
|
|
Title string
|
2017-12-08 23:52:15 +00:00
|
|
|
Path string
|
2017-01-05 01:21:32 +00:00
|
|
|
Content string
|
|
|
|
Cost int64
|
2017-01-19 13:20:34 +00:00
|
|
|
URI string
|
2017-01-05 01:21:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error) {
|
2017-01-05 01:21:32 +00:00
|
|
|
var uh uploadedHint
|
2016-12-04 18:15:39 +00:00
|
|
|
if err := json.Unmarshal(body, &uh); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-05 01:21:32 +00:00
|
|
|
if len(uh.Content) != 0 {
|
|
|
|
return exercice.AddHint(uh.Title, uh.Content, uh.Cost)
|
2017-11-27 01:45:33 +00:00
|
|
|
} else if len(uh.URI) != 0 {
|
2017-12-12 05:22:22 +00:00
|
|
|
return sync.ImportFile(sync.GlobalImporter, uh.URI,
|
2017-11-27 01:45:33 +00:00
|
|
|
func(filePath string, origin string) (interface{}, error) {
|
2017-01-05 01:21:32 +00:00
|
|
|
return exercice.AddHint(uh.Title, "$FILES" + strings.TrimPrefix(filePath, fic.FilesDir), uh.Cost)
|
|
|
|
})
|
|
|
|
} else {
|
2016-12-04 18:15:39 +00:00
|
|
|
return nil, errors.New("Hint's content not filled")
|
|
|
|
}
|
|
|
|
}
|
2016-12-27 20:08:36 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 04:06:34 +00:00
|
|
|
type uploadedKey struct {
|
|
|
|
Label string
|
|
|
|
Key string
|
|
|
|
Hash []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func createExerciceKey(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
|
|
var uk uploadedKey
|
|
|
|
if err := json.Unmarshal(body, &uk); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(uk.Key) == 0 {
|
|
|
|
return nil, errors.New("Key not filled")
|
|
|
|
}
|
|
|
|
|
|
|
|
return exercice.AddRawKey(uk.Label, uk.Key)
|
|
|
|
}
|
|
|
|
|
2016-12-27 20:08:36 +00:00
|
|
|
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) {
|
2017-12-12 04:06:34 +00:00
|
|
|
var uk uploadedKey
|
2016-12-27 20:08:36 +00:00
|
|
|
if err := json.Unmarshal(body, &uk); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-12 04:06:34 +00:00
|
|
|
if len(uk.Label) == 0 {
|
|
|
|
key.Label = "Flag"
|
|
|
|
} else {
|
|
|
|
key.Label = uk.Label
|
2016-12-27 20:08:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 04:06:34 +00:00
|
|
|
if _, err := key.Update(); err != nil {
|
2016-12-27 20:08:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-12 04:06:34 +00:00
|
|
|
return key, nil
|
2016-12-27 20:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteExerciceKey(key fic.Key, _ fic.Exercice, _ []byte) (interface{}, error) {
|
|
|
|
return key.Delete()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-17 01:50:01 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2017-12-27 00:53:01 +00:00
|
|
|
type uploadedFile struct {
|
|
|
|
URI string
|
|
|
|
Digest string
|
|
|
|
}
|
|
|
|
|
2017-01-05 01:21:32 +00:00
|
|
|
func createExerciceFile(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
|
|
var uf uploadedFile
|
|
|
|
if err := json.Unmarshal(body, &uf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-12 05:22:22 +00:00
|
|
|
return sync.ImportFile(sync.GlobalImporter, uf.URI,
|
2017-11-27 01:45:33 +00:00
|
|
|
func(filePath string, origin string) (interface{}, error) {
|
|
|
|
if digest, err := hex.DecodeString(uf.Digest); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return exercice.ImportFile(filePath, origin, digest)
|
|
|
|
}
|
|
|
|
})
|
2017-01-05 01:21:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 20:08:36 +00:00
|
|
|
func showExerciceFile(file fic.EFile, body []byte) (interface{}, error) {
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteExerciceFile(file fic.EFile, _ []byte) (interface{}, error) {
|
|
|
|
return file.Delete()
|
|
|
|
}
|