This commit is contained in:
nemunaire 2020-03-08 01:06:44 +01:00
commit 0a79763f69
17 changed files with 459 additions and 158 deletions

View file

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"errors"
"net/http"
"strconv"
@ -12,12 +13,19 @@ func init() {
router.GET("/api/questions", apiHandler(
func(httprouter.Params, []byte) HTTPResponse {
return formatApiResponse(getQuestions())
}))
router.GET("/api/surveys/:sid/questions", apiHandler(surveyHandler(
func(s Survey, _ []byte) HTTPResponse {
}, adminRestricted))
router.GET("/api/surveys/:sid/questions", apiAuthHandler(surveyAuthHandler(
func(s Survey, u *User, _ []byte) HTTPResponse {
if !s.Shown && !u.IsAdmin {
return APIErrorResponse{err:errors.New("Not accessible")}
}
return formatApiResponse(s.GetQuestions())
})))
router.POST("/api/surveys/:sid/questions", apiHandler(surveyHandler(func(s Survey, body []byte) HTTPResponse {
}), loggedUser))
router.POST("/api/surveys/:sid/questions", apiAuthHandler(surveyAuthHandler(func(s Survey, u *User, body []byte) HTTPResponse {
if !s.Shown && !u.IsAdmin {
return APIErrorResponse{err:errors.New("Not accessible")}
}
var new Question
if err := json.Unmarshal(body, &new); err != nil {
return APIErrorResponse{err:err}
@ -28,11 +36,11 @@ func init() {
router.GET("/api/questions/:qid", apiHandler(questionHandler(
func(s Question, _ []byte) HTTPResponse {
return APIResponse{s}
})))
}), adminRestricted))
router.GET("/api/surveys/:sid/questions/:qid", apiHandler(questionHandler(
func(s Question, _ []byte) HTTPResponse {
return APIResponse{s}
})))
}), adminRestricted))
router.PUT("/api/questions/:qid", apiHandler(questionHandler(func(current Question, body []byte) HTTPResponse {
var new Question
if err := json.Unmarshal(body, &new); err != nil {
@ -41,7 +49,7 @@ func init() {
new.Id = current.Id
return formatApiResponse(new.Update())
})))
}), adminRestricted))
router.PUT("/api/surveys/:sid/questions/:qid", apiHandler(questionHandler(func(current Question, body []byte) HTTPResponse {
var new Question
if err := json.Unmarshal(body, &new); err != nil {
@ -50,15 +58,15 @@ func init() {
new.Id = current.Id
return formatApiResponse(new.Update())
})))
}), adminRestricted))
router.DELETE("/api/questions/:qid", apiHandler(questionHandler(
func(q Question, _ []byte) HTTPResponse {
return formatApiResponse(q.Delete())
})))
}), adminRestricted))
router.DELETE("/api/surveys/:sid/questions/:qid", apiHandler(questionHandler(
func(q Question, _ []byte) HTTPResponse {
return formatApiResponse(q.Delete())
})))
}), adminRestricted))
}
func questionHandler(f func(Question, []byte) HTTPResponse) func(httprouter.Params, []byte) HTTPResponse {