diff --git a/frontend/static/views/home.html b/frontend/static/views/home.html index 2620ae1e..e31aa2a2 100644 --- a/frontend/static/views/home.html +++ b/frontend/static/views/home.html @@ -10,7 +10,7 @@
- +
{{ theme.name }} diff --git a/libfic/db.go b/libfic/db.go index 1180b0fe..41e991b6 100644 --- a/libfic/db.go +++ b/libfic/db.go @@ -73,6 +73,7 @@ CREATE TABLE IF NOT EXISTS themes( url_id VARCHAR(191) NOT NULL UNIQUE, path VARCHAR(191) NOT NULL UNIQUE, intro TEXT NOT NULL, + image VARCHAR(255) NOT NULL, authors TEXT NOT NULL ) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; `); err != nil { diff --git a/libfic/theme.go b/libfic/theme.go index 3f6d20bf..97a7de80 100644 --- a/libfic/theme.go +++ b/libfic/theme.go @@ -10,11 +10,12 @@ type Theme struct { Path string `json:"path"` Authors string `json:"authors,omitempty"` Intro string `json:"intro,omitempty"` + Image string `json:"image,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, path, authors, intro FROM themes"); err != nil { + if rows, err := DBQuery("SELECT id_theme, name, url_id, path, authors, intro, image FROM themes"); err != nil { return nil, err } else { defer rows.Close() @@ -22,7 +23,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.Path, &t.Authors, &t.Intro); err != nil { + if err := rows.Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Image); err != nil { return nil, err } themes = append(themes, t) @@ -38,7 +39,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, path, authors, intro FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro); err != nil { + if err := DBQueryRow("SELECT id_theme, name, url_id, path, authors, intro, image FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Image); err != nil { return t, err } @@ -48,7 +49,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, path, authors, intro FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro); err != nil { + if err := DBQueryRow("SELECT id_theme, name, url_id, path, authors, intro, image FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Image); err != nil { return t, err } @@ -56,13 +57,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, 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 { +func CreateTheme(name string, url_id string, path string, authors string, intro string, image string) (Theme, error) { + if res, err := DBExec("INSERT INTO themes (name, url_id, authors, path, intro, image) VALUES (?, ?, ?, ?, ?, ?)", name, url_id, authors, path, intro, image); err != nil { return Theme{}, err } else if tid, err := res.LastInsertId(); err != nil { return Theme{}, err } else { - return Theme{tid, name, url_id, path, authors, intro}, nil + return Theme{tid, name, url_id, path, authors, intro, image}, nil } } @@ -78,7 +79,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 = ?, path = ?, intro = ? WHERE id_theme = ?", t.Name, t.URLId, t.Authors, t.Path, t.Intro, t.Id); err != nil { + if res, err := DBExec("UPDATE themes SET name = ?, url_id = ?, authors = ?, path = ?, intro = ?, image = ? WHERE id_theme = ?", t.Name, t.URLId, t.Authors, t.Path, t.Intro, t.Image, t.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err diff --git a/libfic/theme_export.go b/libfic/theme_export.go index c691e883..51f61ac3 100644 --- a/libfic/theme_export.go +++ b/libfic/theme_export.go @@ -2,6 +2,7 @@ package fic import ( "fmt" + "path" ) // exportedExercice is a structure representing a challenge, as exposed to players. @@ -21,6 +22,7 @@ type exportedTheme struct { URLId string `json:"urlid"` Authors string `json:"authors"` Intro string `json:"intro"` + Image string `json:"image,omitempty"` Exercices map[string]exportedExercice `json:"exercices"` } @@ -47,11 +49,18 @@ func ExportThemes() (interface{}, error) { exercice.TriedTeamCount(), } } + + imgpath := "" + if len(theme.Image) > 0 { + imgpath = path.Join(FilesDir, theme.Image) + } + ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{ theme.Name, theme.URLId, theme.Authors, theme.Intro, + imgpath, exos, } }