Use pointer receiver more offen

This commit is contained in:
nemunaire 2021-11-22 15:35:07 +01:00
parent 6999b4e728
commit c7569b5e54
59 changed files with 688 additions and 672 deletions

View file

@ -18,20 +18,20 @@ type Theme struct {
}
// 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 || t1.PartnerImage != t2.PartnerImage || t1.PartnerLink != t2.PartnerLink || t1.PartnerText != t2.PartnerText)
func CmpTheme(t1 *Theme, t2 *Theme) bool {
return t1 != nil && t2 != nil && !(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 || t1.PartnerImage != t2.PartnerImage || t1.PartnerLink != t2.PartnerLink || t1.PartnerText != t2.PartnerText)
}
// 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, headline, image, partner_img, partner_href, partner_text FROM themes"); err != nil {
return nil, err
} else {
defer rows.Close()
var themes = make([]Theme, 0)
var themes []*Theme
for rows.Next() {
var t Theme
t := &Theme{}
if err := rows.Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Headline, &t.Image, &t.PartnerImage, &t.PartnerLink, &t.PartnerText); err != nil {
return nil, err
}
@ -46,8 +46,8 @@ func GetThemes() ([]Theme, error) {
}
// GetTheme retrieves a Theme from its identifier.
func GetTheme(id int64) (Theme, error) {
var t Theme
func GetTheme(id int64) (*Theme, error) {
t := &Theme{}
if err := DBQueryRow("SELECT id_theme, name, url_id, path, authors, intro, headline, image, partner_img, partner_href, partner_text FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Headline, &t.Image, &t.PartnerImage, &t.PartnerLink, &t.PartnerText); err != nil {
return t, err
}
@ -56,8 +56,8 @@ func GetTheme(id int64) (Theme, error) {
}
// GetThemeByName retrieves a Theme from its title
func GetThemeByName(name string) (Theme, error) {
var t Theme
func GetThemeByName(name string) (*Theme, error) {
t := &Theme{}
if err := DBQueryRow("SELECT id_theme, name, url_id, path, authors, intro, headline, image, partner_img, partner_text FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Headline, &t.Image, &t.PartnerImage, &t.PartnerText); err != nil {
return t, err
}
@ -66,21 +66,19 @@ func GetThemeByName(name string) (Theme, error) {
}
// GetThemeByPath retrieves a Theme from its dirname
func GetThemeByPath(dirname string) (Theme, error) {
var t Theme
if err := DBQueryRow("SELECT id_theme, name, url_id, path, authors, intro, headline, image, partner_img, partner_href, partner_text FROM themes WHERE path=?", dirname).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Headline, &t.Image, &t.PartnerImage, &t.PartnerLink, &t.PartnerText); err != nil {
return t, err
}
func GetThemeByPath(dirname string) (*Theme, error) {
t := &Theme{}
err := DBQueryRow("SELECT id_theme, name, url_id, path, authors, intro, headline, image, partner_img, partner_href, partner_text FROM themes WHERE path=?", dirname).Scan(&t.Id, &t.Name, &t.URLId, &t.Path, &t.Authors, &t.Intro, &t.Headline, &t.Image, &t.PartnerImage, &t.PartnerLink, &t.PartnerText)
return t, nil
return t, err
}
// CreateTheme creates and fills a new struct Theme and registers it into the database.
func CreateTheme(theme Theme) (Theme, error) {
func CreateTheme(theme *Theme) (*Theme, error) {
if res, err := DBExec("INSERT INTO themes (name, url_id, authors, path, intro, headline, image, partner_img, partner_href, partner_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", theme.Name, theme.URLId, theme.Authors, theme.Path, theme.Intro, theme.Headline, theme.Image, theme.PartnerImage, theme.PartnerLink, theme.PartnerText); err != nil {
return Theme{}, err
return nil, err
} else if theme.Id, err = res.LastInsertId(); err != nil {
return Theme{}, err
return nil, err
} else {
return theme, nil
}
@ -97,7 +95,7 @@ func (t *Theme) FixURLId() bool {
}
// 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 = ?, headline = ?, image = ?, partner_img = ?, partner_href = ?, partner_text = ? WHERE id_theme = ?", t.Name, t.URLId, t.Authors, t.Path, t.Intro, t.Headline, t.Image, t.PartnerImage, t.PartnerLink, t.PartnerText, t.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -108,7 +106,7 @@ func (t Theme) Update() (int64, error) {
}
// Delete the theme from the database.
func (t Theme) Delete() (int64, error) {
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 {
@ -118,7 +116,7 @@ func (t Theme) Delete() (int64, error) {
}
}
func (t Theme) deleteFunc(f func(Exercice) (int64, error)) (int64, error) {
func (t *Theme) deleteFunc(f func(*Exercice) (int64, error)) (int64, error) {
exercices, err := t.GetExercices()
if err != nil {
return 0, err
@ -135,8 +133,8 @@ func (t Theme) deleteFunc(f func(Exercice) (int64, error)) (int64, error) {
}
// DeleteCascade the theme from the database, including inner content but not player content.
func (t Theme) DeleteCascade() (int64, error) {
_, err := t.deleteFunc(func(e Exercice) (int64, error) {
func (t *Theme) DeleteCascade() (int64, error) {
_, err := t.deleteFunc(func(e *Exercice) (int64, error) {
return e.DeleteCascade()
})
if err != nil {
@ -147,8 +145,8 @@ func (t Theme) DeleteCascade() (int64, error) {
}
// DeleteCascadePlayer delete player content related to this theme.
func (t Theme) DeleteCascadePlayer() (int64, error) {
_, err := t.deleteFunc(func(e Exercice) (int64, error) {
func (t *Theme) DeleteCascadePlayer() (int64, error) {
_, err := t.deleteFunc(func(e *Exercice) (int64, error) {
return e.DeleteCascadePlayer()
})
if err != nil {
@ -159,8 +157,8 @@ func (t Theme) DeleteCascadePlayer() (int64, error) {
}
// DeleteDeep the theme from the database, including player content.
func (t Theme) DeleteDeep() (int64, error) {
_, err := t.deleteFunc(func(e Exercice) (int64, error) {
func (t *Theme) DeleteDeep() (int64, error) {
_, err := t.deleteFunc(func(e *Exercice) (int64, error) {
return e.DeleteDeep()
})
if err != nil {