admin: new route to fill URLIds if they are not defined
This commit is contained in:
parent
8ae4d1c42f
commit
6d1ef0f51c
|
@ -50,6 +50,14 @@ func init() {
|
|||
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceKeys(sync.GlobalImporter, exercice), nil })))
|
||||
router.POST("/api/sync/exercices/:eid/quiz", apiHandler(exerciceHandler(
|
||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceMCQ(sync.GlobalImporter, exercice), nil })))
|
||||
|
||||
router.POST("/api/sync/exercices/:eid/fixurlid", apiHandler(exerciceHandler(
|
||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||
if exercice.FixURLId() {
|
||||
return exercice.Update()
|
||||
}
|
||||
return 0, nil
|
||||
})))
|
||||
}
|
||||
|
||||
func listExercices(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
|
|
|
@ -59,6 +59,38 @@ func init() {
|
|||
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceKeys(sync.GlobalImporter, exercice), nil })))
|
||||
router.POST("/api/sync/themes/:thid/exercices/:eid/quiz", apiHandler(exerciceHandler(
|
||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceMCQ(sync.GlobalImporter, exercice), nil })))
|
||||
|
||||
router.POST("/api/sync/themes/:thid/fixurlid", apiHandler(themeHandler(
|
||||
func(theme fic.Theme, _ []byte) (interface{}, error) {
|
||||
if theme.FixURLId() {
|
||||
return theme.Update()
|
||||
}
|
||||
return 0, nil
|
||||
})))
|
||||
router.POST("/api/sync/fixurlids", apiHandler(fixAllURLIds))
|
||||
}
|
||||
|
||||
func fixAllURLIds(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
nbFix := 0
|
||||
if themes, err := fic.GetThemes(); err == nil {
|
||||
for _, theme := range themes {
|
||||
if theme.FixURLId() {
|
||||
theme.Update()
|
||||
nbFix += 1
|
||||
}
|
||||
|
||||
if exercices, err := theme.GetExercices(); err == nil {
|
||||
for _, exercice := range exercices {
|
||||
if exercice.FixURLId() {
|
||||
exercice.Update()
|
||||
nbFix += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nbFix, nil
|
||||
}
|
||||
|
||||
func bindingFiles(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
|
|
|
@ -122,6 +122,14 @@ func (e Exercice) Update() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (e *Exercice) FixURLId() bool {
|
||||
if e.URLId == "" {
|
||||
e.URLId = ToURLid(e.Title)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e Exercice) GetThemeId() (int, error) {
|
||||
var tid int
|
||||
if err := DBQueryRow("SELECT id_theme FROM exercices WHERE id_exercice=?", e.Id).Scan(&tid); err != nil {
|
||||
|
|
|
@ -60,6 +60,14 @@ func CreateTheme(name string, url_id string, authors string, intro string) (Them
|
|||
}
|
||||
}
|
||||
|
||||
func (t *Theme) FixURLId() bool {
|
||||
if t.URLId == "" {
|
||||
t.URLId = ToURLid(t.Name)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue