示例#1
0
func (m *Mongo) GetTest(c *gin.Context) {
	tokenReceived := c.Query("token")
	id := c.Param("id")

	_, err := utils.JwtAuthenticator(tokenReceived)

	if err != nil {
		utils.ErrorResponse(c, http.StatusForbidden, "Log in again")
		return
	}
	log.Println("thr dod", id)
	var test models.Test

	if bson.IsObjectIdHex(id) {
		test.Id = bson.ObjectIdHex(id)
	} else {
		utils.ErrorResponse(c, http.StatusInternalServerError, "Oh boy not again")
		return
	}

	test, err = test.GetTest(m.Database)

	if err != nil {
		utils.ErrorResponse(c, http.StatusInternalServerError, "Could not get the test")
		return
	}

	/**
	todo get all questions for this test
	should be probably done with go routines for
	improved performance
	*/
	var questions []models.Question
	for _, id := range test.QuestionIds {
		temp_question, err := getQuestion(m.Database, id)

		if err != nil {
			log.Println("Somethin's up ", err)
		} else {
			questions = append(questions, temp_question)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"err":       nil,
		"test":      test,
		"questions": questions,
	})
}