admin/sync: import theme introductions
This commit is contained in:
parent
39b57119fe
commit
ea3f3b709d
@ -109,6 +109,7 @@ func showThemedExercice(theme fic.Theme, exercice fic.Exercice, body []byte) (in
|
|||||||
type uploadedTheme struct {
|
type uploadedTheme struct {
|
||||||
Name string
|
Name string
|
||||||
Authors string
|
Authors string
|
||||||
|
Intro string
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTheme(_ httprouter.Params, body []byte) (interface{}, error) {
|
func createTheme(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||||
@ -121,7 +122,7 @@ func createTheme(_ httprouter.Params, body []byte) (interface{}, error) {
|
|||||||
return nil, errors.New("Theme's name not filled")
|
return nil, errors.New("Theme's name not filled")
|
||||||
}
|
}
|
||||||
|
|
||||||
return fic.CreateTheme(ut.Name, ut.Authors)
|
return fic.CreateTheme(ut.Name, ut.Authors, ut.Intro)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateTheme(theme fic.Theme, body []byte) (interface{}, error) {
|
func updateTheme(theme fic.Theme, body []byte) (interface{}, error) {
|
||||||
|
@ -40,24 +40,31 @@ func SyncThemes(i Importer) []string {
|
|||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
} else {
|
} else {
|
||||||
for _, tname := range themes {
|
for _, tname := range themes {
|
||||||
if authors, err := getAuthors(i, tname); err != nil {
|
var authors []string
|
||||||
|
var intro string
|
||||||
|
var theme fic.Theme
|
||||||
|
|
||||||
|
if authors, err = getAuthors(i, tname); err != nil {
|
||||||
errs = append(errs, fmt.Sprintf("%q: unable to get AUTHORS: %s", tname, err))
|
errs = append(errs, fmt.Sprintf("%q: unable to get AUTHORS: %s", tname, err))
|
||||||
continue
|
continue
|
||||||
} else if theme, err := fic.GetThemeByName(tname); err != nil {
|
} else if intro, err = getFileContent(i, path.Join(tname, "introduction.txt")); err != nil {
|
||||||
if _, err := fic.CreateTheme(tname, strings.Join(authors, ", ")); err != nil {
|
errs = append(errs, fmt.Sprintf("%q: unable to get introduction: %s", tname, err))
|
||||||
|
} else if theme, err = fic.GetThemeByName(tname); err != nil {
|
||||||
|
if _, err := fic.CreateTheme(tname, strings.Join(authors, ", "), intro); err != nil {
|
||||||
errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tname, err))
|
errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tname, err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
authors_str := strings.Join(authors, ", ")
|
|
||||||
|
|
||||||
if theme.Name != tname || theme.Authors != authors_str {
|
authors_str := strings.Join(authors, ", ")
|
||||||
theme.Name = tname
|
|
||||||
theme.Authors = authors_str
|
if theme.Name != tname || theme.Authors != authors_str || theme.Intro != intro {
|
||||||
if _, err := theme.Update(); err != nil {
|
theme.Name = tname
|
||||||
errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tname, err))
|
theme.Authors = authors_str
|
||||||
continue
|
theme.Intro = intro
|
||||||
}
|
if _, err := theme.Update(); err != nil {
|
||||||
|
errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tname, err))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,7 @@ CREATE TABLE IF NOT EXISTS events(
|
|||||||
CREATE TABLE IF NOT EXISTS themes(
|
CREATE TABLE IF NOT EXISTS themes(
|
||||||
id_theme INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
id_theme INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||||
name VARCHAR(255) NOT NULL UNIQUE,
|
name VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
intro TEXT NOT NULL,
|
||||||
authors TEXT NOT NULL
|
authors TEXT NOT NULL
|
||||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||||
`); err != nil {
|
`); err != nil {
|
||||||
|
@ -7,11 +7,12 @@ import (
|
|||||||
type Theme struct {
|
type Theme struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Authors string `json:"authors"`
|
Authors string `json:"authors,omitempty"`
|
||||||
|
Intro string `json:"intro,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetThemes() ([]Theme, error) {
|
func GetThemes() ([]Theme, error) {
|
||||||
if rows, err := DBQuery("SELECT id_theme, name, authors FROM themes"); err != nil {
|
if rows, err := DBQuery("SELECT id_theme, name, authors, intro FROM themes"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
@ -19,7 +20,7 @@ func GetThemes() ([]Theme, error) {
|
|||||||
var themes = make([]Theme, 0)
|
var themes = make([]Theme, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var t Theme
|
var t Theme
|
||||||
if err := rows.Scan(&t.Id, &t.Name, &t.Authors); err != nil {
|
if err := rows.Scan(&t.Id, &t.Name, &t.Authors, &t.Intro); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
themes = append(themes, t)
|
themes = append(themes, t)
|
||||||
@ -34,7 +35,7 @@ func GetThemes() ([]Theme, error) {
|
|||||||
|
|
||||||
func GetTheme(id int) (Theme, error) {
|
func GetTheme(id int) (Theme, error) {
|
||||||
var t Theme
|
var t Theme
|
||||||
if err := DBQueryRow("SELECT id_theme, name, authors FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.Authors); err != nil {
|
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 {
|
||||||
return t, err
|
return t, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,25 +44,25 @@ func GetTheme(id int) (Theme, error) {
|
|||||||
|
|
||||||
func GetThemeByName(name string) (Theme, error) {
|
func GetThemeByName(name string) (Theme, error) {
|
||||||
var t Theme
|
var t Theme
|
||||||
if err := DBQueryRow("SELECT id_theme, name, authors FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.Authors); err != nil {
|
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, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTheme(name string, authors string) (Theme, error) {
|
func CreateTheme(name string, authors string, intro string) (Theme, error) {
|
||||||
if res, err := DBExec("INSERT INTO themes (name, authors) VALUES (?, ?)", name, authors); err != nil {
|
if res, err := DBExec("INSERT INTO themes (name, authors, intro) VALUES (?, ?, ?)", name, authors, intro); err != nil {
|
||||||
return Theme{}, err
|
return Theme{}, err
|
||||||
} else if tid, err := res.LastInsertId(); err != nil {
|
} else if tid, err := res.LastInsertId(); err != nil {
|
||||||
return Theme{}, err
|
return Theme{}, err
|
||||||
} else {
|
} else {
|
||||||
return Theme{tid, name, authors}, nil
|
return Theme{tid, name, authors, intro}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Theme) Update() (int64, error) {
|
func (t Theme) Update() (int64, error) {
|
||||||
if res, err := DBExec("UPDATE themes SET name = ?, authors = ? WHERE id_theme = ?", t.Name, t.Authors, t.Id); err != nil {
|
if res, err := DBExec("UPDATE themes SET name = ?, authors = ?, intro = ? WHERE id_theme = ?", t.Name, t.Authors, t.Intro, t.Id); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
} else if nb, err := res.RowsAffected(); err != nil {
|
} else if nb, err := res.RowsAffected(); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -91,6 +92,7 @@ type ExportedExercice struct {
|
|||||||
type exportedTheme struct {
|
type exportedTheme struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Authors string `json:"authors"`
|
Authors string `json:"authors"`
|
||||||
|
Intro string `json:"intro"`
|
||||||
Exercices map[string]ExportedExercice `json:"exercices"`
|
Exercices map[string]ExportedExercice `json:"exercices"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,6 +118,7 @@ func ExportThemes() (interface{}, error) {
|
|||||||
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
|
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
|
||||||
theme.Name,
|
theme.Name,
|
||||||
theme.Authors,
|
theme.Authors,
|
||||||
|
theme.Intro,
|
||||||
exos,
|
exos,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user