예제 #1
0
func (c ApiV1Questions) Get(attendId int, num int) revel.Result {
	var attend models.Attend
	var contest models.Contest
	var question models.Question

	controllers.DB.Where("id = ?", attendId).First(&attend)
	controllers.DB.Model(&attend).Related(&contest)
	n := contest.GetQuestionId(num)

	if err := controllers.DB.First(&question, n).Error; err != nil {
		return c.RenderJson(ErrorResponse{Code: 404, Message: "Invalid id"})
	}
	q := Quiz{Id: question.ID, Text: question.Content}
	choices := []string{question.Select1, question.Select2, question.Select3, question.Select4}
	q.Choices[0] = randPopChoices(&choices)
	q.Choices[1] = randPopChoices(&choices)
	q.Choices[2] = randPopChoices(&choices)
	q.Choices[3] = randPopChoices(&choices)
	return c.RenderJson(q)
}
예제 #2
0
func (c ApiV1Questions) Answer(attendId int, num int, answer string) revel.Result {
	var attend models.Attend
	var contest models.Contest
	var question models.Question

	controllers.DB.Where("id = ?", attendId).First(&attend)
	controllers.DB.Model(&attend).Related(&contest)
	n := contest.GetQuestionId(num)

	if err := controllers.DB.First(&question, n).Error; err != nil {
		return c.RenderJson(ErrorResponse{Code: 404, Message: "Invalid id"})
	}

	var r Response
	if answer == question.Answer {
		attend.SetScore(num, 10)
		r = Response{"correct"}
	} else {
		attend.SetScore(num, 0)
		r = Response{"wrong"}
	}
	return c.RenderJson(r)
}