server/admin/api_exercice.go

145 lines
3.2 KiB
Go

package main
import (
"encoding/json"
"errors"
"strconv"
"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
Hint string
Depend *int64
Gain int
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 {
return nil, err
} else if exercice, err := theme.GetExercice(eid); err != nil {
return nil, err
} else {
if args[1] == "files" {
return createExerciceFile(theme, exercice, args[2:], body)
} else if args[1] == "keys" {
return createExerciceKey(theme, exercice, args[2:], body)
}
}
return nil, nil
} else {
// 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, ue.Hint, depend, ue.Gain, ue.VideoURI)
}
}
type uploadedKey struct {
Name string
Key string
}
func createExerciceKey(theme fic.Theme, exercice fic.Exercice, args []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)
}