2016-12-08 08:12:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
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)))
|
|
|
|
|
|
|
|
router.GET("/api/exercices/:eid/hints", apiHandler(exerciceHandler(listExerciceHints)))
|
|
|
|
router.POST("/api/exercices/:eid/hints", apiHandler(exerciceHandler(createExerciceHint)))
|
|
|
|
|
|
|
|
router.GET("/api/exercices/:eid/keys", apiHandler(exerciceHandler(listExerciceKeys)))
|
|
|
|
router.POST("/api/exercices/:eid/keys", apiHandler(exerciceHandler(createExerciceKey)))
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme.AddExercice(ue.Title, ue.Statement, depend, ue.Gain, ue.VideoURI)
|
|
|
|
}
|
|
|
|
|
|
|
|
type uploadedKey struct {
|
|
|
|
Name string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
func createExerciceKey(exercice fic.Exercice, body []byte) (interface{}, error) {
|
2016-12-08 08:12:18 +00:00
|
|
|
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.Name, uk.Key)
|
|
|
|
}
|
2016-12-04 18:15:39 +00:00
|
|
|
|
2016-12-26 00:14:46 +00:00
|
|
|
func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error) {
|
|
|
|
var uh fic.EHint
|
2016-12-04 18:15:39 +00:00
|
|
|
if err := json.Unmarshal(body, &uh); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(uh.Content) == 0 {
|
|
|
|
return nil, errors.New("Hint's content not filled")
|
|
|
|
}
|
|
|
|
|
|
|
|
return exercice.AddHint(uh.Title, uh.Content, uh.Cost)
|
|
|
|
}
|