admin/sync: theme's name is now part of the theme's dirname

This commit is contained in:
nemunaire 2018-07-13 07:25:21 +02:00 committed by Pierre-Olivier Mercier
commit 9ab5738cff
5 changed files with 37 additions and 24 deletions

View file

@ -69,8 +69,9 @@ CREATE TABLE IF NOT EXISTS events(
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS themes(
id_theme INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
url_id VARCHAR(255) NOT NULL UNIQUE,
path VARCHAR(255) NOT NULL UNIQUE,
intro TEXT NOT NULL,
authors TEXT NOT NULL
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
@ -115,7 +116,7 @@ CREATE TABLE IF NOT EXISTS exercices(
id_theme INTEGER NOT NULL,
title VARCHAR(255) NOT NULL,
url_id VARCHAR(255) NOT NULL,
path VARCHAR(255) NOT NULL,
path VARCHAR(255) NOT NULL UNIQUE,
statement TEXT NOT NULL,
overview TEXT NOT NULL,
depend INTEGER,

View file

@ -7,13 +7,14 @@ type Theme struct {
Id int64 `json:"id"`
Name string `json:"name"`
URLId string `json:"urlid"`
Path string `json:"path"`
Authors string `json:"authors,omitempty"`
Intro string `json:"intro,omitempty"`
}
// GetThemes returns a list of registered Themes from the database.
func GetThemes() ([]Theme, error) {
if rows, err := DBQuery("SELECT id_theme, name, url_id, authors, intro FROM themes"); err != nil {
if rows, err := DBQuery("SELECT id_theme, name, url_id, path, authors, intro FROM themes"); err != nil {
return nil, err
} else {
defer rows.Close()
@ -21,7 +22,7 @@ func GetThemes() ([]Theme, error) {
var themes = make([]Theme, 0)
for rows.Next() {
var t Theme
if err := rows.Scan(&t.Id, &t.Name, &t.URLId, &t.Authors, &t.Intro); err != nil {
if err := rows.Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro); err != nil {
return nil, err
}
themes = append(themes, t)
@ -37,7 +38,7 @@ func GetThemes() ([]Theme, error) {
// GetTheme retrieves a Theme from its identifier.
func GetTheme(id int) (Theme, error) {
var t Theme
if err := DBQueryRow("SELECT id_theme, name, url_id, authors, intro FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.URLId, &t.Authors, &t.Intro); err != nil {
if err := DBQueryRow("SELECT id_theme, name, url_id, path, authors, intro FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro); err != nil {
return t, err
}
@ -47,7 +48,7 @@ func GetTheme(id int) (Theme, error) {
// GetThemeByName retrieves a Theme from its title
func GetThemeByName(name string) (Theme, error) {
var t Theme
if err := DBQueryRow("SELECT id_theme, name, url_id, authors, intro FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.URLId, &t.Authors, &t.Intro); err != nil {
if err := DBQueryRow("SELECT id_theme, name, url_id, path, authors, intro FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro); err != nil {
return t, err
}
@ -55,13 +56,13 @@ func GetThemeByName(name string) (Theme, error) {
}
// CreateTheme creates and fills a new struct Theme and registers it into the database.
func CreateTheme(name string, url_id string, authors string, intro string) (Theme, error) {
if res, err := DBExec("INSERT INTO themes (name, url_id, authors, intro) VALUES (?, ?, ?, ?)", name, url_id, authors, intro); err != nil {
func CreateTheme(name string, url_id string, path string, authors string, intro string) (Theme, error) {
if res, err := DBExec("INSERT INTO themes (name, url_id, authors, path, intro) VALUES (?, ?, ?, ?, ?)", name, url_id, authors, path, intro); err != nil {
return Theme{}, err
} else if tid, err := res.LastInsertId(); err != nil {
return Theme{}, err
} else {
return Theme{tid, name, url_id, authors, intro}, nil
return Theme{tid, name, url_id, path, authors, intro}, nil
}
}
@ -77,7 +78,7 @@ func (t *Theme) FixURLId() bool {
// Update applies modifications back to the database.
func (t Theme) Update() (int64, error) {
if res, err := DBExec("UPDATE themes SET name = ?, url_id = ?, authors = ?, intro = ? WHERE id_theme = ?", t.Name, t.URLId, t.Authors, t.Intro, t.Id); err != nil {
if res, err := DBExec("UPDATE themes SET name = ?, url_id = ?, authors = ?, path = ?, intro = ? WHERE id_theme = ?", t.Name, t.URLId, t.Authors, t.Path, t.Intro, t.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err