75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strconv"
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
)
|
|
|
|
type uploadedExercice struct {
|
|
Title string
|
|
Statement string
|
|
Hint string
|
|
Depend *int64
|
|
Gain int
|
|
VideoURI string
|
|
}
|
|
|
|
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)
|
|
}
|