fic: SaveNamedExercice on title and path

This commit is contained in:
nemunaire 2022-01-20 17:56:39 +01:00
parent 40a9078b70
commit 081ad1f928
1 changed files with 25 additions and 17 deletions

View File

@ -55,10 +55,9 @@ type Exercice struct {
SeeAlso string `json:"seealso"`
}
// GetExercice retrieves the challenge with the given id.
func GetExercice(id int64) (*Exercice, error) {
func getExercice(condition string, args ...interface{}) (*Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices "+condition, args...).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
return nil, err
}
@ -66,23 +65,23 @@ func GetExercice(id int64) (*Exercice, error) {
}
// GetExercice retrieves the challenge with the given id.
func (t *Theme) GetExercice(id int) (*Exercice, error) {
e := &Exercice{}
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
return nil, err
}
func GetExercice(id int64) (*Exercice, error) {
return getExercice("WHERE id_exercice = ?", id)
}
return e, nil
// GetExercice retrieves the challenge with the given id.
func (t *Theme) GetExercice(id int) (*Exercice, error) {
return getExercice("WHERE id_theme = ? AND id_exercice = ?", t.Id, id)
}
// GetExerciceByTitle retrieves the challenge with the given title.
func (t *Theme) GetExerciceByTitle(title string) (*Exercice, error) {
e := &Exercice{}
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.IdTheme, &e.Title, &t.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
return nil, err
}
return getExercice("WHERE id_theme = ? AND title = ?", t.Id, title)
}
return e, nil
// GetExerciceByPath retrieves the challenge with the given path.
func (t *Theme) GetExerciceByPath(epath string) (*Exercice, error) {
return getExercice("WHERE id_theme = ? AND path = ?", t.Id, epath)
}
// GetExercices returns the list of all challenges present in the database.
@ -134,7 +133,18 @@ func (t *Theme) GetExercices() ([]*Exercice, error) {
// SaveNamedExercice looks for an exercice with the same title to update it, or create it if it doesn't exists yet.
func (t *Theme) SaveNamedExercice(e *Exercice) (err error) {
var search *Exercice
if search, err = t.GetExerciceByTitle(e.Title); err == nil {
// Search same title
search, _ = t.GetExerciceByTitle(e.Title)
// Search on same path
if search == nil && len(e.Path) > 0 {
search, err = t.GetExerciceByPath(e.Path)
}
if search == nil {
err = t.addExercice(e)
} else {
// Force ID
e.Id = search.Id
@ -150,8 +160,6 @@ func (t *Theme) SaveNamedExercice(e *Exercice) (err error) {
}
_, err = e.Update()
} else {
err = t.addExercice(e)
}
return
}