admin: New option to drop all solutions from the database
This commit is contained in:
parent
79afaa8fb2
commit
daae6f4f07
3 changed files with 54 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
|
|
@ -136,6 +137,45 @@ func declareSyncRoutes(router *gin.RouterGroup) {
|
|||
|
||||
c.JSON(http.StatusOK, true)
|
||||
})
|
||||
|
||||
// Remove soluces from the database.
|
||||
apiSyncRoutes.POST("/drop_soluces", func(c *gin.Context) {
|
||||
exercices, err := fic.GetExercices()
|
||||
if err != nil {
|
||||
log.Println("Unable to GetExercices:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieve exercices list."})
|
||||
return
|
||||
}
|
||||
|
||||
var errs error
|
||||
for _, e := range exercices {
|
||||
// Remove any published video
|
||||
if len(e.VideoURI) > 0 && strings.HasPrefix(e.VideoURI, "$FILES$") {
|
||||
vpath := path.Join(fic.FilesDir, strings.TrimPrefix(e.VideoURI, "$FILES$/"))
|
||||
err = os.Remove(vpath)
|
||||
if err != nil {
|
||||
errs = multierr.Append(errs, fmt.Errorf("unable to delete published video (%q): %w", e.VideoURI, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Clean the database
|
||||
if len(e.VideoURI) > 0 || len(e.Resolution) > 0 {
|
||||
e.VideoURI = ""
|
||||
e.Resolution = ""
|
||||
|
||||
_, err = e.Update()
|
||||
if err != nil {
|
||||
errs = multierr.Append(errs, fmt.Errorf("unable to update exercice (%d: %s): %w", e.Id, e.Title, err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errmsg": flatifySyncErrors(err)})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, true)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func declareSyncExercicesRoutes(router *gin.RouterGroup) {
|
||||
|
|
|
|||
Reference in a new issue