admin: add exercices/ API to access and modify exercices
This commit is contained in:
parent
97386a5d6f
commit
c711f9e78e
2 changed files with 76 additions and 5 deletions
11
admin/api.go
11
admin/api.go
|
@ -13,11 +13,12 @@ import (
|
||||||
type DispatchFunction func([]string, []byte) (interface{}, error)
|
type DispatchFunction func([]string, []byte) (interface{}, error)
|
||||||
|
|
||||||
var apiRoutes = map[string]*(map[string]DispatchFunction){
|
var apiRoutes = map[string]*(map[string]DispatchFunction){
|
||||||
"version": &ApiVersionRouting,
|
"version": &ApiVersionRouting,
|
||||||
"ca": &ApiCARouting,
|
"ca": &ApiCARouting,
|
||||||
"events": &ApiEventsRouting,
|
"events": &ApiEventsRouting,
|
||||||
"themes": &ApiThemesRouting,
|
"exercices": &ApiExercicesRouting,
|
||||||
"teams": &ApiTeamsRouting,
|
"themes": &ApiThemesRouting,
|
||||||
|
"teams": &ApiTeamsRouting,
|
||||||
}
|
}
|
||||||
|
|
||||||
type apiRouting struct{}
|
type apiRouting struct{}
|
||||||
|
|
|
@ -8,6 +8,39 @@ import (
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ApiExercicesRouting = map[string]DispatchFunction{
|
||||||
|
"GET": listExercice,
|
||||||
|
"PATCH": updateExercice,
|
||||||
|
"DELETE": deletionExercice,
|
||||||
|
}
|
||||||
|
|
||||||
|
func listExercice(args []string, body []byte) (interface{}, error) {
|
||||||
|
if len(args) == 1 {
|
||||||
|
if eid, err := strconv.Atoi(string(args[0])); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return fic.GetExercice(int64(eid))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// List all exercices
|
||||||
|
return fic.GetExercices()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func deletionExercice(args []string, body []byte) (interface{}, error) {
|
||||||
|
if len(args) == 1 {
|
||||||
|
if eid, err := strconv.Atoi(string(args[0])); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return exercice.Delete()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type uploadedExercice struct {
|
type uploadedExercice struct {
|
||||||
Title string
|
Title string
|
||||||
Statement string
|
Statement string
|
||||||
|
@ -17,6 +50,43 @@ type uploadedExercice struct {
|
||||||
VideoURI string
|
VideoURI string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateExercice(args []string, body []byte) (interface{}, error) {
|
||||||
|
if len(args) == 1 {
|
||||||
|
if eid, err := strconv.Atoi(string(args[0])); 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.Hint = ue.Hint
|
||||||
|
exercice.Depend = ue.Depend
|
||||||
|
exercice.Gain = int64(ue.Gain)
|
||||||
|
exercice.VideoURI = ue.VideoURI
|
||||||
|
|
||||||
|
return exercice.Update()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createExercice(theme fic.Theme, args []string, body []byte) (interface{}, error) {
|
func createExercice(theme fic.Theme, args []string, body []byte) (interface{}, error) {
|
||||||
if len(args) >= 1 {
|
if len(args) >= 1 {
|
||||||
if eid, err := strconv.Atoi(args[0]); err != nil {
|
if eid, err := strconv.Atoi(args[0]); err != nil {
|
||||||
|
|
Reference in a new issue