From 63cf665f2dae854558664289950c55a9afabb8fe Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 5 May 2023 11:44:09 +0200 Subject: [PATCH] admin: Refactor sync/auto --- admin/api/sync.go | 53 +++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/admin/api/sync.go b/admin/api/sync.go index 22f63154..a9ce8592 100644 --- a/admin/api/sync.go +++ b/admin/api/sync.go @@ -224,32 +224,39 @@ func autoSync(c *gin.Context) { return } + var theTheme *fic.Theme + for _, theme := range themes { if theme.Path == p { - if !IsProductionEnv { - exercices, err := theme.GetExercices() - if err == nil { - for _, exercice := range exercices { - exercice.DeleteDeep() - } - } - } - - exceptions := sync.LoadException(sync.GlobalImporter, theme) - - var st []string - for _, se := range sync.SyncThemeDeep(sync.GlobalImporter, theme, 0, 250, exceptions) { - st = append(st, se.Error()) - } - sync.EditDeepReport(&sync.SyncReport{Themes: map[string][]string{theme.Name: st}}, false) - sync.DeepSyncProgress = 255 - - settings.ForceRegeneration() - - c.JSON(http.StatusOK, st) - return + theTheme = theme + break } } - c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": fmt.Sprintf("Theme not found %q", p)}) + if theTheme == nil { + c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": fmt.Sprintf("Theme not found %q", p)}) + return + } + + if !IsProductionEnv { + exercices, err := theTheme.GetExercices() + if err == nil { + for _, exercice := range exercices { + exercice.DeleteDeep() + } + } + } + + exceptions := sync.LoadException(sync.GlobalImporter, theTheme) + + var st []string + for _, se := range sync.SyncThemeDeep(sync.GlobalImporter, theTheme, 0, 250, exceptions) { + st = append(st, se.Error()) + } + sync.EditDeepReport(&sync.SyncReport{Themes: map[string][]string{theTheme.Name: st}}, false) + sync.DeepSyncProgress = 255 + + settings.ForceRegeneration() + + c.JSON(http.StatusOK, st) }