admin: add exercices/ API to access and modify exercices
This commit is contained in:
parent
97386a5d6f
commit
c711f9e78e
11
admin/api.go
11
admin/api.go
|
@ -13,11 +13,12 @@ import (
|
|||
type DispatchFunction func([]string, []byte) (interface{}, error)
|
||||
|
||||
var apiRoutes = map[string]*(map[string]DispatchFunction){
|
||||
"version": &ApiVersionRouting,
|
||||
"ca": &ApiCARouting,
|
||||
"events": &ApiEventsRouting,
|
||||
"themes": &ApiThemesRouting,
|
||||
"teams": &ApiTeamsRouting,
|
||||
"version": &ApiVersionRouting,
|
||||
"ca": &ApiCARouting,
|
||||
"events": &ApiEventsRouting,
|
||||
"exercices": &ApiExercicesRouting,
|
||||
"themes": &ApiThemesRouting,
|
||||
"teams": &ApiTeamsRouting,
|
||||
}
|
||||
|
||||
type apiRouting struct{}
|
||||
|
|
|
@ -8,6 +8,39 @@ import (
|
|||
"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 {
|
||||
Title string
|
||||
Statement string
|
||||
|
@ -17,6 +50,43 @@ type uploadedExercice struct {
|
|||
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) {
|
||||
if len(args) >= 1 {
|
||||
if eid, err := strconv.Atoi(args[0]); err != nil {
|
||||
|
|
Loading…
Reference in New Issue
Block a user