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

@ -13,7 +13,7 @@ import (
func init() {
router.GET("/api/surveys", apiAuthHandler(
func(u *User, _ httprouter.Params, _ []byte) HTTPResponse {
if u.IsAdmin {
if u != nil && u.IsAdmin {
return formatApiResponse(getSurveys(""))
} else {
return formatApiResponse(getSurveys("WHERE shown = TRUE"))
@ -29,7 +29,7 @@ func init() {
}, adminRestricted))
router.GET("/api/surveys/:sid", apiAuthHandler(surveyAuthHandler(
func(s Survey, u *User, _ []byte) HTTPResponse {
if s.Shown || u.IsAdmin {
if s.Shown || (u != nil && u.IsAdmin) {
return APIResponse{s}
} else {
return APIErrorResponse{
@ -51,6 +51,17 @@ func init() {
func(s Survey, _ []byte) HTTPResponse {
return formatApiResponse(s.Delete())
}), adminRestricted))
router.GET("/api/surveys/:sid/score", apiAuthHandler(surveyAuthHandler(
func(s Survey, u *User, _ []byte) HTTPResponse {
if s.Shown || (u != nil && u.IsAdmin) {
return formatApiResponse(s.GetScore(u))
} else {
return APIErrorResponse{
status: http.StatusForbidden,
err: errors.New("Not accessible"),
}
}
}), loggedUser))
}
func surveyHandler(f func(Survey, []byte) HTTPResponse) func(httprouter.Params, []byte) HTTPResponse {
@ -81,19 +92,20 @@ type Survey struct {
Id int64 `json:"id"`
Title string `json:"title"`
Shown bool `json:"shown"`
Corrected bool `json:"corrected"`
StartAvailability time.Time `json:"start_availability"`
EndAvailability time.Time `json:"end_availability"`
}
func getSurveys(cnd string, param... interface{}) (surveys []Survey, err error) {
if rows, errr := DBQuery("SELECT id_survey, title, shown, start_availability, end_availability FROM surveys " + cnd, param...); errr != nil {
if rows, errr := DBQuery("SELECT id_survey, title, shown, corrected, start_availability, end_availability FROM surveys " + cnd, param...); errr != nil {
return nil, errr
} else {
defer rows.Close()
for rows.Next() {
var s Survey
if err = rows.Scan(&s.Id, &s.Title, &s.Shown, &s.StartAvailability, &s.EndAvailability); err != nil {
if err = rows.Scan(&s.Id, &s.Title, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability); err != nil {
return
}
surveys = append(surveys, s)
@ -107,7 +119,7 @@ func getSurveys(cnd string, param... interface{}) (surveys []Survey, err error)
}
func getSurvey(id int) (s Survey, err error) {
err = DBQueryRow("SELECT id_survey, title, shown, start_availability, end_availability FROM surveys WHERE id_survey=?", id).Scan(&s.Id, &s.Title, &s.Shown, &s.StartAvailability, &s.EndAvailability)
err = DBQueryRow("SELECT id_survey, title, shown, corrected, start_availability, end_availability FROM surveys WHERE id_survey=?", id).Scan(&s.Id, &s.Title, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability)
return
}
@ -117,12 +129,17 @@ func NewSurvey(title string, shown bool, startAvailability time.Time, endAvailab
} else if sid, err := res.LastInsertId(); err != nil {
return Survey{}, err
} else {
return Survey{sid, title, shown, startAvailability, endAvailability}, nil
return Survey{sid, title, shown, false, startAvailability, endAvailability}, nil
}
}
func (s Survey) GetScore(u *User) (score int64, err error) {
err = DBQueryRow("SELECT SUM(M.score) FROM (SELECT MAX(score) FROM survey_responses R INNER JOIN survey_quests Q ON Q.id_question = R.id_question WHERE Q.id_survey=? AND R.id_user=? GROUP BY id_question) M", s.Id, u.Id).Scan(&score)
return
}
func (s Survey) Update() (int64, error) {
if res, err := DBExec("UPDATE surveys SET title = ?, shown = ?, start_availability = ?, end_availability = ? WHERE id_survey = ?", s.Title, s.Shown, s.StartAvailability, s.EndAvailability, s.Id); err != nil {
if res, err := DBExec("UPDATE surveys SET title = ?, shown = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_survey = ?", s.Title, s.Shown, s.Corrected, s.StartAvailability, s.EndAvailability, s.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err