Export themes.json
This commit is contained in:
parent
47610f0e2a
commit
5a8d2c36b7
2 changed files with 56 additions and 3 deletions
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
|
@ -14,8 +15,43 @@ var ApiThemesRouting = map[string]DispatchFunction{
|
||||||
"DELETE": deletionTheme,
|
"DELETE": deletionTheme,
|
||||||
}
|
}
|
||||||
|
|
||||||
type uploadedTheme struct {
|
type exportedExercice struct {
|
||||||
Name string
|
Title string `json:"title"`
|
||||||
|
Gain int64 `json:"gain"`
|
||||||
|
Solved int64 `json:"solved"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type exportedTheme struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
|
||||||
|
theme.Name,
|
||||||
|
exos,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTheme(args []string) (fic.Theme, error) {
|
func getTheme(args []string) (fic.Theme, error) {
|
||||||
|
@ -50,7 +86,11 @@ func listTheme(args []string, body []byte) (interface{}, error) {
|
||||||
} else if len(args) == 2 {
|
} else if len(args) == 2 {
|
||||||
return getExercice(args)
|
return getExercice(args)
|
||||||
} else if len(args) == 1 {
|
} else if len(args) == 1 {
|
||||||
return getTheme(args)
|
if args[0] == "themes.json" {
|
||||||
|
return exportThemes()
|
||||||
|
} else {
|
||||||
|
return getTheme(args)
|
||||||
|
}
|
||||||
} else if len(args) == 0 {
|
} else if len(args) == 0 {
|
||||||
// List all themes
|
// List all themes
|
||||||
return fic.GetThemes()
|
return fic.GetThemes()
|
||||||
|
@ -58,6 +98,10 @@ func listTheme(args []string, body []byte) (interface{}, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type uploadedTheme struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
func creationTheme(args []string, body []byte) (interface{}, error) {
|
func creationTheme(args []string, body []byte) (interface{}, error) {
|
||||||
if len(args) >= 1 {
|
if len(args) >= 1 {
|
||||||
if theme, err := getTheme(args); err != nil {
|
if theme, err := getTheme(args); err != nil {
|
||||||
|
|
|
@ -133,6 +133,15 @@ func (e Exercice) Solved(t Team) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e Exercice) SolvedCount() int64 {
|
||||||
|
var nb int64
|
||||||
|
if err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_exercice = ?", e.Id).Scan(&nb); err != nil {
|
||||||
|
return 0
|
||||||
|
} else {
|
||||||
|
return nb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (e Exercice) CheckResponse(response string, t Team) (bool, error) {
|
func (e Exercice) CheckResponse(response string, t Team) (bool, error) {
|
||||||
s, _, _ := t.HasSolved(e)
|
s, _, _ := t.HasSolved(e)
|
||||||
if s {
|
if s {
|
||||||
|
|
Reference in a new issue