server/qa/api/todo.go

177 lines
3.9 KiB
Go
Raw Normal View History

2020-09-08 11:30:28 +00:00
package api
import (
2022-11-06 15:36:31 +00:00
"net/http"
2020-09-08 11:30:28 +00:00
"srs.epita.fr/fic-server/libfic"
2022-11-06 15:36:31 +00:00
"github.com/gin-gonic/gin"
2020-09-08 11:30:28 +00:00
)
2022-11-06 15:36:31 +00:00
func declareTodoRoutes(router *gin.RouterGroup) {
router.GET("/qa_exercices.json", getExerciceTested)
router.GET("/qa_mywork.json", getQAWork)
router.GET("/qa_myexercices.json", getQAView)
router.POST("/qa_my_exercices.json", addQAView)
router.GET("/qa_work.json", getQATodo)
router.POST("/qa_work.json", createQATodo)
2020-09-08 11:30:28 +00:00
}
2020-09-09 19:17:09 +00:00
type exerciceTested map[int64]string
2022-11-06 15:36:31 +00:00
func getExerciceTested(c *gin.Context) {
teamid := c.MustGet("LoggedTeam").(int64)
team, err := fic.GetTeam(teamid)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
exercices, err := fic.GetExercices()
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
ret := exerciceTested{}
for _, exercice := range exercices {
if team.HasAccess(exercice) {
if t := team.HasSolved(exercice); t != nil {
ret[exercice.Id] = "solved"
} else if cnt, _ := team.CountTries(exercice); cnt > 0 {
ret[exercice.Id] = "tried"
} else {
ret[exercice.Id] = "access"
2020-09-09 19:17:09 +00:00
}
}
}
2022-11-06 15:36:31 +00:00
c.JSON(http.StatusOK, ret)
2020-09-09 19:17:09 +00:00
}
2022-11-06 15:36:31 +00:00
func getQAView(c *gin.Context) {
teamid := c.MustGet("LoggedTeam").(int64)
team, err := fic.GetTeam(teamid)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
2020-11-13 12:11:58 +00:00
}
2022-11-06 15:36:31 +00:00
view, err := team.GetQAView()
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, view)
2020-11-13 12:11:58 +00:00
}
2022-11-06 15:36:31 +00:00
func getQAWork(c *gin.Context) {
teamid := c.MustGet("LoggedTeam").(int64)
team, err := fic.GetTeam(teamid)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
2020-09-08 11:30:28 +00:00
}
2022-11-06 15:36:31 +00:00
queries, err := team.GetQAQueries()
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, queries)
2020-09-08 11:30:28 +00:00
}
2022-11-06 15:36:31 +00:00
func getQATodo(c *gin.Context) {
teamid := c.MustGet("LoggedTeam").(int64)
2021-02-05 11:05:23 +00:00
2022-11-06 15:36:31 +00:00
team, err := fic.GetTeam(teamid)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
todo, err := team.GetQATodo()
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
2021-02-05 11:05:23 +00:00
2022-11-06 15:36:31 +00:00
exercices, err := fic.GetExercices()
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
2020-09-08 11:30:28 +00:00
}
2022-11-06 15:36:31 +00:00
for _, exercice := range exercices {
if cnt, _ := team.CountTries(exercice); cnt > 0 {
todo = append(todo, &fic.QATodo{0, teamid, exercice.Id})
}
}
c.JSON(http.StatusOK, todo)
2020-09-08 11:30:28 +00:00
}
2022-11-06 15:36:31 +00:00
func createQATodo(c *gin.Context) {
ficteam := c.MustGet("LoggedUser").(string)
if ficteam != "nemunaire" {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"errmsg": "Restricted"})
return
2020-09-08 11:30:28 +00:00
}
var ut fic.QATodo
2022-11-06 15:36:31 +00:00
if err := c.ShouldBindJSON(&ut); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
team, err := fic.GetTeam(ut.IdTeam)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
2020-09-08 11:30:28 +00:00
}
2022-11-06 15:36:31 +00:00
todo, err := team.NewQATodo(ut.IdExercice)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
2020-09-08 11:30:28 +00:00
}
2022-11-06 15:36:31 +00:00
c.JSON(http.StatusOK, todo)
2020-09-08 11:30:28 +00:00
}
2020-11-13 12:11:58 +00:00
2022-11-06 15:36:31 +00:00
func addQAView(c *gin.Context) {
ficteam := c.MustGet("LoggedUser").(string)
if ficteam != "nemunaire" {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"errmsg": "Restricted"})
return
2020-11-13 12:11:58 +00:00
}
var ut fic.QATodo
2022-11-06 15:36:31 +00:00
if err := c.ShouldBindJSON(&ut); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
team, err := fic.GetTeam(ut.IdTeam)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
2020-11-13 12:11:58 +00:00
}
2022-11-06 15:36:31 +00:00
view, err := team.NewQAView(ut.IdExercice)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
2020-11-13 12:11:58 +00:00
}
2022-11-06 15:36:31 +00:00
c.JSON(http.StatusOK, view)
2020-11-13 12:11:58 +00:00
}