Able to sync and export standalone exercices

This commit is contained in:
nemunaire 2024-03-15 17:46:50 +01:00
parent 76f830b332
commit adb0e36dd4
15 changed files with 159 additions and 31 deletions

View file

@ -91,17 +91,44 @@ func GetExercice(id int64) (*Exercice, error) {
// GetExercice retrieves the challenge with the given id.
func (t *Theme) GetExercice(id int) (*Exercice, error) {
return getExercice("exercices", "WHERE id_theme = ? AND id_exercice = ?", t.Id, id)
query := "WHERE id_exercice = ? AND id_theme = ?"
args := []interface{}{id}
if t.GetId() == nil {
query = "WHERE id_exercice = ? AND id_theme IS NULL"
} else {
args = append(args, t.GetId())
}
return getExercice("exercices", query, args...)
}
// GetExerciceByTitle retrieves the challenge with the given title.
func (t *Theme) GetExerciceByTitle(title string) (*Exercice, error) {
return getExercice("exercices", "WHERE id_theme = ? AND title = ?", t.Id, title)
query := "WHERE title = ? AND id_theme = ?"
args := []interface{}{title}
if t.GetId() == nil {
query = "WHERE title = ? AND id_theme IS NULL"
} else {
args = append(args, t.GetId())
}
return getExercice("exercices", query, args...)
}
// GetExerciceByPath retrieves the challenge with the given path.
func (t *Theme) GetExerciceByPath(epath string) (*Exercice, error) {
return getExercice("exercices", "WHERE id_theme = ? AND path = ?", t.Id, epath)
query := "WHERE path = ? AND id_theme = ?"
args := []interface{}{epath}
if t.GetId() == nil {
query = "WHERE path = ? AND id_theme IS NULL"
} else {
args = append(args, t.GetId())
}
return getExercice("exercices", query, args...)
}
// GetDiscountedExercice retrieves the challenge with the given id.
@ -153,7 +180,15 @@ func GetDiscountedExercices() ([]*Exercice, error) {
// GetExercices returns the list of all challenges in the Theme.
func (t *Theme) GetExercices() ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, authors, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? ORDER BY path ASC", t.Id); err != nil {
query := "SELECT id_exercice, id_theme, title, authors, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme IS NULL ORDER BY path ASC"
args := []interface{}{}
if t.GetId() != nil {
query = "SELECT id_exercice, id_theme, title, authors, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? ORDER BY path ASC"
args = append(args, t.GetId())
}
if rows, err := DBQuery(query, args...); err != nil {
return nil, err
} else {
defer rows.Close()
@ -224,7 +259,7 @@ func (t *Theme) addExercice(e *Exercice) (err error) {
if e.WIP {
wip = "%"
}
if res, err := DBExec("INSERT INTO exercices (id_theme, title, authors, image, disabled, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, resolution, seealso, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.Id, wip+e.Title, e.Authors, e.Image, e.Disabled, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI, e.Resolution, e.SeeAlso); err != nil {
if res, err := DBExec("INSERT INTO exercices (id_theme, title, authors, image, disabled, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, resolution, seealso, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.GetId(), wip+e.Title, e.Authors, e.Image, e.Disabled, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI, e.Resolution, e.SeeAlso); err != nil {
return err
} else if eid, err := res.LastInsertId(); err != nil {
return err