2021-02-05 15:56:27 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-05-16 09:38:46 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-02-05 15:56:27 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-02-05 15:56:27 +00:00
|
|
|
)
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func declareQARoutes(router *gin.RouterGroup) {
|
|
|
|
router.POST("/qa/", importExerciceQA)
|
|
|
|
|
|
|
|
apiQARoutes := router.Group("/qa/:qid")
|
2024-03-12 09:12:05 +00:00
|
|
|
apiQARoutes.Use(QAHandler)
|
2022-05-16 09:38:46 +00:00
|
|
|
apiQARoutes.POST("/comments", importQAComment)
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func QAHandler(c *gin.Context) {
|
|
|
|
qid, err := strconv.ParseInt(string(c.Params.ByName("qid")), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Invalid QA identifier"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
qa, err := fic.GetQAQuery(qid)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "QA query not found"})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
2022-05-16 09:38:46 +00:00
|
|
|
|
|
|
|
c.Set("qa-query", qa)
|
|
|
|
|
|
|
|
c.Next()
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func importExerciceQA(c *gin.Context) {
|
2021-02-05 15:56:27 +00:00
|
|
|
// Create a new query
|
|
|
|
var uq fic.QAQuery
|
2022-05-16 09:38:46 +00:00
|
|
|
err := c.ShouldBindJSON(&uq)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:35:07 +00:00
|
|
|
var exercice *fic.Exercice
|
2021-02-05 15:56:27 +00:00
|
|
|
if uq.IdExercice == 0 {
|
2022-05-16 09:38:46 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "id_exercice not filled"})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
} else if exercice, err = fic.GetExercice(uq.IdExercice); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Unable to find requested exercice"})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(uq.State) == 0 {
|
2022-05-16 09:38:46 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "State not filled"})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(uq.Subject) == 0 {
|
2022-05-16 09:38:46 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Subject not filled"})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 01:00:22 +00:00
|
|
|
if qa, err := exercice.NewQAQuery(uq.Subject, uq.IdTeam, uq.User, uq.State, nil); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to importExerciceQA:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during query creation."})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
} else {
|
|
|
|
qa.Creation = uq.Creation
|
|
|
|
qa.Solved = uq.Solved
|
2022-07-08 21:11:11 +00:00
|
|
|
qa.Closed = uq.Closed
|
2021-02-05 15:56:27 +00:00
|
|
|
|
|
|
|
_, err = qa.Update()
|
2022-05-16 09:38:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to update in importExerciceQA:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during query updating."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, qa)
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func importQAComment(c *gin.Context) {
|
|
|
|
query := c.MustGet("qa-query").(*fic.QAQuery)
|
|
|
|
|
2021-02-05 15:56:27 +00:00
|
|
|
// Create a new query
|
|
|
|
var uc fic.QAComment
|
2022-05-16 09:38:46 +00:00
|
|
|
err := c.ShouldBindJSON(&uc)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(uc.Content) == 0 {
|
2022-05-16 09:38:46 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Empty comment"})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if qac, err := query.AddComment(uc.Content, uc.IdTeam, uc.User); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to AddComment in importQAComment:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during comment creation."})
|
|
|
|
return
|
2021-02-05 15:56:27 +00:00
|
|
|
} else {
|
|
|
|
qac.Date = uc.Date
|
|
|
|
|
|
|
|
_, err = qac.Update()
|
2022-05-16 09:38:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to Update comment in importQAComment")
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during comment creation."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, qac)
|
2021-02-05 15:56:27 +00:00
|
|
|
}
|
|
|
|
}
|