server/admin/api_theme.go

208 lines
4.4 KiB
Go
Raw Normal View History

2016-01-07 19:47:07 +00:00
package main
import (
2016-01-13 12:07:45 +00:00
"encoding/json"
"errors"
2016-01-21 00:37:33 +00:00
"fmt"
2016-01-07 19:47:07 +00:00
"strconv"
2016-01-13 19:25:25 +00:00
"srs.epita.fr/fic-server/libfic"
2016-01-07 19:47:07 +00:00
)
var ApiThemesRouting = map[string]DispatchFunction{
2016-01-13 12:07:45 +00:00
"GET": listTheme,
"PATCH": updateTheme,
2016-01-13 12:07:45 +00:00
"POST": creationTheme,
"DELETE": deletionTheme,
}
2016-01-21 00:37:33 +00:00
type exportedExercice struct {
2016-01-23 12:19:28 +00:00
Title string `json:"title"`
Gain int64 `json:"gain"`
Solved int64 `json:"solved"`
2016-01-25 02:05:32 +00:00
Tried int64 `json:"tried"`
2016-01-21 00:37:33 +00:00
}
type exportedTheme struct {
2016-01-23 12:19:28 +00:00
Name string `json:"name"`
Authors string `json:"authors"`
2016-01-21 00:37:33 +00:00
Exercices map[string]exportedExercice `json:"exercices"`
}
func exportThemes() (interface{}, error) {
if themes, err := fic.GetThemes(); err != nil {
return nil, err
} else {
ret := map[string]exportedTheme{}
for _, theme := range themes {
if exercices, err := theme.GetExercices(); err != nil {
return nil, err
} else {
exos := map[string]exportedExercice{}
for _, exercice := range exercices {
exos[fmt.Sprintf("%d", exercice.Id)] = exportedExercice{
exercice.Title,
exercice.Gain,
exercice.SolvedCount(),
2016-01-25 02:05:32 +00:00
exercice.TriedTeamCount(),
2016-01-21 00:37:33 +00:00
}
}
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
theme.Name,
2016-01-24 13:32:46 +00:00
theme.Authors[:len(theme.Authors)-1],
2016-01-21 00:37:33 +00:00
exos,
}
}
}
return ret, nil
}
2016-01-13 12:07:45 +00:00
}
2016-02-01 17:57:47 +00:00
func bindingFiles() (string, error) {
if files, err := fic.GetFiles(); err != nil {
return "", err
} else {
ret := ""
for _, file := range files {
ret += fmt.Sprintf("%s;%s\n", file.GetOrigin(), file.Path)
}
return ret, nil
}
}
2016-01-13 19:25:25 +00:00
func getTheme(args []string) (fic.Theme, error) {
2016-01-13 12:07:45 +00:00
if tid, err := strconv.Atoi(string(args[0])); err != nil {
2016-01-13 19:25:25 +00:00
return fic.Theme{}, err
2016-01-13 12:07:45 +00:00
} else {
2016-01-13 19:25:25 +00:00
return fic.GetTheme(tid)
2016-01-13 12:07:45 +00:00
}
}
2016-01-13 19:25:25 +00:00
func getExercice(args []string) (fic.Exercice, error) {
2016-01-13 12:07:45 +00:00
if theme, err := getTheme(args); err != nil {
2016-01-13 19:25:25 +00:00
return fic.Exercice{}, err
2016-01-13 12:07:45 +00:00
} else if eid, err := strconv.Atoi(string(args[1])); err != nil {
2016-01-13 19:25:25 +00:00
return fic.Exercice{}, err
2016-01-13 12:07:45 +00:00
} else {
return theme.GetExercice(eid)
}
2016-01-07 19:47:07 +00:00
}
func listTheme(args []string, body []byte) (interface{}, error) {
2016-01-18 18:41:04 +00:00
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 {
if args[1] == "exercices" {
if theme, err := getTheme(args); err != nil {
return nil, err
} else {
return theme.GetExercices()
}
} else {
return getExercice(args)
}
2016-01-13 12:07:45 +00:00
} else if len(args) == 1 {
2016-02-01 17:57:47 +00:00
if args[0] == "files-bindings" {
return bindingFiles()
} else if args[0] == "themes.json" {
2016-01-21 00:37:33 +00:00
return exportThemes()
} else {
return getTheme(args)
}
2016-01-13 12:07:45 +00:00
} else if len(args) == 0 {
// List all themes
2016-01-13 19:25:25 +00:00
return fic.GetThemes()
2016-01-13 12:07:45 +00:00
}
2016-01-18 18:41:04 +00:00
return nil, nil
2016-01-13 12:07:45 +00:00
}
2016-01-21 00:37:33 +00:00
type uploadedTheme struct {
2016-01-23 11:21:02 +00:00
Name string
Authors string
2016-01-21 00:37:33 +00:00
}
2016-01-13 12:07:45 +00:00
func creationTheme(args []string, body []byte) (interface{}, error) {
2016-01-18 18:41:04 +00:00
if len(args) >= 1 {
2016-01-13 12:07:45 +00:00
if theme, err := getTheme(args); err != nil {
2016-01-07 19:47:07 +00:00
return nil, err
} else {
2016-01-18 18:41:04 +00:00
return createExercice(theme, args[1:], body)
2016-01-07 19:47:07 +00:00
}
} else if len(args) == 0 {
2016-01-13 12:07:45 +00:00
// Create a new theme
var ut uploadedTheme
if err := json.Unmarshal(body, &ut); err != nil {
return nil, err
}
if len(ut.Name) == 0 {
return nil, errors.New("Theme's name not filled")
}
2016-01-23 11:21:02 +00:00
return fic.CreateTheme(ut.Name, ut.Authors)
2016-01-13 12:07:45 +00:00
} else {
return nil, nil
}
}
func updateTheme(args []string, body []byte) (interface{}, error) {
if len(args) == 2 {
// Update an exercice
var ue fic.Exercice
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 _, err := ue.Update(); err != nil {
return nil, err
}
return ue, nil
} else if len(args) == 1 {
// Update a theme
var ut fic.Theme
if err := json.Unmarshal(body, &ut); err != nil {
return nil, err
}
if len(ut.Name) == 0 {
return nil, errors.New("Theme's name not filled")
}
return ut.Update()
} else {
return nil, nil
}
}
2016-01-13 12:07:45 +00:00
func deletionTheme(args []string, body []byte) (interface{}, error) {
if len(args) == 2 {
if exercice, err := getExercice(args); err != nil {
return nil, err
} else {
return exercice.Delete()
}
} else if len(args) == 1 {
if theme, err := getTheme(args); err != nil {
return nil, err
} else {
return theme.Delete()
}
2016-01-07 19:47:07 +00:00
} else {
return nil, nil
}
}