sync: refactor exercice synchronization
This commit is contained in:
parent
5b53fbda0b
commit
dc4a4925e3
5 changed files with 237 additions and 134 deletions
|
|
@ -2,6 +2,7 @@ package fic
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -122,25 +123,76 @@ func (t Theme) GetExercices() ([]Exercice, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// AddExercice creates and fills a new struct Exercice and registers it into the database.
|
||||
func (t Theme) AddExercice(title string, urlId string, path string, statement string, overview string, headline string, depend *Exercice, gain int64, videoURI string, finished string) (Exercice, error) {
|
||||
var dpd interface{}
|
||||
if depend == nil {
|
||||
dpd = nil
|
||||
} else {
|
||||
dpd = depend.Id
|
||||
}
|
||||
if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, headline, issue, depend, gain, video_uri, finished) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", t.Id, title, urlId, path, statement, overview, headline, "", dpd, gain, videoURI, finished); err != nil {
|
||||
return Exercice{}, err
|
||||
} else if eid, err := res.LastInsertId(); err != nil {
|
||||
return Exercice{}, err
|
||||
} else {
|
||||
if depend == nil {
|
||||
return Exercice{eid, title, urlId, path, statement, overview, headline, finished, "", "info", nil, gain, 1.0, videoURI}, nil
|
||||
} else {
|
||||
return Exercice{eid, title, urlId, path, statement, overview, headline, finished, "", "info", &depend.Id, gain, 1.0, videoURI}, nil
|
||||
// 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 {
|
||||
// Force ID
|
||||
e.Id = search.Id
|
||||
|
||||
// Don't expect those values
|
||||
if e.Coefficient == 0 {
|
||||
e.Coefficient = search.Coefficient
|
||||
}
|
||||
if len(e.Issue) == 0 {
|
||||
e.Issue = search.Issue
|
||||
}
|
||||
if len(e.IssueKind) == 0 {
|
||||
e.IssueKind = search.IssueKind
|
||||
}
|
||||
|
||||
_, err = e.Update()
|
||||
} else {
|
||||
err = t.addExercice(e)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (t Theme) addExercice(e *Exercice) (err error) {
|
||||
var ik = "DEFAULT"
|
||||
if len(e.IssueKind) > 0 {
|
||||
ik = fmt.Sprintf("%q", e.IssueKind)
|
||||
}
|
||||
|
||||
var cc = "DEFAULT"
|
||||
if e.Coefficient != 0 {
|
||||
cc = fmt.Sprintf("%f", e.Coefficient)
|
||||
}
|
||||
|
||||
if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + ik + ", " + cc + ")", t.Id, e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI); err != nil {
|
||||
return err
|
||||
} else if eid, err := res.LastInsertId(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
e.Id = eid
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// AddExercice creates and fills a new struct Exercice and registers it into the database.
|
||||
func (t Theme) AddExercice(title string, urlId string, path string, statement string, overview string, headline string, depend *Exercice, gain int64, videoURI string, finished string) (e Exercice, err error) {
|
||||
var dpd *int64 = nil
|
||||
if depend != nil {
|
||||
dpd = &depend.Id
|
||||
}
|
||||
|
||||
e = Exercice{
|
||||
Title: title,
|
||||
URLId: urlId,
|
||||
Path: path,
|
||||
Statement: statement,
|
||||
Overview: overview,
|
||||
Headline: headline,
|
||||
Depend: dpd,
|
||||
Finished: finished,
|
||||
Gain: gain,
|
||||
VideoURI: videoURI,
|
||||
}
|
||||
|
||||
err = t.addExercice(&e)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Update applies modifications back to the database.
|
||||
|
|
|
|||
Reference in a new issue