82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package fic
|
|
|
|
import ()
|
|
|
|
type Theme struct {
|
|
Id int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
URLId string `json:"urlid"`
|
|
Authors string `json:"authors,omitempty"`
|
|
Intro string `json:"intro,omitempty"`
|
|
}
|
|
|
|
func GetThemes() ([]Theme, error) {
|
|
if rows, err := DBQuery("SELECT id_theme, name, url_id, authors, intro 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.Authors, &t.Intro); err != nil {
|
|
return nil, err
|
|
}
|
|
themes = append(themes, t)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return themes, nil
|
|
}
|
|
}
|
|
|
|
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 {
|
|
return t, err
|
|
}
|
|
|
|
return t, nil
|
|
}
|
|
|
|
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 {
|
|
return t, err
|
|
}
|
|
|
|
return t, nil
|
|
}
|
|
|
|
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
|
|
} else if tid, err := res.LastInsertId(); err != nil {
|
|
return Theme{}, err
|
|
} else {
|
|
return Theme{tid, name, url_id, authors, intro}, nil
|
|
}
|
|
}
|
|
|
|
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
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
return 0, err
|
|
} else {
|
|
return nb, err
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|