2016-01-13 19:25:25 +00:00
|
|
|
package fic
|
2016-01-07 17:43:02 +00:00
|
|
|
|
2018-01-09 14:16:16 +00:00
|
|
|
import ()
|
2016-01-07 17:43:02 +00:00
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// Theme represents a group of challenges, to display to players
|
2016-01-07 17:43:02 +00:00
|
|
|
type Theme struct {
|
2016-01-23 11:21:02 +00:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
2018-01-18 10:07:50 +00:00
|
|
|
URLId string `json:"urlid"`
|
2018-07-13 05:25:21 +00:00
|
|
|
Path string `json:"path"`
|
2017-12-21 21:17:41 +00:00
|
|
|
Authors string `json:"authors,omitempty"`
|
|
|
|
Intro string `json:"intro,omitempty"`
|
2016-01-07 17:43:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// GetThemes returns a list of registered Themes from the database.
|
2016-01-07 17:43:02 +00:00
|
|
|
func GetThemes() ([]Theme, error) {
|
2018-07-13 05:25:21 +00:00
|
|
|
if rows, err := DBQuery("SELECT id_theme, name, url_id, path, authors, intro FROM themes"); err != nil {
|
2016-01-07 17:43:02 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var themes = make([]Theme, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var t Theme
|
2018-07-13 05:25:21 +00:00
|
|
|
if err := rows.Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro); err != nil {
|
2016-01-07 17:43:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
themes = append(themes, t)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return themes, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// GetTheme retrieves a Theme from its identifier.
|
2016-01-07 17:43:02 +00:00
|
|
|
func GetTheme(id int) (Theme, error) {
|
|
|
|
var t Theme
|
2018-07-13 05:25:21 +00:00
|
|
|
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 {
|
2016-01-07 17:43:02 +00:00
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// GetThemeByName retrieves a Theme from its title
|
2017-12-08 20:03:50 +00:00
|
|
|
func GetThemeByName(name string) (Theme, error) {
|
|
|
|
var t Theme
|
2018-07-13 05:25:21 +00:00
|
|
|
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 {
|
2017-12-08 20:03:50 +00:00
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// CreateTheme creates and fills a new struct Theme and registers it into the database.
|
2018-07-13 05:25:21 +00:00
|
|
|
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 {
|
2016-01-07 17:43:02 +00:00
|
|
|
return Theme{}, err
|
|
|
|
} else if tid, err := res.LastInsertId(); err != nil {
|
|
|
|
return Theme{}, err
|
|
|
|
} else {
|
2018-07-13 05:25:21 +00:00
|
|
|
return Theme{tid, name, url_id, path, authors, intro}, nil
|
2016-01-07 17:43:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// FixURLId generates a valid URLid from the Theme Title.
|
|
|
|
// It returns true if something has been generated.
|
2018-01-23 00:00:24 +00:00
|
|
|
func (t *Theme) FixURLId() bool {
|
|
|
|
if t.URLId == "" {
|
|
|
|
t.URLId = ToURLid(t.Name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// Update applies modifications back to the database.
|
2016-01-07 17:43:02 +00:00
|
|
|
func (t Theme) Update() (int64, error) {
|
2018-07-13 05:25:21 +00:00
|
|
|
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 {
|
2016-01-07 17:43:02 +00:00
|
|
|
return 0, err
|
|
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
return nb, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// Delete the theme from the database.
|
2016-01-07 17:43:02 +00:00
|
|
|
func (t Theme) Delete() (int64, error) {
|
2016-01-13 00:20:21 +00:00
|
|
|
if res, err := DBExec("DELETE FROM themes WHERE id_theme = ?", t.Id); err != nil {
|
2016-01-07 17:43:02 +00:00
|
|
|
return 0, err
|
|
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
return nb, err
|
|
|
|
}
|
|
|
|
}
|