From 20c41ec573633980aaf100067fe8192b743d2616 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 5 May 2023 12:04:28 +0200 Subject: [PATCH] admin: Handle exercice path given to auto-sync --- admin/api/sync.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/admin/api/sync.go b/admin/api/sync.go index f9be0378..3f08f716 100644 --- a/admin/api/sync.go +++ b/admin/api/sync.go @@ -203,7 +203,7 @@ func declareSyncExercicesRoutes(router *gin.RouterGroup) { // It'll sync most of modified things, and will delete out of sync data. // Avoid using it in a production environment. func autoSync(c *gin.Context) { - p := strings.TrimPrefix(c.Params.ByName("p"), "/") + p := strings.Split(strings.TrimPrefix(c.Params.ByName("p"), "/"), "/") themes, err := fic.GetThemes() if err != nil { @@ -212,7 +212,8 @@ func autoSync(c *gin.Context) { return } - if p == "" { + // No argument, do a deep sync + if len(p) == 0 { if !IsProductionEnv { for _, theme := range themes { theme.DeleteDeep() @@ -226,8 +227,9 @@ func autoSync(c *gin.Context) { var theTheme *fic.Theme + // Find the given theme for _, theme := range themes { - if theme.Path == p { + if theme.Path == p[0] { theTheme = theme break } @@ -238,24 +240,26 @@ func autoSync(c *gin.Context) { rThemes, err := sync.GetThemes(sync.GlobalImporter) if err == nil { for _, theme := range rThemes { - if theme == p { + if theme == p[0] { sync.SyncThemes(sync.GlobalImporter) themes, err := fic.GetThemes() if err == nil { for _, theme := range themes { - if theme.Path == p { + if theme.Path == p[0] { theTheme = theme break } } } + + break } } } if theTheme == nil { - c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": fmt.Sprintf("Theme not found %q", p)}) + c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": fmt.Sprintf("Theme not found %q", p[0])}) return } } @@ -264,7 +268,9 @@ func autoSync(c *gin.Context) { exercices, err := theTheme.GetExercices() if err == nil { for _, exercice := range exercices { - exercice.DeleteDeep() + if len(p) <= 1 || exercice.Path == path.Join(p[0], p[1]) { + exercice.DeleteDeep() + } } } }