Use gin-gonic instead of httprouter
This commit is contained in:
parent
7c719d9fd5
commit
a203cdc36a
22 changed files with 1631 additions and 1355 deletions
306
corrections.go
306
corrections.go
|
|
@ -1,160 +1,173 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/surveys/:sid/questions/:qid/corrections", apiHandler(questionHandler(
|
||||
func(q Question, _ []byte) HTTPResponse {
|
||||
if cts, err := q.GetCorrectionTemplates(); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return APIResponse{cts}
|
||||
}
|
||||
}), adminRestricted))
|
||||
router.POST("/api/surveys/:sid/questions/:qid/corrections", apiHandler(questionHandler(func(q Question, body []byte) HTTPResponse {
|
||||
var new CorrectionTemplate
|
||||
if err := json.Unmarshal(body, &new); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
func declareAPIAdminCorrectionsRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/corrections", func(c *gin.Context) {
|
||||
q := c.MustGet("question").(*Question)
|
||||
|
||||
cts, err := q.GetCorrectionTemplates()
|
||||
if err != nil {
|
||||
log.Println("Unable to GetCorrectionTemplates:", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs when trying to retrive correction's templates"})
|
||||
return
|
||||
}
|
||||
|
||||
return formatApiResponse(q.NewCorrectionTemplate(new.Label, new.RegExp, new.Score, new.ScoreExplaination))
|
||||
}), adminRestricted))
|
||||
c.JSON(http.StatusOK, cts)
|
||||
})
|
||||
router.POST("/corrections", func(c *gin.Context) {
|
||||
q := c.MustGet("question").(*Question)
|
||||
|
||||
router.GET("/api/surveys/:sid/questions/:qid/corrections/:cid", apiHandler(correctionHandler(
|
||||
func(ct CorrectionTemplate, _ []byte) HTTPResponse {
|
||||
if users, err := ct.GetUserCorrected(); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return APIResponse{users}
|
||||
}
|
||||
}), adminRestricted))
|
||||
router.PUT("/api/surveys/:sid/questions/:qid/corrections/:cid", apiHandler(correctionHandler(func(current CorrectionTemplate, body []byte) HTTPResponse {
|
||||
var new CorrectionTemplate
|
||||
if err := json.Unmarshal(body, &new); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
if err := c.ShouldBindJSON(&new); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ct, err := q.NewCorrectionTemplate(new.Label, new.RegExp, new.Score, new.ScoreExplaination)
|
||||
if err != nil {
|
||||
log.Println("Unable to NewCorrectionTemplate:", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs when trying to insert new correction template."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, ct)
|
||||
})
|
||||
|
||||
correctionsRoutes := router.Group("/corrections/:cid")
|
||||
correctionsRoutes.Use(correctionHandler)
|
||||
|
||||
correctionsRoutes.GET("", func(c *gin.Context) {
|
||||
ct := c.MustGet("correctiontemplate").(*CorrectionTemplate)
|
||||
|
||||
users, err := ct.GetUserCorrected()
|
||||
if err != nil {
|
||||
log.Println("Unable to GetUserCorrected:", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs when trying to retrieve users' corrections"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, users)
|
||||
})
|
||||
correctionsRoutes.PUT("", func(c *gin.Context) {
|
||||
current := c.MustGet("correctiontemplate").(*CorrectionTemplate)
|
||||
|
||||
var new CorrectionTemplate
|
||||
if err := c.ShouldBindJSON(&new); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
new.Id = current.Id
|
||||
|
||||
if err := new.Update(); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return APIResponse{new}
|
||||
}
|
||||
}), adminRestricted))
|
||||
router.DELETE("/api/surveys/:sid/questions/:qid/corrections/:cid", apiHandler(correctionHandler(func(ct CorrectionTemplate, body []byte) HTTPResponse {
|
||||
return formatApiResponse(ct.Delete())
|
||||
}), adminRestricted))
|
||||
|
||||
router.GET("/api/questions/:qid/corrections", apiHandler(questionHandler(
|
||||
func(q Question, _ []byte) HTTPResponse {
|
||||
if cts, err := q.GetCorrectionTemplates(); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return APIResponse{cts}
|
||||
}
|
||||
}), adminRestricted))
|
||||
router.POST("/api/questions/:qid/corrections", apiHandler(questionHandler(func(q Question, body []byte) HTTPResponse {
|
||||
var new CorrectionTemplate
|
||||
if err := json.Unmarshal(body, &new); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
log.Println("Unable to Update correctionTemplate:", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs when trying to update the correction template"})
|
||||
return
|
||||
}
|
||||
|
||||
return formatApiResponse(q.NewCorrectionTemplate(new.Label, new.RegExp, new.Score, new.ScoreExplaination))
|
||||
}), adminRestricted))
|
||||
c.JSON(http.StatusOK, new)
|
||||
})
|
||||
correctionsRoutes.DELETE("", func(c *gin.Context) {
|
||||
ct := c.MustGet("correctiontemplate").(*CorrectionTemplate)
|
||||
|
||||
router.GET("/api/questions/:qid/corrections/:cid", apiHandler(correctionHandler(
|
||||
func(ct CorrectionTemplate, _ []byte) HTTPResponse {
|
||||
if users, err := ct.GetUserCorrected(); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return APIResponse{users}
|
||||
}
|
||||
}), adminRestricted))
|
||||
router.PUT("/api/questions/:qid/corrections/:cid", apiHandler(correctionHandler(func(current CorrectionTemplate, body []byte) HTTPResponse {
|
||||
var new CorrectionTemplate
|
||||
if err := json.Unmarshal(body, &new); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
if _, err := ct.Delete(); err != nil {
|
||||
log.Println("Unable to Delete correctionTemplate:", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs when trying to delete the correction template."})
|
||||
return
|
||||
}
|
||||
|
||||
new.Id = current.Id
|
||||
if err := new.Update(); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return APIResponse{new}
|
||||
c.JSON(http.StatusOK, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func declareAPIAdminUserCorrectionsRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/corrections", func(c *gin.Context) {
|
||||
user := c.MustGet("user").(*User)
|
||||
|
||||
corrections, err := user.GetCorrections()
|
||||
if err != nil {
|
||||
log.Printf("Unable to GetCorrections(uid=%d): %s", user.Id, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieve corrections."})
|
||||
return
|
||||
}
|
||||
}), adminRestricted))
|
||||
router.DELETE("/api/questions/:qid/corrections/:cid", apiHandler(correctionHandler(func(ct CorrectionTemplate, body []byte) HTTPResponse {
|
||||
return formatApiResponse(ct.Delete())
|
||||
}), adminRestricted))
|
||||
|
||||
router.GET("/api/users/:uid/questions/:qid", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse {
|
||||
return userHandler(func(u User, _ []byte) HTTPResponse {
|
||||
if qid, err := strconv.Atoi(string(ps.ByName("qid"))); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if question, err := getQuestion(qid); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return formatApiResponse(question.ComputeScoreQuestion(&u))
|
||||
}
|
||||
})(ps, body)
|
||||
}, adminRestricted))
|
||||
c.JSON(http.StatusOK, corrections)
|
||||
})
|
||||
router.POST("/corrections", func(c *gin.Context) {
|
||||
user := c.MustGet("user").(*User)
|
||||
|
||||
router.GET("/api/users/:uid/corrections", apiHandler(userHandler(
|
||||
func(u User, _ []byte) HTTPResponse {
|
||||
return formatApiResponse(u.GetCorrections())
|
||||
}), adminRestricted))
|
||||
router.POST("/api/users/:uid/corrections", apiHandler(userHandler(func(u User, body []byte) HTTPResponse {
|
||||
var new UserCorrection
|
||||
if err := json.Unmarshal(body, &new); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
if err := c.ShouldBindJSON(&new); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
return formatApiResponse(u.NewCorrection(new.IdTemplate))
|
||||
}), adminRestricted))
|
||||
router.PUT("/api/users/:uid/corrections", apiHandler(userHandler(func(u User, body []byte) HTTPResponse {
|
||||
correction, err := user.NewCorrection(new.IdTemplate)
|
||||
if err != nil {
|
||||
log.Printf("Unable to NewCorrection(uid=%d): %s", user.Id, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to insert the new correction."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, correction)
|
||||
})
|
||||
router.PUT("/corrections", func(c *gin.Context) {
|
||||
user := c.MustGet("user").(*User)
|
||||
|
||||
var new map[int64]bool
|
||||
if err := json.Unmarshal(body, &new); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
if err := c.ShouldBindJSON(&new); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
return formatApiResponse(u.EraseCorrections(new))
|
||||
}), adminRestricted))
|
||||
router.DELETE("/api/users/:uid/corrections/:cid", apiHandler(userCorrectionHandler(func(u User, uc UserCorrection, body []byte) HTTPResponse {
|
||||
return formatApiResponse(uc.Delete(u))
|
||||
}), adminRestricted))
|
||||
correction, err := user.EraseCorrections(new)
|
||||
if err != nil {
|
||||
log.Printf("Unable to EraseCorrections(uid=%d): %s", user.Id, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to erase the correction."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, correction)
|
||||
})
|
||||
|
||||
correctionsRoutes := router.Group("/corrections/:cid")
|
||||
correctionsRoutes.Use(userCorrectionHandler)
|
||||
|
||||
correctionsRoutes.DELETE("", func(c *gin.Context) {
|
||||
user := c.MustGet("user").(*User)
|
||||
uc := c.MustGet("correction").(*UserCorrection)
|
||||
|
||||
if _, err := uc.Delete(user); err != nil {
|
||||
log.Printf("Unable to Delete(uid=%d, cid=%d) user correction: %s", user.Id, uc.Id, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to delete this correction."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func correctionHandler(f func(CorrectionTemplate, []byte) HTTPResponse) func(httprouter.Params, []byte) HTTPResponse {
|
||||
return func(ps httprouter.Params, body []byte) HTTPResponse {
|
||||
return questionHandler(func(q Question, body []byte) HTTPResponse {
|
||||
if cid, err := strconv.Atoi(string(ps.ByName("cid"))); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if correction, err := q.GetCorrectionTemplate(cid); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return f(correction, body)
|
||||
}
|
||||
})(ps, body)
|
||||
}
|
||||
}
|
||||
func correctionHandler(c *gin.Context) {
|
||||
q := c.MustGet("question").(*Question)
|
||||
|
||||
func userCorrectionHandler(f func(User, UserCorrection, []byte) HTTPResponse) func(httprouter.Params, []byte) HTTPResponse {
|
||||
return func(ps httprouter.Params, body []byte) HTTPResponse {
|
||||
return userHandler(func(u User, body []byte) HTTPResponse {
|
||||
if cid, err := strconv.Atoi(string(ps.ByName("cid"))); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if correction, err := u.GetCorrection(cid); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return f(u, correction, body)
|
||||
}
|
||||
})(ps, body)
|
||||
if cid, err := strconv.Atoi(string(c.Param("cid"))); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Invalid correction id"})
|
||||
return
|
||||
} else if correction, err := q.GetCorrectionTemplate(cid); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Correction not found"})
|
||||
return
|
||||
} else {
|
||||
c.Set("correctiontemplate", correction)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -167,7 +180,7 @@ type CorrectionTemplate struct {
|
|||
ScoreExplaination string `json:"score_explaination,omitempty"`
|
||||
}
|
||||
|
||||
func (q *Question) GetCorrectionTemplates() (ct []CorrectionTemplate, err error) {
|
||||
func (q *Question) GetCorrectionTemplates() (ct []*CorrectionTemplate, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_template, id_question, label, re, score, score_explanation FROM correction_templates WHERE id_question=?", q.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
|
|
@ -178,7 +191,7 @@ func (q *Question) GetCorrectionTemplates() (ct []CorrectionTemplate, err error)
|
|||
if err = rows.Scan(&c.Id, &c.IdQuestion, &c.Label, &c.RegExp, &c.Score, &c.ScoreExplaination); err != nil {
|
||||
return
|
||||
}
|
||||
ct = append(ct, c)
|
||||
ct = append(ct, &c)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return
|
||||
|
|
@ -188,23 +201,25 @@ func (q *Question) GetCorrectionTemplates() (ct []CorrectionTemplate, err error)
|
|||
}
|
||||
}
|
||||
|
||||
func (q *Question) GetCorrectionTemplate(id int) (c CorrectionTemplate, err error) {
|
||||
func (q *Question) GetCorrectionTemplate(id int) (c *CorrectionTemplate, err error) {
|
||||
c = new(CorrectionTemplate)
|
||||
err = DBQueryRow("SELECT id_template, id_question, label, re, score, score_explanation FROM correction_templates WHERE id_question=? AND id_template=?", q.Id, id).Scan(&c.Id, &c.IdQuestion, &c.Label, &c.RegExp, &c.Score, &c.ScoreExplaination)
|
||||
return
|
||||
}
|
||||
|
||||
func GetCorrectionTemplate(id int64) (c CorrectionTemplate, err error) {
|
||||
func GetCorrectionTemplate(id int64) (c *CorrectionTemplate, err error) {
|
||||
c = new(CorrectionTemplate)
|
||||
err = DBQueryRow("SELECT id_template, id_question, label, re, score, score_explanation FROM correction_templates WHERE id_template=?", id).Scan(&c.Id, &c.IdQuestion, &c.Label, &c.RegExp, &c.Score, &c.ScoreExplaination)
|
||||
return
|
||||
}
|
||||
|
||||
func (q *Question) NewCorrectionTemplate(label string, regexp string, score int, score_explaination string) (CorrectionTemplate, error) {
|
||||
func (q *Question) NewCorrectionTemplate(label string, regexp string, score int, score_explaination string) (*CorrectionTemplate, error) {
|
||||
if res, err := DBExec("INSERT INTO correction_templates (id_question, label, re, score, score_explanation) VALUES (?, ?, ?, ?, ?)", q.Id, label, regexp, score, score_explaination); err != nil {
|
||||
return CorrectionTemplate{}, err
|
||||
return nil, err
|
||||
} else if cid, err := res.LastInsertId(); err != nil {
|
||||
return CorrectionTemplate{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return CorrectionTemplate{cid, q.Id, label, regexp, score, score_explaination}, nil
|
||||
return &CorrectionTemplate{cid, q.Id, label, regexp, score, score_explaination}, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -223,7 +238,7 @@ func (t *CorrectionTemplate) Delete() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (t *CorrectionTemplate) GetUserCorrected() (ucs []UserCorrection, err error) {
|
||||
func (t *CorrectionTemplate) GetUserCorrected() (ucs []*UserCorrection, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_correction, id_user, id_template FROM student_corrected WHERE id_template=?", t.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
|
|
@ -234,7 +249,7 @@ func (t *CorrectionTemplate) GetUserCorrected() (ucs []UserCorrection, err error
|
|||
if err = rows.Scan(&c.Id, &c.IdUser, &c.IdTemplate); err != nil {
|
||||
return
|
||||
}
|
||||
ucs = append(ucs, c)
|
||||
ucs = append(ucs, &c)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return
|
||||
|
|
@ -260,6 +275,22 @@ type UserCorrection struct {
|
|||
IdTemplate int64 `json:"id_template"`
|
||||
}
|
||||
|
||||
func userCorrectionHandler(c *gin.Context) {
|
||||
u := c.MustGet("user").(*User)
|
||||
|
||||
if cid, err := strconv.Atoi(string(c.Param("cid"))); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Invalid correction id"})
|
||||
return
|
||||
} else if correction, err := u.GetCorrection(cid); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Correction not found"})
|
||||
return
|
||||
} else {
|
||||
c.Set("correction", correction)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func (u *User) GetCorrections() (uc []UserCorrection, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_correction, id_template FROM student_corrected WHERE id_user=?", u.Id); errr != nil {
|
||||
return nil, errr
|
||||
|
|
@ -302,7 +333,8 @@ func (u *User) GetCorrectionsTemplate() (tpls []int64, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (u *User) GetCorrection(id int) (c UserCorrection, err error) {
|
||||
func (u *User) GetCorrection(id int) (c *UserCorrection, err error) {
|
||||
c = new(UserCorrection)
|
||||
err = DBQueryRow("SELECT id_correction, id_template FROM student_corrected WHERE id_user=? AND id_correction=?", u.Id, id).Scan(&c.Id, &c.IdTemplate)
|
||||
return
|
||||
}
|
||||
|
|
@ -340,7 +372,7 @@ func (u *User) EraseCorrections(ids map[int64]bool) (*UserCorrectionSummary, err
|
|||
}
|
||||
}
|
||||
|
||||
func (c *UserCorrection) Delete(u User) (*UserCorrectionSummary, error) {
|
||||
func (c *UserCorrection) Delete(u *User) (*UserCorrectionSummary, error) {
|
||||
if res, err := DBExec("DELETE FROM student_corrected WHERE id_correction = ?", c.Id); err != nil {
|
||||
return nil, err
|
||||
} else if _, err := res.RowsAffected(); err != nil {
|
||||
|
|
@ -374,7 +406,7 @@ func (q *Question) ComputeScoreQuestion(u *User) (*UserCorrectionSummary, error)
|
|||
} else if corrections, err := u.GetCorrectionsTemplate(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
tpls := map[int64]CorrectionTemplate{}
|
||||
tpls := map[int64]*CorrectionTemplate{}
|
||||
for _, tpl := range templates {
|
||||
tpls[tpl.Id] = tpl
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue