Exemple #1
0
func GetQuestion(w http.ResponseWriter, r *http.Request, c *server.Context) {
	id := c.PathParams["id"]
	questionId, err := strconv.Atoi(id)
	if err != nil {
		c.Render.NotFound(w)
		return
	}

	question := models.GetQuestionById(c.DB.DB, questionId)
	if question != nil {
		c.Render.ResultOK(w, question)
	} else {
		c.Render.NotFound(w)
	}
}
Exemple #2
0
func CreateAnswer(w http.ResponseWriter, r *http.Request, c *server.Context) {
	userId := c.Session.UserId
	var a AnswerParam
	if c.MustDecodeBody(w, &a) == false {
		return
	}
	questionId := int(a.QuestionId)

	if models.GetQuestionById(c.DB.DB, questionId) == nil {
		c.Render.BadRequest(w, errors.New("Question does not exist"))
		return
	}

	answer, err := models.InsertAnswer(c.DB.DB, userId, questionId, a.Message)
	if err != nil {
		c.Render.BadRequest(w, err)
	} else {
		c.Render.ResultOK(w, answer)
	}
}