admin: Handle exercice path given to auto-sync

This commit is contained in:
nemunaire 2023-05-05 12:04:28 +02:00
parent 75eae43f60
commit 20c41ec573

View file

@ -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()
}
}
}
}