Gradation: add route to add missing users
This commit is contained in:
parent
107b17c11f
commit
3397b9f123
4 changed files with 104 additions and 6 deletions
43
works.go
43
works.go
|
@ -179,6 +179,49 @@ func declareAPIAdminWorksRoutes(router *gin.RouterGroup) {
|
|||
|
||||
c.JSON(http.StatusOK, grades)
|
||||
})
|
||||
worksRoutes.PATCH("/grades", func(c *gin.Context) {
|
||||
w := c.MustGet("work").(*Work)
|
||||
|
||||
// Fetch existing grades
|
||||
grades, err := w.GetGrades("")
|
||||
if err != nil {
|
||||
log.Printf("Unable to GetGrades(wid=%d): %s", w.Id, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during grades retrieval."})
|
||||
return
|
||||
}
|
||||
|
||||
// Create an index
|
||||
known_users := map[int64]bool{}
|
||||
for _, g := range grades {
|
||||
known_users[g.IdUser] = true
|
||||
}
|
||||
|
||||
// Fetch students list registered for this course
|
||||
users, err := getFilteredUsers(w.Promo, w.Group)
|
||||
if err != nil {
|
||||
log.Printf("Unable to getFilteredUsers(%d, %s): %s", w.Promo, w.Group, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during users retrieval."})
|
||||
return
|
||||
}
|
||||
|
||||
var toAdd []WorkGrade
|
||||
for _, user := range users {
|
||||
if _, ok := known_users[user.Id]; !ok {
|
||||
toAdd = append(toAdd, WorkGrade{
|
||||
IdUser: user.Id,
|
||||
Login: user.Login,
|
||||
Grade: 0,
|
||||
Comment: "- Non rendu -",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(toAdd) > 0 {
|
||||
w.AddGrades(toAdd)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, toAdd)
|
||||
})
|
||||
worksRoutes.PUT("/grades", func(c *gin.Context) {
|
||||
w := c.MustGet("work").(*Work)
|
||||
|
||||
|
|
Reference in a new issue