//GetLeaderBoardsOfTest handler func to get the leaderboards of a particilar test
func (m *Mongo) GetLeaderBoardsOfTest(c *gin.Context) {
	testId := c.Param("testId")

	if testId == "" {
		utils.ErrorResponse(c, http.StatusBadRequest, "No id provided")
		return
	}

	var result models.Result

	//convert testid string to bson.ObjectId
	if bson.IsObjectIdHex(testId) {
		result.TestId = bson.ObjectIdHex(testId)
	} else {
		utils.ErrorResponse(c, http.StatusExpectationFailed, "Cannot retreive test id")
		return
	}

	results, err := result.GetResultByID(m.Database)

	log.Println("The res", results)

	if err != nil {
		utils.ErrorResponse(c, http.StatusInternalServerError, "Cannot retreive from db")
		return
	}

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

}
Exemple #2
0
func (m *Mongo) GetAllTest(c *gin.Context) {
	tokenReceived := c.Query("token")

	subject, err := utils.AuthenticateTokenGetSubject(tokenReceived)

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

	var test models.Test
	test.Subject = subject

	tests, err := test.GetAllTest(m.Database)

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

	c.JSON(http.StatusOK, gin.H{
		"err":   nil,
		"tests": tests,
	})
}
func (m *Mongo) StudentLogin(c *gin.Context) {
	var loginForm struct {
		Username string `json:"username"`
		Password string `json:"password"`
	}

	err := c.BindJSON(&loginForm)

	if err != nil {
		log.Println("Github retards", err)
		utils.ErrorResponse(c, http.StatusInternalServerError, "Incorrect params")
		return
	}

	var studMahan models.Student
	studMahan.Username = loginForm.Username
	studMahan.Password = loginForm.Password

	stud, err := studMahan.CheckUserPassword(m.Database)

	if err != nil {
		log.Println("Absent", err)
		utils.ErrorResponse(c, http.StatusNotFound, "No such user")
		return
	}

	if stud.Username != "" {
		//make the f****n token

		/**
		Create the token
		*/
		token := jwt.New(jwt.SigningMethodHS256)
		token.Claims["username"] = stud.Username
		token.Claims["group"] = stud.Group
		token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()

		tokenString, err := token.SignedString([]byte(utils.MySigningKey))

		if err != nil {
			utils.ErrorResponse(c, http.StatusInternalServerError, "Error creating token")
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"token": tokenString,
		})
	} else {
		utils.ErrorResponse(c, http.StatusNotFound, "No such user")
		return
	}
}
Exemple #4
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,
	})
}
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,
	})

}
Exemple #6
0
/**
Handler for new teacher
*/
func (mongo *Mongo) AddTeacher(c *gin.Context) {
	/**
	Room for imporv use auto bindind
	*/

	var teacher models.Teacher

	var req struct {
		AdminPass string `json:"adminPass"`
		Username  string `json:"username"`
		Password  string `json:"password"`
		Subject   string `json:"subject"`
	}

	err := c.BindJSON(&req)

	if err != nil {
		utils.ErrorResponse(c, http.StatusBadRequest, "Could not parse")
		return
	}

	adminPass := req.AdminPass

	if adminPass == "admin" {

		teacher.Password = req.Password
		teacher.Subject = req.Subject
		teacher.Username = req.Username

		if teacher.Password == "" || teacher.Subject == "" || teacher.Username == "" {
			var res utils.Response
			res.Err = true
			res.Msg = "Missing params"

			c.JSON(http.StatusForbidden, res)
		} else {

			/**
			Insert the teacher in the db
			*/
			response := teacher.AddDoc(mongo.Database)

			if response.Err == true {
				c.JSON(http.StatusInternalServerError, response)
			} else {
				c.JSON(http.StatusOK, response)
			}
		}

	} else {
		var res utils.Response
		res.Err = true
		res.Msg = "Unauth "

		c.JSON(http.StatusForbidden, res)
	}
}
func (m *Mongo) GetUserNames(c *gin.Context) {
	usernames, err := models.GetUserNames(m.Database)

	if err != nil {
		println("E", err)
		utils.ErrorResponse(c, http.StatusInternalServerError, "F**k's gone wrong")
		return
	}

	c.JSON(http.StatusOK, usernames)
}
func (m *Mongo) StudentSignUp(c *gin.Context) {
	var signUpForm struct {
		Username string   `json:"username"`
		Password string   `json:"password"`
		Group    []string `json:"groups"`
	}

	err := c.BindJSON(&signUpForm)

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

	if signUpForm.Username == "" || signUpForm.Password == "" || len(signUpForm.Group) == 0 {
		log.Println(signUpForm)
		utils.ErrorResponse(c, http.StatusBadRequest, "Give Proper Formats")
		return
	}

	student := models.Student{
		Username: signUpForm.Username,
		Password: signUpForm.Password,
		Group:    signUpForm.Group,
	}

	err = student.NewUser(m.Database)

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

	c.JSON(http.StatusOK, gin.H{
		"msg": "All done",
	})
}
Exemple #9
0
func (m *Mongo) EnableTest(c *gin.Context) {
	tokenReceived := c.Query("token")
	id := c.Param("id")
	enabler := c.Query("enable")

	subject, err := utils.AuthenticateTokenGetSubject(tokenReceived)

	if err != nil {
		utils.ErrorResponse(c, http.StatusForbidden, "Log in again")
		return
	}
	log.Println("thr dod", id)
	var test models.Test
	test.Subject = subject
	if bson.IsObjectIdHex(id) {
		test.Id = bson.ObjectIdHex(id)
	} else {
		utils.ErrorResponse(c, http.StatusInternalServerError, "Oh boy not again")
		return
	}

	if enabler == "true" {
		test.Enable = false
	} else {
		test.Enable = true
	}

	err = test.SetTestProps(m.Database)

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

	c.JSON(http.StatusOK, gin.H{
		"err": nil,
	})
}
Exemple #10
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,
	})

}
//GetRanks ..get the overall rankings implementing the ranking algo
func (m *Mongo) GetRanks(c *gin.Context) {
	results, err := models.GetResults(m.Database)

	if err != nil {
		utils.ErrorResponse(c, http.StatusInternalServerError, "Error querying")
		return
	}

	users := make(map[string]float64)
	tests := make(map[string]struct{})

	var tempTestId string

	//get unique usernames and testids
	for _, user := range results {
		_, ok := users[user.Username]

		if !ok {
			users[user.Username] = 0
		}

		tempTestId = user.TestId.Hex()
		_, okAgain := tests[tempTestId]

		if !okAgain && tempTestId != "" {
			tests[tempTestId] = struct{}{}
		}
	}

	log.Println("Users", users, tests)

	for user := range users {
		theScore := 0.0
		for _ = range tests {
			var restScore, myScore []float64
			totRestScore := 0.0

			for _, result := range results {
				thisScore, err := strconv.ParseFloat(result.Score, 64)

				if err != nil {
					log.Println("Errorwa", err)
					utils.ErrorResponse(c, http.StatusInternalServerError, "Cannot convert to str")
					return
				}

				if result.Username == user {
					myScore = append(myScore, thisScore)
				} else {
					restScore = append(restScore, thisScore)
					totRestScore += thisScore
				}
			}

			expectedScore := totRestScore / float64(len(restScore))

			for _, sc := range myScore {
				theScore += (sc - expectedScore)
			}

		}

		users[user] = theScore
	}

	panic("Not implemented")

	c.JSON(http.StatusOK, gin.H{
		"users": users,
		"tests": tests,
	})
}
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",
	})

}