Add API for keys and files
This commit is contained in:
parent
381aefa597
commit
8655997246
3 changed files with 153 additions and 53 deletions
93
admin/api_exercice.go
Normal file
93
admin/api_exercice.go
Normal file
|
@ -0,0 +1,93 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
)
|
||||
|
||||
type uploadedExercice struct {
|
||||
Title string
|
||||
Statement string
|
||||
Hint string
|
||||
Depend *int
|
||||
Gain int
|
||||
VideoURI string
|
||||
}
|
||||
|
||||
func createExercice(theme fic.Theme, 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 := 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 uploadedFile struct {
|
||||
URI string
|
||||
}
|
||||
|
||||
func createExerciceFile(theme fic.Theme, exercice fic.Exercice, args []string, body []byte) (interface{}, error) {
|
||||
var uf uploadedFile
|
||||
if err := json.Unmarshal(body, &uf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(uf.URI) == 0 {
|
||||
return nil, errors.New("URI not filled")
|
||||
}
|
||||
|
||||
// TODO: se connecter à OwnCloud
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
Reference in a new issue