107 lines
3.4 KiB
Go
107 lines
3.4 KiB
Go
package fic
|
|
|
|
import ()
|
|
|
|
// Theme represents a group of challenges, to display to players
|
|
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"`
|
|
Headline string `json:"headline,omitempty"`
|
|
Image string `json:"image,omitempty"`
|
|
}
|
|
|
|
// CmpTheme returns true if given Themes are identicals.
|
|
func CmpTheme(t1 Theme, t2 Theme) bool {
|
|
return !(t1.Name != t2.Name || t1.URLId != t2.URLId || t1.Path != t2.Path || t1.Authors != t2.Authors || t1.Intro != t2.Intro || t1.Headline != t2.Headline || t1.Image != t2.Image)
|
|
}
|
|
|
|
// 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, headline, image FROM themes"); err != nil {
|
|
return nil, err
|
|
} else {
|
|
defer rows.Close()
|
|
|
|
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, &t.Headline, &t.Image); err != nil {
|
|
return nil, err
|
|
}
|
|
themes = append(themes, t)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return themes, nil
|
|
}
|
|
}
|
|
|
|
// GetTheme retrieves a Theme from its identifier.
|
|
func GetTheme(id int64) (Theme, error) {
|
|
var t Theme
|
|
if err := DBQueryRow("SELECT id_theme, name, url_id, path, authors, intro, headline, image FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Headline, &t.Image); err != nil {
|
|
return t, err
|
|
}
|
|
|
|
return t, nil
|
|
}
|
|
|
|
// 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, headline, image FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Headline, &t.Image); err != nil {
|
|
return t, err
|
|
}
|
|
|
|
return t, nil
|
|
}
|
|
|
|
// CreateTheme creates and fills a new struct Theme and registers it into the database.
|
|
func CreateTheme(theme Theme) (Theme, error) {
|
|
if res, err := DBExec("INSERT INTO themes (name, url_id, authors, path, intro, headline, image) VALUES (?, ?, ?, ?, ?, ?, ?)", theme.Name, theme.URLId, theme.Authors, theme.Path, theme.Intro, theme.Headline, theme.Image); err != nil {
|
|
return Theme{}, err
|
|
} else if theme.Id, err = res.LastInsertId(); err != nil {
|
|
return Theme{}, err
|
|
} else {
|
|
return theme, nil
|
|
}
|
|
}
|
|
|
|
// FixURLId generates a valid URLid from the Theme Title.
|
|
// It returns true if something has been generated.
|
|
func (t *Theme) FixURLId() bool {
|
|
if t.URLId == "" {
|
|
t.URLId = ToURLid(t.Name)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// 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 = ?, headline = ?, image = ? WHERE id_theme = ?", t.Name, t.URLId, t.Authors, t.Path, t.Intro, t.Headline, t.Image, t.Id); err != nil {
|
|
return 0, err
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
return 0, err
|
|
} else {
|
|
return nb, err
|
|
}
|
|
}
|
|
|
|
// Delete the theme from the database.
|
|
func (t Theme) Delete() (int64, error) {
|
|
if res, err := DBExec("DELETE FROM themes WHERE id_theme = ?", t.Id); err != nil {
|
|
return 0, err
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
return 0, err
|
|
} else {
|
|
return nb, err
|
|
}
|
|
}
|