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) 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) 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, }) }
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, }) }