Themes can have header image

This commit is contained in:
nemunaire 2018-11-25 03:20:03 +01:00
parent 26295dd978
commit 0effdbcf5e
4 changed files with 20 additions and 9 deletions

View File

@ -10,7 +10,7 @@
<div class="card-columns"> <div class="card-columns">
<div class="card" ng-repeat="(k,theme) in themes"> <div class="card" ng-repeat="(k,theme) in themes">
<img class="card-img-top" src=".../100px180/" alt=""> <img class="card-img-top" src="{{ theme.image }}" alt="">
<div class="card-body"> <div class="card-body">
<h5 class="card-title"> <h5 class="card-title">
<a ng-href="/{{ theme.urlid }}">{{ theme.name }}</a> <a ng-href="/{{ theme.urlid }}">{{ theme.name }}</a>

View File

@ -73,6 +73,7 @@ CREATE TABLE IF NOT EXISTS themes(
url_id VARCHAR(191) NOT NULL UNIQUE, url_id VARCHAR(191) NOT NULL UNIQUE,
path VARCHAR(191) NOT NULL UNIQUE, path VARCHAR(191) NOT NULL UNIQUE,
intro TEXT NOT NULL, intro TEXT NOT NULL,
image VARCHAR(255) NOT NULL,
authors TEXT NOT NULL authors TEXT NOT NULL
) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; ) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
`); err != nil { `); err != nil {

View File

@ -10,11 +10,12 @@ type Theme struct {
Path string `json:"path"` Path string `json:"path"`
Authors string `json:"authors,omitempty"` Authors string `json:"authors,omitempty"`
Intro string `json:"intro,omitempty"` Intro string `json:"intro,omitempty"`
Image string `json:"image,omitempty"`
} }
// GetThemes returns a list of registered Themes from the database. // GetThemes returns a list of registered Themes from the database.
func GetThemes() ([]Theme, error) { 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 return nil, err
} else { } else {
defer rows.Close() defer rows.Close()
@ -22,7 +23,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.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 return nil, err
} }
themes = append(themes, t) themes = append(themes, t)
@ -38,7 +39,7 @@ func GetThemes() ([]Theme, error) {
// GetTheme retrieves a Theme from its identifier. // GetTheme retrieves a Theme from its identifier.
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, 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 return t, err
} }
@ -48,7 +49,7 @@ func GetTheme(id int) (Theme, error) {
// GetThemeByName retrieves a Theme from its title // GetThemeByName retrieves a Theme from its title
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, 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 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. // 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) { 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) VALUES (?, ?, ?, ?, ?)", name, url_id, authors, path, intro); err != nil { 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 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, 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. // Update applies modifications back to the database.
func (t Theme) Update() (int64, error) { 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 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

View File

@ -2,6 +2,7 @@ package fic
import ( import (
"fmt" "fmt"
"path"
) )
// exportedExercice is a structure representing a challenge, as exposed to players. // exportedExercice is a structure representing a challenge, as exposed to players.
@ -21,6 +22,7 @@ type exportedTheme struct {
URLId string `json:"urlid"` URLId string `json:"urlid"`
Authors string `json:"authors"` Authors string `json:"authors"`
Intro string `json:"intro"` Intro string `json:"intro"`
Image string `json:"image,omitempty"`
Exercices map[string]exportedExercice `json:"exercices"` Exercices map[string]exportedExercice `json:"exercices"`
} }
@ -47,11 +49,18 @@ func ExportThemes() (interface{}, error) {
exercice.TriedTeamCount(), exercice.TriedTeamCount(),
} }
} }
imgpath := ""
if len(theme.Image) > 0 {
imgpath = path.Join(FilesDir, theme.Image)
}
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{ ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
theme.Name, theme.Name,
theme.URLId, theme.URLId,
theme.Authors, theme.Authors,
theme.Intro, theme.Intro,
imgpath,
exos, exos,
} }
} }