libfic: explode theme file

This commit is contained in:
nemunaire 2018-01-09 15:16:16 +01:00 committed by nemunaire
parent db4cc9ce85
commit e7d3e34c0b
2 changed files with 53 additions and 50 deletions

View file

@ -1,8 +1,6 @@
package fic
import (
"fmt"
)
import ()
type Theme struct {
Id int64 `json:"id"`
@ -80,50 +78,3 @@ func (t Theme) Delete() (int64, error) {
return nb, err
}
}
type ExportedExercice struct {
Title string `json:"title"`
Gain int64 `json:"gain"`
Coeff float64 `json:"curcoeff"`
Solved int64 `json:"solved"`
Tried int64 `json:"tried"`
}
type exportedTheme struct {
Name string `json:"name"`
Authors string `json:"authors"`
Intro string `json:"intro"`
Exercices map[string]ExportedExercice `json:"exercices"`
}
func ExportThemes() (interface{}, error) {
if themes, err := 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.Coefficient,
exercice.SolvedCount(),
exercice.TriedTeamCount(),
}
}
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
theme.Name,
theme.Authors,
theme.Intro,
exos,
}
}
}
return ret, nil
}
}

52
libfic/theme_export.go Normal file
View file

@ -0,0 +1,52 @@
package fic
import (
"fmt"
)
type ExportedExercice struct {
Title string `json:"title"`
Gain int64 `json:"gain"`
Coeff float64 `json:"curcoeff"`
Solved int64 `json:"solved"`
Tried int64 `json:"tried"`
}
type exportedTheme struct {
Name string `json:"name"`
Authors string `json:"authors"`
Intro string `json:"intro"`
Exercices map[string]ExportedExercice `json:"exercices"`
}
func ExportThemes() (interface{}, error) {
if themes, err := 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.Coefficient,
exercice.SolvedCount(),
exercice.TriedTeamCount(),
}
}
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
theme.Name,
theme.Authors,
theme.Intro,
exos,
}
}
}
return ret, nil
}
}