qa: New route to assign all exercices

This commit is contained in:
nemunaire 2024-09-18 12:32:11 +02:00
parent 1f295c3411
commit 2f6c7ecd8b
2 changed files with 93 additions and 3 deletions

View file

@ -1,8 +1,11 @@
package api
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"srs.epita.fr/fic-server/libfic"
@ -17,6 +20,7 @@ func declareTodoRoutes(router *gin.RouterGroup) {
}
func declareTodoManagerRoutes(router *gin.RouterGroup) {
router.POST("/qa_assign_work", assignWork)
router.POST("/qa_my_exercices.json", addQAView)
router.POST("/qa_work.json", createQATodo)
@ -235,3 +239,55 @@ func deleteQATodo(c *gin.Context) {
c.Status(http.StatusOK)
}
}
type QAAssignWork struct {
Turns int `json:"turns"`
Start int `json:"start"`
}
func assignWork(c *gin.Context) {
var uaw QAAssignWork
if err := c.ShouldBindJSON(&uaw); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
if uaw.Turns == 0 {
uaw.Turns = 1
}
teams, err := fic.GetTeams()
if err != nil {
log.Println("Unable to GetTeams: ", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to list teams: %s", err.Error())})
return
}
// Remove assistant team
for tid := len(teams) - 1; tid >= 0; tid-- {
team := teams[tid]
if strings.Contains(strings.ToLower(team.Name), "assistants") {
teams = append(teams[:tid], teams[tid+1:]...)
}
}
exercices, err := fic.GetExercices()
if err != nil {
log.Println("Unable to GetExercices: ", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to list exercices: %s", err.Error())})
return
}
for i := 0; i < uaw.Turns; i++ {
for eid, ex := range exercices {
team := teams[(uaw.Start+eid+uaw.Turns*i)%len(teams)]
_, err := team.NewQATodo(ex.Id)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
}
}
c.JSON(http.StatusOK, "true")
}