Use gin-gonic instead of httprouter
This commit is contained in:
parent
7c719d9fd5
commit
a203cdc36a
22 changed files with 1668 additions and 1392 deletions
98
grades.go
98
grades.go
|
@ -1,58 +1,68 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/users/:uid/surveys/:sid/grades", apiAuthHandler(func(uauth *User, ps httprouter.Params, body []byte) HTTPResponse {
|
||||
return surveyAuthHandler(func(s Survey, uauth *User, _ []byte) HTTPResponse {
|
||||
return userHandler(func(u User, _ []byte) HTTPResponse {
|
||||
if uauth != nil && ((s.Shown && u.Id == uauth.Id) || uauth.IsAdmin) {
|
||||
if score, err := s.GetUserGrades(&u); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if score == nil {
|
||||
return APIResponse{"N/A"}
|
||||
} else {
|
||||
return APIResponse{score}
|
||||
}
|
||||
} else {
|
||||
return APIErrorResponse{
|
||||
status: http.StatusForbidden,
|
||||
err: errors.New("Not accessible"),
|
||||
}
|
||||
func declareAPIAuthGradesRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/grades", func(c *gin.Context) {
|
||||
uauth := c.MustGet("LoggedUser").(*User)
|
||||
|
||||
if survey, ok := c.Get("survey"); !ok {
|
||||
if uauth == nil || !uauth.IsAdmin {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"errmsg": "Not authorized"})
|
||||
return
|
||||
}
|
||||
|
||||
grades, err := GetAllGrades()
|
||||
if err != nil {
|
||||
log.Println("Unable to GetAllGrades:", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs when trying to retrieve grades."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, grades)
|
||||
} else {
|
||||
s := survey.(*Survey)
|
||||
|
||||
if user, ok := c.Get("user"); ok {
|
||||
u := user.(*User)
|
||||
|
||||
if uauth == nil || !((s.Shown && u.Id == uauth.Id) || uauth.IsAdmin) {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"errmsg": "Not accessible"})
|
||||
return
|
||||
}
|
||||
})(ps, body)
|
||||
})(uauth, ps, body)
|
||||
}, loggedUser))
|
||||
router.GET("/api/surveys/:sid/grades", apiAuthHandler(surveyAuthHandler(func(s Survey, uauth *User, _ []byte) HTTPResponse {
|
||||
if scores, err := s.GetGrades(); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if scores == nil {
|
||||
return APIResponse{"N/A"}
|
||||
} else {
|
||||
return APIResponse{scores}
|
||||
}
|
||||
}), adminRestricted))
|
||||
router.GET("/api/grades", apiAuthHandler(func(uauth *User, ps httprouter.Params, body []byte) HTTPResponse {
|
||||
if uauth != nil && uauth.IsAdmin {
|
||||
if score, err := GetAllGrades(); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if score == nil {
|
||||
return APIResponse{"N/A"}
|
||||
|
||||
score, err := s.GetUserGrades(u)
|
||||
if err != nil {
|
||||
log.Printf("Unable to GetUserGrades(sid=%d; uid=%d): %s", s.Id, u.Id, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrive user grade. Please try again later."})
|
||||
return
|
||||
}
|
||||
|
||||
if score == nil {
|
||||
c.JSON(http.StatusOK, "N/A")
|
||||
} else {
|
||||
c.JSON(http.StatusOK, score)
|
||||
}
|
||||
} else if uauth.IsAdmin {
|
||||
scores, err := s.GetGrades()
|
||||
if err != nil {
|
||||
log.Printf("Unable to GetGrades(sid=%d): %s", s.Id, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrive grades."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, scores)
|
||||
} else {
|
||||
return APIResponse{score}
|
||||
}
|
||||
} else {
|
||||
return APIErrorResponse{
|
||||
status: http.StatusForbidden,
|
||||
err: errors.New("Not accessible"),
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"errmsg": "Not authorized"})
|
||||
return
|
||||
}
|
||||
}
|
||||
}, adminRestricted))
|
||||
})
|
||||
}
|
||||
|
||||
func GetAllGrades() (scores map[int64]map[int64]*float64, err error) {
|
||||
|
|
Reference in a new issue