admin/sync: handle dependancy between exercices

This commit is contained in:
nemunaire 2017-12-17 16:00:32 +01:00
parent 954dd7540a
commit bfca9d3bc2

View File

@ -2,7 +2,6 @@ package sync
import (
"fmt"
"log"
"path"
"strings"
"strconv"
@ -32,6 +31,7 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
if exercices, err := getExercices(i, theme); err != nil {
errs = append(errs, err.Error())
} else {
dmap := map[int]fic.Exercice{}
emap := map[string]int{}
for _, edir := range exercices {
edir_splt := strings.SplitN(edir, "-", 2)
@ -61,9 +61,6 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
continue
}
// TODO: retrieve depends
var depend *fic.Exercice = nil
// TODO: calculate gain
var gain int64 = 42
@ -71,37 +68,58 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
videoURI := ""
if e, err := theme.GetExerciceByTitle(ename); err != nil {
if _, err := theme.AddExercice(ename, path.Join(theme.Name, edir), statement, overview, nil, gain, videoURI); err != nil {
if ex, err := theme.AddExercice(ename, path.Join(theme.Name, edir), statement, overview, nil, gain, videoURI); err != nil {
errs = append(errs, fmt.Sprintf("%q: error on exercice add: %s", edir, err))
continue
} else {
dmap[eid] = ex
}
} else if e.Title != ename || e.Statement != statement || e.Overview != overview || (depend == nil && e.Depend != nil) || (depend != nil && e.Depend == nil) || (depend != nil && e.Depend != nil && *e.Depend != depend.Id) || e.Gain != gain || e.VideoURI != videoURI {
} else if e.Title != ename || e.Statement != statement || e.Overview != overview || e.Gain != gain || e.VideoURI != videoURI {
e.Title = ename
e.Statement = statement
e.Overview = overview
if depend != nil {
e.Depend = &depend.Id
} else {
e.Depend = nil
}
e.Gain = gain
e.VideoURI = videoURI
if _, err := e.Update(); err != nil {
errs = append(errs, fmt.Sprintf("%q: error on exercice update: %s", edir, err))
continue
} else {
dmap[eid] = e
}
} else {
dmap[eid] = e
}
}
// Remove old exercices
if exercices, err := theme.GetExercices(); err == nil {
log.Println(emap)
for _, ex := range exercices {
if _, ok := emap[ex.Title]; !ok {
log.Println(ok)
ex.Delete()
} else {
log.Println(ok)
}
}
}
dmap_keys := []string{}
for k, _ := range dmap {
dmap_keys = append(dmap_keys, fmt.Sprintf("%d", k))
}
for _, e := range dmap {
// Treat depends.txt
if depends, err := getFileContent(i, path.Join(e.Path, "depends.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: depends.txt: %s", path.Base(e.Path), err))
} else {
for nline, dep := range strings.Split(depends, "\n") {
if did, err := strconv.Atoi(dep); err != nil {
errs = append(errs, fmt.Sprintf("%q: depends.txt:%d: %s", path.Base(e.Path), nline + 1, err))
continue
} else if exdep, exist := dmap[did]; !exist {
errs = append(errs, fmt.Sprintf("%q: depends.txt:%d: Unable to find required dependancy %q (%d ; available: %s)", path.Base(e.Path), nline + 1, dep, did, strings.Join(dmap_keys, ",")))
continue
} else {
e.Depend = &exdep.Id
e.Update()
}
}
}
}