admin/api: use gorilla/mux instead of Go router
This commit is contained in:
parent
0e30259b7e
commit
307c253d7a
18 changed files with 643 additions and 720 deletions
117
admin/api/exercice.go
Normal file
117
admin/api/exercice.go
Normal file
|
@ -0,0 +1,117 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.Path("/exercices/").Methods("GET").HandlerFunc(apiHandler(listExercices))
|
||||
re := router.Path("/exercices/{eid:[0-9]+}").Subrouter()
|
||||
re.Methods("GET").HandlerFunc(apiHandler(exerciceHandler(showExercice)))
|
||||
re.Methods("PUT").HandlerFunc(apiHandler(updateExercice))
|
||||
re.Methods("DELETE").HandlerFunc(apiHandler(deleteExercice))
|
||||
}
|
||||
|
||||
func listExercices(args map[string]string, body []byte) (interface{}, error) {
|
||||
// List all exercices
|
||||
return fic.GetExercices()
|
||||
}
|
||||
|
||||
func showExercice(exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
|
||||
return exercice, nil
|
||||
}
|
||||
|
||||
func deleteExercice(args map[string]string, body []byte) (interface{}, error) {
|
||||
if eid, err := strconv.Atoi(args["eid"]); err != nil {
|
||||
return nil, err
|
||||
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return exercice.Delete()
|
||||
}
|
||||
}
|
||||
|
||||
type uploadedExercice struct {
|
||||
Title string
|
||||
Statement string
|
||||
Depend *int64
|
||||
Gain int
|
||||
VideoURI string
|
||||
}
|
||||
|
||||
func updateExercice(args map[string]string, body []byte) (interface{}, error) {
|
||||
if eid, err := strconv.Atoi(args["eid"]); err != nil {
|
||||
return nil, err
|
||||
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
// Update an exercice
|
||||
var ue uploadedExercice
|
||||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(ue.Title) == 0 {
|
||||
return nil, errors.New("Exercice's title not filled")
|
||||
}
|
||||
|
||||
if ue.Depend != nil {
|
||||
if _, err := fic.GetExercice(*ue.Depend); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
exercice.Title = ue.Title
|
||||
exercice.Statement = ue.Statement
|
||||
exercice.Depend = ue.Depend
|
||||
exercice.Gain = int64(ue.Gain)
|
||||
exercice.VideoURI = ue.VideoURI
|
||||
|
||||
return exercice.Update()
|
||||
}
|
||||
}
|
||||
|
||||
func createExercice(theme fic.Theme, args map[string]string, body []byte) (interface{}, error) {
|
||||
// Create a new exercice
|
||||
var ue uploadedExercice
|
||||
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
|
||||
}
|
||||
|
||||
func createExerciceKey(theme fic.Theme, exercice fic.Exercice, args map[string]string, 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.Name, uk.Key)
|
||||
}
|
Reference in a new issue