admin: Add new API route to perform smart theme update controled by hooks

This commit is contained in:
Pierre-Olivier Mercier 2021-11-13 02:00:24 +01:00
parent 1a1343596a
commit a06602a7e8

View File

@ -5,9 +5,11 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
"srs.epita.fr/fic-server/settings"
"github.com/julienschmidt/httprouter"
)
@ -71,6 +73,49 @@ func init() {
sync.DeepSyncProgress = 255
return st, nil
})))
router.POST("/api/sync/auto/*p", apiHandler(
func(ps httprouter.Params, _ []byte) (interface{}, error) {
p := strings.TrimPrefix(ps.ByName("p"), "/")
themes, err := fic.GetThemes()
if err != nil {
return nil, err
}
if p == "" {
if !IsProductionEnv {
for _, theme := range themes {
theme.DeleteDeep()
}
}
st := sync.SyncDeep(sync.GlobalImporter)
return st, nil
}
for _, theme := range themes {
if theme.Path == p {
if !IsProductionEnv {
exercices, err := theme.GetExercices()
if err == nil {
for _, exercice := range exercices {
exercice.DeleteDeep()
}
}
}
st := sync.SyncThemeDeep(sync.GlobalImporter, theme, 0, 250)
sync.EditDeepReport(map[string][]string{theme.Name: st}, false)
sync.DeepSyncProgress = 255
settings.ForceRegeneration()
return st, nil
}
}
return nil, fmt.Errorf("Theme not found %q", p)
}))
router.POST("/api/sync/themes", apiHandler(
func(_ httprouter.Params, _ []byte) (interface{}, error) {
return sync.SyncThemes(sync.GlobalImporter), nil
@ -215,12 +260,12 @@ func getThemedExercicesStats(theme fic.Theme, body []byte) (interface{}, error)
ret := []exerciceStats{}
for _, e := range exercices {
ret = append(ret, exerciceStats{
IdExercice: e.Id,
TeamTries: e.TriedTeamCount(),
TotalTries: e.TriedCount(),
IdExercice: e.Id,
TeamTries: e.TriedTeamCount(),
TotalTries: e.TriedCount(),
SolvedCount: e.SolvedCount(),
FlagSolved: e.FlagSolved(),
MCQSolved: e.MCQSolved(),
FlagSolved: e.FlagSolved(),
MCQSolved: e.MCQSolved(),
})
}
return ret, nil