Esempio n. 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,
	})
}
Esempio n. 2
0
func (mongo *Mongo) AddSubject(c *gin.Context) {
	var tagInput struct {
		Name  string `json:"name"`
		Token string `json:"token"`
	}

	err := c.BindJSON(&tagInput)

	var subject models.Subject
	subject.Name = tagInput.Name

	log.Println("Why u do this", tagInput.Name)

	if err != nil {
		log.Println("Add tags err", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"err": true,
			"msg": "Error Parsing",
		})
		return
	}

	token, err1 := utils.JwtAuthenticator(tagInput.Token)

	if err1 != nil || !token.Valid {
		c.JSON(http.StatusInternalServerError, gin.H{
			"err": true,
			"msg": "Login Again",
		})
		return
	}

	err = subject.Add(mongo.Database)

	if err != nil {
		log.Println("Add tags db err", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"err": true,
			"msg": "Error Inserting",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"err": false,
		"msg": "Tag Added",
	})

}
Esempio n. 3
0
func (m *Mongo) GetExams(c *gin.Context) {
	tokenReceived := c.Query("token")
	token, err := utils.JwtAuthenticator(tokenReceived)

	println("ofijwejio")
	if err != nil {
		log.Println("kauwa", err)
		utils.ErrorResponse(c, http.StatusForbidden, "Log IN again")
		return
	}

	username, ok := token.Claims["username"].(string)

	if !ok {
		log.Println("kauwa", ok)
		utils.ErrorResponse(c, http.StatusForbidden, "log in afain")
		return
	}

	var s models.Student
	s.Username = username
	groups, err := s.GetGroup(m.Database)

	if err != nil {
		log.Println("kauwa", err)
		utils.ErrorResponse(c, http.StatusInternalServerError, "Its the fuckif se")
		return
	}

	var tests []models.Test
	for _, group := range groups {
		t, err := models.GetEnabledByGroup(m.Database, group)

		if err == nil {
			for _, value := range t {
				tests = append(tests, value)
			}

		}
	}

	c.JSON(http.StatusOK, gin.H{
		"tests": tests,
	})

}
Esempio n. 4
0
/**
handlers to retrieve questions
from database
*/
func (m *Mongo) GetQuestions(c *gin.Context) {

	tokenForm := c.Query("token")

	if tokenForm == "" {
		log.Println("HUW", tokenForm)
		c.JSON(http.StatusBadRequest, gin.H{
			"err": "Parse error",
		})
		return
	}

	token, err := utils.JwtAuthenticator(tokenForm)

	if err != nil {
		c.JSON(http.StatusForbidden, gin.H{
			"err": "Log In Again",
		})
		return
	}

	_, ok := token.Claims["subject"].(string)

	if !ok {
		c.JSON(http.StatusForbidden, gin.H{
			"err": "Log In Again",
		})
		return
	}

	questions, err := new(models.Question).GetQuestions(m.Database)

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"err": err,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"err":       nil,
		"questions": questions,
	})
}
Esempio n. 5
0
func (m *Mongo) AddTest(c *gin.Context) {
	var addTestInput struct {
		Token string   `json:"token"`
		Ids   []string `json:"ids"`
		Name  string   `json:"name"`
		Group string   `json:"group"`
	}

	//Parse the req body
	err := c.BindJSON(&addTestInput)

	if err != nil {
		//send the incorrect response
		utils.ErrorResponse(c, http.StatusBadRequest, "Parse error")
		return
	}

	//check the token
	token, err := utils.JwtAuthenticator(addTestInput.Token)

	if err != nil {
		utils.ErrorResponse(c, http.StatusForbidden, "Log In again")
		return
	}

	var test models.Test

	subject, ok := token.Claims["subject"].(string)

	if !ok {
		utils.ErrorResponse(c, http.StatusForbidden, "Log In again")
		return
	}

	var questionIds []bson.ObjectId
	//convert string ids to object ids

	for _, e := range addTestInput.Ids {
		if bson.IsObjectIdHex(e) {
			log.Println("The objectid for")
			questionIds = append(questionIds, bson.ObjectIdHex(e))
		} else {
			utils.ErrorResponse(c, http.StatusInternalServerError, "ITS NOT U ITS ME ")
			return
		}

	}

	test.Subject = subject
	test.QuestionIds = questionIds
	test.Name = addTestInput.Name
	test.Group = addTestInput.Group
	test.Enable = false

	//add test to database
	err = test.AddTest(m.Database)

	if err != nil {
		log.Println("The funckin eeror", err)
		utils.ErrorResponse(c, http.StatusInternalServerError, "Cannot Insert")
		return
	}

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

}
Esempio n. 6
0
func (mongo *Mongo) AddQuestion(c *gin.Context) {
	var QuestionInput struct {
		Text    string   `json:"questionText"`
		Option1 string   `json:"option1"`
		Option2 string   `json:"option2"`
		Option3 string   `json:"option3"`
		Option4 string   `json:"option4"`
		Token   string   `json:"token"`
		Subject string   `json:"subject"`
		Tags    []string `json:"tags"`
		Correct string   `json:"correct"`
	}

	err := c.BindJSON(&QuestionInput)

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"err": true,
			"msg": "Error parsing",
		})
		return
	}

	if QuestionInput.Text != "" && QuestionInput.Option1 != "" &&
		QuestionInput.Option2 != "" && QuestionInput.Option3 != "" &&
		QuestionInput.Option4 != "" && QuestionInput.Subject != "" &&
		QuestionInput.Token != "" && QuestionInput.Correct != "" {

		/*
			validate jwt token
		*/
		/*		token, err := jwt.Parse(QuestionInput.Token, func(token *jwt.Token) (interface{}, error) {
				if token.Claims["subject"] == QuestionInput.Subject {
					return []byte(mySigningKey), nil
				} else {
					return nil, nil
				}

			})*/

		token, err := utils.JwtAuthenticator(QuestionInput.Token)

		if err == nil && token.Valid {
			/**
			Token is gud . Now move
			*/
			var question models.Question
			question.QuestionText = QuestionInput.Text
			question.Option1 = QuestionInput.Option1
			question.Option2 = QuestionInput.Option2
			question.Option3 = QuestionInput.Option3
			question.Option4 = QuestionInput.Option4
			question.Subject = QuestionInput.Subject
			question.Tags = QuestionInput.Tags
			question.Correct = QuestionInput.Correct
			question.QuestionSetter = token.Claims["username"].(string)

			err := question.AddQuestion(mongo.Database)

			if err != nil {
				c.JSON(http.StatusInternalServerError, gin.H{
					"err": true,
					"msg": "No insert",
				})
				return
			}

			c.JSON(http.StatusOK, gin.H{
				"err": false,
				"msg": "Quesiton Inserted",
			})
		} else {
			log.Println(err)
			c.JSON(http.StatusUnauthorized, gin.H{
				"err": true,
				"msg": "Sth fishy Login Again",
			})

		}

	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"err": true,
			"msg": "Missing Params",
		})
	}
}
Esempio n. 7
0
func (m *Mongo) StoreResult(c *gin.Context) {
	type ResponseForm struct {
		Id     string `json:"id"`
		Answer string `json:"answer"`
	}

	var resultForm struct {
		Token    string         `json:"token"`
		Score    string         `json:"score"`
		Response []ResponseForm `json:"response"`
		TestId   string         `json:"test_id"`
	}

	err := c.BindJSON(&resultForm)

	if err != nil {
		log.Println("rui", err)
		utils.ErrorResponse(c, http.StatusBadRequest, "Params wring")
		return
	}

	token, err := utils.JwtAuthenticator(resultForm.Token)

	println("ofijwejio")
	if err != nil {
		log.Println("kauwa", err)
		utils.ErrorResponse(c, http.StatusForbidden, "Log IN again")
		return
	}

	username, ok := token.Claims["username"].(string)

	if !ok {
		log.Println("kauwa", ok)
		utils.ErrorResponse(c, http.StatusForbidden, "log in afain")
		return
	}

	var res []models.Answer

	for _, r := range resultForm.Response {
		var temp models.Answer
		temp.Answer = r.Answer

		if bson.IsObjectIdHex(r.Id) {
			temp.QuestionId = bson.ObjectIdHex(r.Id)
		}

		res = append(res, temp)
	}

	var r models.Result
	r.Username = username
	r.Response = res
	r.Score = resultForm.Score

	if bson.IsObjectIdHex(resultForm.TestId) {
		r.TestId = bson.ObjectIdHex(resultForm.TestId)
	}

	err = r.InsertResult(m.Database)

	if err != nil {
		log.Println("kauwa", err)
		utils.ErrorResponse(c, http.StatusInternalServerError, "Cannot save result")
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg": "Done",
	})

}