admin: Reexpose themed exercices_stats.json

Lost in 8b3fbdb64a
This commit is contained in:
nemunaire 2024-03-12 10:38:41 +01:00
parent c7fc18bfb4
commit 0c45b52e04
1 changed files with 22 additions and 28 deletions

View File

@ -48,6 +48,8 @@ func declareThemesRoutes(router *gin.RouterGroup) {
apiThemesRoutes.PUT("", updateTheme)
apiThemesRoutes.DELETE("", deleteTheme)
apiThemesRoutes.GET("/exercices_stats.json", getThemedExercicesStats)
declareExercicesRoutes(apiThemesRoutes)
// Remote
@ -117,18 +119,6 @@ func bindingFiles(c *gin.Context) {
c.String(http.StatusOK, ret)
}
func getExercice(args []string) (*fic.Exercice, error) {
if tid, err := strconv.ParseInt(string(args[0]), 10, 64); err != nil {
return nil, err
} else if theme, err := fic.GetTheme(tid); err != nil {
return nil, err
} else if eid, err := strconv.Atoi(string(args[1])); err != nil {
return nil, err
} else {
return theme.GetExercice(eid)
}
}
func listThemes(c *gin.Context) {
themes, err := fic.GetThemes()
if err != nil {
@ -243,21 +233,25 @@ func deleteTheme(c *gin.Context) {
c.JSON(http.StatusOK, true)
}
func getThemedExercicesStats(theme *fic.Theme, body []byte) (interface{}, error) {
if exercices, err := theme.GetExercices(); err != nil {
return nil, err
} else {
ret := []exerciceStats{}
for _, e := range exercices {
ret = append(ret, exerciceStats{
IdExercice: e.Id,
TeamTries: e.TriedTeamCount(),
TotalTries: e.TriedCount(),
SolvedCount: e.SolvedCount(),
FlagSolved: e.FlagSolved(),
MCQSolved: e.MCQSolved(),
})
}
return ret, nil
func getThemedExercicesStats(c *gin.Context) {
theme := c.MustGet("theme").(*fic.Theme)
exercices, err := theme.GetExercices()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to fetch exercices: %s", err.Error())})
return
}
ret := []exerciceStats{}
for _, e := range exercices {
ret = append(ret, exerciceStats{
IdExercice: e.Id,
TeamTries: e.TriedTeamCount(),
TotalTries: e.TriedCount(),
SolvedCount: e.SolvedCount(),
FlagSolved: e.FlagSolved(),
MCQSolved: e.MCQSolved(),
})
}
c.JSON(http.StatusOK, ret)
}