Add API for keys and files
This commit is contained in:
parent
381aefa597
commit
8655997246
|
@ -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)
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
)
|
||||
|
@ -15,41 +16,55 @@ var ApiTeamsRouting = map[string]DispatchFunction{
|
|||
"DELETE": deletionTeam,
|
||||
}
|
||||
|
||||
type uploadedTeam struct {
|
||||
Name string
|
||||
type myTeamFile struct {
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
Checksum []byte `json:"checksum"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
type uploadedMember struct {
|
||||
Firstname string
|
||||
Lastname string
|
||||
Nickname string
|
||||
Company string
|
||||
type myTeamExercice struct {
|
||||
Statement string `json:"statement"`
|
||||
Hint string `json:"hint"`
|
||||
Gain int64 `json:"gain"`
|
||||
Files []myTeamFile `json:"files"`
|
||||
Solved bool `json:"solved"`
|
||||
SolvedTime time.Time `json:"solved_time"`
|
||||
SolvedNumber int64 `json:"solved_number"`
|
||||
}
|
||||
type myTeam struct {
|
||||
Id int64 `json:"team_id"`
|
||||
Exercices map[string]myTeamExercice `json:"exercices"`
|
||||
}
|
||||
|
||||
func myJSONTeam(t fic.Team) (interface{}, error) {
|
||||
ret := map[string]interface{}{}
|
||||
|
||||
ret["team_id"] = t.Id
|
||||
|
||||
exercices := map[string]interface{}{}
|
||||
ret := myTeam{}
|
||||
ret.Id = t.Id
|
||||
ret.Exercices = map[string]myTeamExercice{}
|
||||
|
||||
if exos, err := fic.GetExercices(); err != nil {
|
||||
return ret, err
|
||||
} else {
|
||||
for _, e := range exos {
|
||||
if t.HasAccess(e) {
|
||||
exercice := map[string]interface{}{}
|
||||
exercice["statement"] = e.Statement
|
||||
exercice["hint"] = e.Hint
|
||||
exercice["solved"] = t.HasSolved(e)
|
||||
exercice := myTeamExercice{}
|
||||
exercice.Statement = e.Statement
|
||||
exercice.Hint = e.Hint
|
||||
exercice.Solved, exercice.SolvedTime, exercice.SolvedNumber = t.HasSolved(e)
|
||||
exercice.Files = []myTeamFile{}
|
||||
|
||||
exercices[fmt.Sprintf("%d", e.Id)] = exercice
|
||||
if files, err := e.GetFiles(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
for _, f := range files {
|
||||
exercice.Files = append(exercice.Files, myTeamFile{f.Path, f.Name, f.Checksum, f.Size})
|
||||
}
|
||||
}
|
||||
|
||||
ret.Exercices[fmt.Sprintf("%d", e.Id)] = exercice
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret["exercices"] = exercices
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
|
@ -66,6 +81,17 @@ func nginxGenTeam() (string, error) {
|
|||
}
|
||||
}
|
||||
|
||||
type uploadedTeam struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type uploadedMember struct {
|
||||
Firstname string
|
||||
Lastname string
|
||||
Nickname string
|
||||
Company string
|
||||
}
|
||||
|
||||
func listTeam(args []string, body []byte) (interface{}, error) {
|
||||
if len(args) == 2 {
|
||||
if tid, err := strconv.Atoi(string(args[0])); err != nil {
|
||||
|
|
|
@ -18,15 +18,6 @@ type uploadedTheme struct {
|
|||
Name string
|
||||
}
|
||||
|
||||
type uploadedExercice struct {
|
||||
Title string
|
||||
Statement string
|
||||
Hint string
|
||||
Depend *int
|
||||
Gain int
|
||||
VideoURI string
|
||||
}
|
||||
|
||||
func getTheme(args []string) (fic.Theme, error) {
|
||||
if tid, err := strconv.Atoi(string(args[0])); err != nil {
|
||||
return fic.Theme{}, err
|
||||
|
@ -46,43 +37,33 @@ func getExercice(args []string) (fic.Exercice, error) {
|
|||
}
|
||||
|
||||
func listTheme(args []string, body []byte) (interface{}, error) {
|
||||
if len(args) == 2 {
|
||||
if len(args) == 3 {
|
||||
if e, err := getExercice(args); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if args[2] == "files" {
|
||||
return e.GetFiles()
|
||||
} else if args[2] == "keys" {
|
||||
return e.GetKeys()
|
||||
}
|
||||
}
|
||||
} else if len(args) == 2 {
|
||||
return getExercice(args)
|
||||
} else if len(args) == 1 {
|
||||
return getTheme(args)
|
||||
} else if len(args) == 0 {
|
||||
// List all themes
|
||||
return fic.GetThemes()
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func creationTheme(args []string, body []byte) (interface{}, error) {
|
||||
if len(args) == 1 {
|
||||
if len(args) >= 1 {
|
||||
if theme, err := getTheme(args); err != nil {
|
||||
return nil, err
|
||||
} 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)
|
||||
return createExercice(theme, args[1:], body)
|
||||
}
|
||||
} else if len(args) == 0 {
|
||||
// Create a new theme
|
||||
|
|
Loading…
Reference in New Issue