Write docs!
This commit is contained in:
parent
c460bb7bf5
commit
bcc598ebd5
37 changed files with 478 additions and 188 deletions
|
|
@ -2,6 +2,7 @@ package fic
|
|||
|
||||
import ()
|
||||
|
||||
// Theme represents a group of challenges, to display to players
|
||||
type Theme struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
|
@ -10,6 +11,7 @@ type Theme struct {
|
|||
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 {
|
||||
return nil, err
|
||||
|
|
@ -32,6 +34,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 {
|
||||
|
|
@ -41,6 +44,7 @@ func GetTheme(id int) (Theme, error) {
|
|||
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, authors, intro FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.URLId, &t.Authors, &t.Intro); err != nil {
|
||||
|
|
@ -50,6 +54,7 @@ func GetThemeByName(name string) (Theme, error) {
|
|||
return t, nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return Theme{}, err
|
||||
|
|
@ -60,6 +65,8 @@ func CreateTheme(name string, url_id string, authors string, intro string) (Them
|
|||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
|
@ -68,6 +75,7 @@ func (t *Theme) FixURLId() bool {
|
|||
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 = ?, intro = ? WHERE id_theme = ?", t.Name, t.URLId, t.Authors, t.Intro, t.Id); err != nil {
|
||||
return 0, err
|
||||
|
|
@ -78,6 +86,7 @@ func (t Theme) Update() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
|
|||
Reference in a new issue