From 9ab5738cff43164d360522ac87172b18ba472afd Mon Sep 17 00:00:00 2001 From: nemunaire Date: Fri, 13 Jul 2018 07:25:21 +0200 Subject: [PATCH] admin/sync: theme's name is now part of the theme's dirname --- admin/api/theme.go | 2 +- admin/sync/exercices.go | 16 +++++++++------- admin/sync/themes.go | 21 +++++++++++++++------ libfic/db.go | 5 +++-- libfic/theme.go | 17 +++++++++-------- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/admin/api/theme.go b/admin/api/theme.go index 0e54b766..4c8c6503 100644 --- a/admin/api/theme.go +++ b/admin/api/theme.go @@ -163,7 +163,7 @@ func createTheme(_ httprouter.Params, body []byte) (interface{}, error) { return nil, errors.New("Theme's name not filled") } - return fic.CreateTheme(ut.Name, ut.URLId, ut.Authors, ut.Intro) + return fic.CreateTheme(ut.Name, ut.URLId, "", ut.Authors, ut.Intro) } func updateTheme(theme fic.Theme, body []byte) (interface{}, error) { diff --git a/admin/sync/exercices.go b/admin/sync/exercices.go index aa3625cc..ed8669e3 100644 --- a/admin/sync/exercices.go +++ b/admin/sync/exercices.go @@ -13,11 +13,13 @@ import ( func getExercices(i Importer, theme fic.Theme) ([]string, error) { var exercices []string - if dirs, err := i.listDir(theme.Name); err != nil { + if len(theme.Path) == 0 { + return []string{}, nil + } else if dirs, err := i.listDir(theme.Path); err != nil { return []string{}, err } else { for _, dir := range dirs { - if _, err := i.listDir(path.Join(theme.Name, dir)); err == nil { + if _, err := i.listDir(path.Join(theme.Path, dir)); err == nil { exercices = append(exercices, dir) } } @@ -52,12 +54,12 @@ func SyncExercices(i Importer, theme fic.Theme) []string { emap[ename] = eid // Overview and scenario - overview, err := getFileContent(i, path.Join(theme.Name, edir, "overview.txt")) + overview, err := getFileContent(i, path.Join(theme.Path, edir, "overview.txt")) if err != nil { errs = append(errs, fmt.Sprintf("%q: overview.txt: %s", edir, err)) } - statement, err := getFileContent(i, path.Join(theme.Name, edir, "statement.txt")) + statement, err := getFileContent(i, path.Join(theme.Path, edir, "statement.txt")) if err != nil { errs = append(errs, fmt.Sprintf("%q: statement.txt: %s", edir, err)) continue @@ -65,7 +67,7 @@ func SyncExercices(i Importer, theme fic.Theme) []string { // Handle score gain var gain int64 - if p, err := parseExerciceParams(i, path.Join(theme.Name, edir)); err != nil { + if p, err := parseExerciceParams(i, path.Join(theme.Path, edir)); err != nil { errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", edir, err)) continue } else if p.Gain == 0 { @@ -75,14 +77,14 @@ func SyncExercices(i Importer, theme fic.Theme) []string { } // Handle video - videoURI := path.Join(theme.Name, edir, "resolution.mp4") + videoURI := path.Join(theme.Path, edir, "resolution.mp4") if !i.exists(videoURI) { errs = append(errs, fmt.Sprintf("%q: resolution.mp4: no video file found at %s", edir, videoURI)) videoURI = "" } if e, err := theme.GetExerciceByTitle(ename); err != nil { - if ex, err := theme.AddExercice(ename, fic.ToURLid(ename), path.Join(theme.Name, edir), statement, overview, nil, gain, videoURI); err != nil { + if ex, err := theme.AddExercice(ename, fic.ToURLid(ename), path.Join(theme.Path, edir), statement, overview, nil, gain, videoURI); err != nil { errs = append(errs, fmt.Sprintf("%q: error on exercice add: %s", edir, err)) continue } else { diff --git a/admin/sync/themes.go b/admin/sync/themes.go index 9190a6c7..87a0f932 100644 --- a/admin/sync/themes.go +++ b/admin/sync/themes.go @@ -42,19 +42,27 @@ func SyncThemes(i Importer) []string { if themes, err := getThemes(i); err != nil { errs = append(errs, err.Error()) } else { - for _, tname := range themes { + for _, tdir := range themes { var authors []string var intro string var theme fic.Theme + var tname string - if authors, err = getAuthors(i, tname); err != nil { + // Extract theme's label + if f := strings.Index(tdir, "-"); f >= 0 { + tname = tdir[f+1:] + } else { + tname = tdir + } + + if authors, err = getAuthors(i, tdir); err != nil { errs = append(errs, fmt.Sprintf("%q: unable to get AUTHORS: %s", tname, err)) continue - } else if intro, err = getFileContent(i, path.Join(tname, "overview.txt")); err != nil { + } else if intro, err = getFileContent(i, path.Join(tdir, "overview.txt")); err != nil { errs = append(errs, fmt.Sprintf("%q: unable to get theme's overview: %s", tname, err)) } else if theme, err = fic.GetThemeByName(tname); err != nil { - if _, err := fic.CreateTheme(tname, fic.ToURLid(tname), strings.Join(authors, ", "), intro); err != nil { - errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tname, err)) + if _, err := fic.CreateTheme(tname, fic.ToURLid(tname), tdir, strings.Join(authors, ", "), intro); err != nil { + errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tdir, err)) continue } } @@ -65,8 +73,9 @@ func SyncThemes(i Importer) []string { theme.Name = tname theme.Authors = authors_str theme.Intro = intro + theme.Path = tdir if _, err := theme.Update(); err != nil { - errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tname, err)) + errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tdir, err)) continue } } diff --git a/libfic/db.go b/libfic/db.go index 325c0193..09e9d3c7 100644 --- a/libfic/db.go +++ b/libfic/db.go @@ -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, diff --git a/libfic/theme.go b/libfic/theme.go index d1ffc6b2..3f6d20bf 100644 --- a/libfic/theme.go +++ b/libfic/theme.go @@ -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