server/libfic/theme.go

130 lines
3.2 KiB
Go
Raw Normal View History

2016-01-13 19:25:25 +00:00
package fic
2016-01-07 17:43:02 +00:00
import (
"fmt"
)
2016-01-07 17:43:02 +00:00
type Theme struct {
2016-01-23 11:21:02 +00:00
Id int64 `json:"id"`
Name string `json:"name"`
2017-12-21 21:17:41 +00:00
Authors string `json:"authors,omitempty"`
Intro string `json:"intro,omitempty"`
2016-01-07 17:43:02 +00:00
}
func GetThemes() ([]Theme, error) {
2017-12-21 21:17:41 +00:00
if rows, err := DBQuery("SELECT id_theme, name, authors, intro FROM themes"); err != nil {
2016-01-07 17:43:02 +00:00
return nil, err
} else {
defer rows.Close()
var themes = make([]Theme, 0)
for rows.Next() {
var t Theme
2017-12-21 21:17:41 +00:00
if err := rows.Scan(&t.Id, &t.Name, &t.Authors, &t.Intro); err != nil {
2016-01-07 17:43:02 +00:00
return nil, err
}
themes = append(themes, t)
}
if err := rows.Err(); err != nil {
return nil, err
}
return themes, nil
}
}
func GetTheme(id int) (Theme, error) {
var t Theme
2017-12-21 21:17:41 +00:00
if err := DBQueryRow("SELECT id_theme, name, authors, intro FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.Authors, &t.Intro); err != nil {
2016-01-07 17:43:02 +00:00
return t, err
}
return t, nil
}
func GetThemeByName(name string) (Theme, error) {
var t Theme
2017-12-21 21:17:41 +00:00
if err := DBQueryRow("SELECT id_theme, name, authors, intro FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.Authors, &t.Intro); err != nil {
return t, err
}
return t, nil
}
2017-12-21 21:17:41 +00:00
func CreateTheme(name string, authors string, intro string) (Theme, error) {
if res, err := DBExec("INSERT INTO themes (name, authors, intro) VALUES (?, ?, ?)", name, authors, intro); err != nil {
2016-01-07 17:43:02 +00:00
return Theme{}, err
} else if tid, err := res.LastInsertId(); err != nil {
return Theme{}, err
} else {
2017-12-21 21:17:41 +00:00
return Theme{tid, name, authors, intro}, nil
2016-01-07 17:43:02 +00:00
}
}
func (t Theme) Update() (int64, error) {
2017-12-21 21:17:41 +00:00
if res, err := DBExec("UPDATE themes SET name = ?, authors = ?, intro = ? WHERE id_theme = ?", t.Name, t.Authors, t.Intro, t.Id); err != nil {
2016-01-07 17:43:02 +00:00
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
func (t Theme) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM themes WHERE id_theme = ?", t.Id); err != nil {
2016-01-07 17:43:02 +00:00
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
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"`
2017-12-21 21:17:41 +00:00
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,
2017-12-21 21:17:41 +00:00
theme.Intro,
exos,
}
}
}
return ret, nil
}
}