admin: Use gin-gonic as router
This commit is contained in:
parent
83468ad723
commit
8b3fbdb64a
32 changed files with 2785 additions and 1635 deletions
|
@ -1,84 +1,118 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.POST("/api/qa/", apiHandler(importExerciceQA))
|
||||
router.POST("/api/qa/:qid/comments", apiHandler(qaHandler(importQAComment)))
|
||||
func declareQARoutes(router *gin.RouterGroup) {
|
||||
router.POST("/qa/", importExerciceQA)
|
||||
|
||||
apiQARoutes := router.Group("/qa/:qid")
|
||||
apiQARoutes.POST("/comments", importQAComment)
|
||||
}
|
||||
|
||||
func qaHandler(f func(*fic.QAQuery, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
|
||||
return func(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if qid, err := strconv.ParseInt(string(ps.ByName("qid")), 10, 64); err != nil {
|
||||
return nil, err
|
||||
} else if query, err := fic.GetQAQuery(qid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(query, body)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
c.Set("qa-query", qa)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
||||
func importExerciceQA(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
func importExerciceQA(c *gin.Context) {
|
||||
// Create a new query
|
||||
var uq fic.QAQuery
|
||||
if err := json.Unmarshal(body, &uq); err != nil {
|
||||
return nil, err
|
||||
err := c.ShouldBindJSON(&uq)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var exercice *fic.Exercice
|
||||
var err error
|
||||
if uq.IdExercice == 0 {
|
||||
return nil, errors.New("id_exercice not filled")
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "id_exercice not filled"})
|
||||
return
|
||||
} else if exercice, err = fic.GetExercice(uq.IdExercice); err != nil {
|
||||
return nil, err
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Unable to find requested exercice"})
|
||||
return
|
||||
}
|
||||
|
||||
if len(uq.State) == 0 {
|
||||
return nil, errors.New("State not filled")
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "State not filled"})
|
||||
return
|
||||
}
|
||||
|
||||
if len(uq.Subject) == 0 {
|
||||
return nil, errors.New("Subject not filled")
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Subject not filled"})
|
||||
return
|
||||
}
|
||||
|
||||
if qa, err := exercice.NewQAQuery(uq.Subject, uq.IdTeam, uq.User, uq.State); err != nil {
|
||||
return nil, err
|
||||
log.Println("Unable to importExerciceQA:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during query creation."})
|
||||
return
|
||||
} else {
|
||||
qa.Creation = uq.Creation
|
||||
qa.Solved = uq.Solved
|
||||
qa.Closed = qa.Closed
|
||||
|
||||
_, err = qa.Update()
|
||||
return qa, err
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func importQAComment(query *fic.QAQuery, body []byte) (interface{}, error) {
|
||||
func importQAComment(c *gin.Context) {
|
||||
query := c.MustGet("qa-query").(*fic.QAQuery)
|
||||
|
||||
// Create a new query
|
||||
var uc fic.QAComment
|
||||
if err := json.Unmarshal(body, &uc); err != nil {
|
||||
return nil, err
|
||||
err := c.ShouldBindJSON(&uc)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if len(uc.Content) == 0 {
|
||||
return nil, errors.New("Empty comment")
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Empty comment"})
|
||||
return
|
||||
}
|
||||
|
||||
if qac, err := query.AddComment(uc.Content, uc.IdTeam, uc.User); err != nil {
|
||||
return nil, err
|
||||
log.Println("Unable to AddComment in importQAComment:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during comment creation."})
|
||||
return
|
||||
} else {
|
||||
qac.Date = uc.Date
|
||||
|
||||
_, err = qac.Update()
|
||||
return qac, err
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue