server/qa/api/todo.go

238 lines
5.2 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"
"strconv"
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.GET("/qa_work.json", getQATodo)
}
func declareTodoManagerRoutes(router *gin.RouterGroup) {
router.POST("/qa_my_exercices.json", addQAView)
2022-11-06 15:36:31 +00:00
router.POST("/qa_work.json", createQATodo)
todosRoutes := router.Group("/todo/:wid")
todosRoutes.Use(todoHandler)
todosRoutes.GET("", showTodo)
todosRoutes.DELETE("", deleteQATodo)
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) {
var teamid int64
if team, ok := c.Get("team"); ok {
teamid = team.(*fic.Team).Id
} else {
teamid = c.MustGet("LoggedTeam").(int64)
}
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
}
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) {
var teamid int64
if team, ok := c.Get("team"); ok {
teamid = team.(*fic.Team).Id
} else {
teamid = c.MustGet("LoggedTeam").(int64)
}
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
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) {
var teamid int64
if team, ok := c.Get("team"); ok {
teamid = team.(*fic.Team).Id
} else {
teamid = c.MustGet("LoggedTeam").(int64)
}
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
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) {
var teamid int64
if team, ok := c.Get("team"); ok {
teamid = team.(*fic.Team).Id
} else {
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{
Id: 0,
IdTeam: teamid,
IdExercice: exercice.Id,
})
2022-11-06 15:36:31 +00:00
}
}
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) {
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) {
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
}
func todoHandler(c *gin.Context) {
team := c.MustGet("team").(*fic.Team)
var wid int64
var todos []*fic.QATodo
var err error
if wid, err = strconv.ParseInt(string(c.Param("wid")), 10, 64); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Bad todo identifier."})
return
}
if todos, err = team.GetQATodo(); err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Todo not found."})
return
}
for _, t := range todos {
if t.Id == wid {
c.Set("todo", t)
c.Next()
return
}
}
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Unable to find the requested QA Todo"})
}
func showTodo(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("todo"))
}
func deleteQATodo(c *gin.Context) {
todo := c.MustGet("todo").(*fic.QATodo)
if _, err := todo.Delete(); err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
} else {
c.Status(http.StatusOK)
}
}