diff --git a/htdocs/views/survey.html b/htdocs/views/survey.html index cc4244c..7ec4d9c 100644 --- a/htdocs/views/survey.html +++ b/htdocs/views/survey.html @@ -64,8 +64,8 @@

{{qid + 1}}. {{ question.title }}

-

- +

+
diff --git a/questions.go b/questions.go index df862ce..ed44aaa 100644 --- a/questions.go +++ b/questions.go @@ -8,6 +8,7 @@ import ( "time" "github.com/julienschmidt/httprouter" + "gopkg.in/russross/blackfriday.v2" ) func init() { @@ -18,24 +19,24 @@ func init() { 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 APIErrorResponse{err: errors.New("Not accessible")} } if s.StartAvailability.After(time.Now()) && !u.IsAdmin { - return APIErrorResponse{status: http.StatusPaymentRequired, err:errors.New("Not available yet")} + return APIErrorResponse{status: http.StatusPaymentRequired, err: errors.New("Not available yet")} } return formatApiResponse(s.GetQuestions()) }), 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")} + return APIErrorResponse{err: errors.New("Not accessible")} } var new Question if err := json.Unmarshal(body, &new); err != nil { - return APIErrorResponse{err:err} + return APIErrorResponse{err: err} } - return formatApiResponse(s.NewQuestion(new.Title, new.Description, new.Placeholder, new.Kind)) + return formatApiResponse(s.NewQuestion(new.Title, new.DescriptionRaw, new.Placeholder, new.Kind)) }), adminRestricted)) router.GET("/api/questions/:qid", apiHandler(questionHandler( func(s Question, _ []byte) HTTPResponse { @@ -48,7 +49,7 @@ func init() { router.PUT("/api/questions/:qid", apiHandler(questionHandler(func(current Question, body []byte) HTTPResponse { var new Question if err := json.Unmarshal(body, &new); err != nil { - return APIErrorResponse{err:err} + return APIErrorResponse{err: err} } new.Id = current.Id @@ -57,7 +58,7 @@ func init() { 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 { - return APIErrorResponse{err:err} + return APIErrorResponse{err: err} } new.Id = current.Id @@ -84,16 +85,16 @@ func questionHandler(f func(Question, []byte) HTTPResponse) func(httprouter.Para } if qid, err := strconv.Atoi(string(ps.ByName("qid"))); err != nil { - return APIErrorResponse{err:err} + return APIErrorResponse{err: err} } else if survey == nil { if question, err := getQuestion(qid); err != nil { - return APIErrorResponse{err:err} + return APIErrorResponse{err: err} } else { return f(question, body) } } else { if question, err := survey.GetQuestion(qid); err != nil { - return APIErrorResponse{err:err} + return APIErrorResponse{err: err} } else { return f(question, body) } @@ -109,7 +110,7 @@ func questionAuthHandler(f func(Question, *User, []byte) HTTPResponse, access .. if err := a(u, &q); err != nil { return APIErrorResponse{ status: http.StatusForbidden, - err: err, + err: err, } } } @@ -120,12 +121,13 @@ func questionAuthHandler(f func(Question, *User, []byte) HTTPResponse, access .. } type Question struct { - Id int64 `json:"id"` - IdSurvey int64 `json:"id_survey"` - Title string `json:"title"` - Description string `json:"description"` - Placeholder string `json:"placeholder,omitempty"` - Kind string `json:"kind"` + Id int64 `json:"id"` + IdSurvey int64 `json:"id_survey"` + Title string `json:"title"` + Description string `json:"description"` + DescriptionRaw string `json:"desc_raw,omitempty"` + Placeholder string `json:"placeholder,omitempty"` + Kind string `json:"kind"` } func getQuestions() (questions []Question, err error) { @@ -136,9 +138,10 @@ func getQuestions() (questions []Question, err error) { for rows.Next() { var q Question - if err = rows.Scan(&q.Id, &q.IdSurvey, &q.Title, &q.Description, &q.Placeholder, &q.Kind); err != nil { + if err = rows.Scan(&q.Id, &q.IdSurvey, &q.Title, &q.DescriptionRaw, &q.Placeholder, &q.Kind); err != nil { return } + q.Description = string(blackfriday.Run([]byte(q.DescriptionRaw))) questions = append(questions, q) } if err = rows.Err(); err != nil { @@ -157,9 +160,10 @@ func (s *Survey) GetQuestions() (questions []Question, err error) { for rows.Next() { var q Question - if err = rows.Scan(&q.Id, &q.IdSurvey, &q.Title, &q.Description, &q.Placeholder, &q.Kind); err != nil { + if err = rows.Scan(&q.Id, &q.IdSurvey, &q.Title, &q.DescriptionRaw, &q.Placeholder, &q.Kind); err != nil { return } + q.Description = string(blackfriday.Run([]byte(q.DescriptionRaw))) questions = append(questions, q) } if err = rows.Err(); err != nil { @@ -171,12 +175,14 @@ func (s *Survey) GetQuestions() (questions []Question, err error) { } func getQuestion(id int) (q Question, err error) { - err = DBQueryRow("SELECT id_question, id_survey, title, description, placeholder, kind FROM survey_quests WHERE id_question=?", id).Scan(&q.Id, &q.IdSurvey, &q.Title, &q.Description, &q.Placeholder, &q.Kind) + err = DBQueryRow("SELECT id_question, id_survey, title, description, placeholder, kind FROM survey_quests WHERE id_question=?", id).Scan(&q.Id, &q.IdSurvey, &q.Title, &q.DescriptionRaw, &q.Placeholder, &q.Kind) + q.Description = string(blackfriday.Run([]byte(q.DescriptionRaw))) return } func (s *Survey) GetQuestion(id int) (q Question, err error) { - err = DBQueryRow("SELECT id_question, id_survey, title, description, placeholder, kind FROM survey_quests WHERE id_question=? AND id_survey=?", id, s.Id).Scan(&q.Id, &q.IdSurvey, &q.Title, &q.Description, &q.Placeholder, &q.Kind) + err = DBQueryRow("SELECT id_question, id_survey, title, description, placeholder, kind FROM survey_quests WHERE id_question=? AND id_survey=?", id, s.Id).Scan(&q.Id, &q.IdSurvey, &q.Title, &q.DescriptionRaw, &q.Placeholder, &q.Kind) + q.Description = string(blackfriday.Run([]byte(q.DescriptionRaw))) return } @@ -186,7 +192,7 @@ func (s *Survey) NewQuestion(title string, description string, placeholder strin } else if qid, err := res.LastInsertId(); err != nil { return Question{}, err } else { - return Question{qid, s.Id, title, description, placeholder, kind}, nil + return Question{qid, s.Id, title, string(blackfriday.Run([]byte(description))), description, placeholder, kind}, nil } } @@ -195,7 +201,7 @@ func (q Question) GetSurvey() (Survey, error) { } func (q Question) Update() (Question, error) { - if _, err := DBExec("UPDATE survey_quests SET id_survey = ?, title = ?, description = ?, placeholder = ?, kind = ? WHERE id_question = ?", q.IdSurvey, q.Title, q.Description, q.Placeholder, q.Kind, q.Id); err != nil { + if _, err := DBExec("UPDATE survey_quests SET id_survey = ?, title = ?, description = ?, placeholder = ?, kind = ? WHERE id_question = ?", q.IdSurvey, q.Title, q.DescriptionRaw, q.Placeholder, q.Kind, q.Id); err != nil { return Question{}, err } else { return q, err