Export themes.json

This commit is contained in:
nemunaire 2016-01-21 01:37:33 +01:00
parent 47610f0e2a
commit 5a8d2c36b7
2 changed files with 56 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"srs.epita.fr/fic-server/libfic"
@ -14,8 +15,43 @@ var ApiThemesRouting = map[string]DispatchFunction{
"DELETE": deletionTheme,
}
type uploadedTheme struct {
Name string
type exportedExercice struct {
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) {
@ -50,7 +86,11 @@ func listTheme(args []string, body []byte) (interface{}, error) {
} else if len(args) == 2 {
return getExercice(args)
} else if len(args) == 1 {
return getTheme(args)
if args[0] == "themes.json" {
return exportThemes()
} else {
return getTheme(args)
}
} else if len(args) == 0 {
// List all themes
return fic.GetThemes()
@ -58,6 +98,10 @@ func listTheme(args []string, body []byte) (interface{}, error) {
return nil, nil
}
type uploadedTheme struct {
Name string
}
func creationTheme(args []string, body []byte) (interface{}, error) {
if len(args) >= 1 {
if theme, err := getTheme(args); err != nil {

View File

@ -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) {
s, _, _ := t.HasSolved(e)
if s {