Use markdown to format questions' descriptions

This commit is contained in:
nemunaire 2020-10-31 18:06:39 +01:00
parent 5fd17156fb
commit 42b15a1546
2 changed files with 31 additions and 25 deletions

View File

@ -64,8 +64,8 @@
<h4 class="card-title" ng-if="!question.edit">{{qid + 1}}. {{ question.title }}</h4>
<div class="card-title form-group row" ng-if="question.edit"><label for="q{{qid}}title" class="col-auto col-form-label font-weight-bold">Titre&nbsp;:</label><div class="col"><input id="q{{qid}}title" class="form-control" ng-model="question.title"></div></div>
<p class="card-text" style="white-space: pre-line" ng-bind-html="question.description" ng-if="!question.edit"></p>
<textarea class="form-control mb-2" ng-if="question.edit" ng-model="question.description" placeholder="Description de la question"></textarea>
<p class="card-text" ng-bind-html="question.description" ng-if="!question.edit"></p>
<textarea class="form-control mb-2" ng-if="question.edit" ng-model="question.desc_raw" placeholder="Description de la question"></textarea>
<div class="form-group row" ng-if="question.edit">
<label class="col-2 col-form-label" for="q{{qid}}kind">Type de réponse</label>

View File

@ -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