Skip to content

Commit

Permalink
Merge pull request vacaramin#83 from vacaramin/Quiz-Backend
Browse files Browse the repository at this point in the history
Quiz addition
  • Loading branch information
vacaramin authored Dec 13, 2023
2 parents 2c55eed + 2bacb9f commit 9cad34f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 17 deletions.
83 changes: 79 additions & 4 deletions backend/src/quiz/controllers/quiz.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,83 @@ type QuizController struct{}

type QuizFunctions interface {
GetQuiz(*gin.Context)
AddQuiz(*gin.Context)
}

func (*QuizController) AddQuiz(ctx *gin.Context) {
// Check user authorization
_, err := utils.GetUserFromToken(ctx)
if err != nil {
log.Println(err)
ctx.JSON(http.StatusUnauthorized, gin.H{
"status": "failed",
"error": "Unauthorized",
})
return
}

// Define request body structure
type Options struct {
Option string `json:"option"`
Correct bool `json:"correct"`
}

type QuizQuestion struct {
Message string `json:"message"`
Question string `json:"question"`
Options []Options `json:"options"`
}

type RequestBody struct {
CourseID uint `json:"course_id"`
Message string `json:"message"`
Questions []QuizQuestion `json:"questions"`
}

var body RequestBody

// Bind JSON request body
if err := ctx.ShouldBindJSON(&body); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"status": "failed",
"error": "Invalid request body",
})
return
}

log.Println("Body after binding", body)

// Create quiz record
quiz := models.Quiz{
CourseID: body.CourseID,
Message: body.Message,
}
initializers.DB.Create(&quiz)

// Create quiz questions and options
for _, q := range body.Questions {
quizQuestion := models.QuizQuestion{
QuizID: quiz.ID,
Message: q.Message,
Question: q.Question,
}
initializers.DB.Create(&quizQuestion)

for _, o := range q.Options {
quizOption := models.QuizQuestionOption{
QuizQuestionID: quizQuestion.ID,
Option: o.Option,
Correct: o.Correct,
}
initializers.DB.Create(&quizOption)
}
}

ctx.JSON(http.StatusOK, gin.H{
"status": "Success",
"Quiz": quiz,
})
}
func (*QuizController) GetQuiz(ctx *gin.Context) {
// Finding User From Token Pretty much useless just for authorization
_, err := utils.GetUserFromToken(ctx)
Expand All @@ -38,10 +113,10 @@ func (*QuizController) GetQuiz(ctx *gin.Context) {
log.Println("Body after binding", Body)

var Quiz models.Quizzes
log.Println("Body Subject", Body.Subject)
log.Println("Executing query for course_id:", Body.Subject)
result := initializers.DB.Where("course_id = ?", Body.Subject).Find(&Quiz)
log.Println("Query executed:", result.Dialector.Explain(result.Statement.SQL.String(), result.Statement.Vars...))
result := initializers.DB.
Preload("Questions.Options").
Where("course_id = ?", Body.Subject).
Find(&Quiz)

if result.Error != nil {
log.Println(result.Error)
Expand Down
11 changes: 6 additions & 5 deletions backend/src/quiz/models/quiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (

// Quiz represents the "quiz" table
type Quiz struct {
ID uint `json:"quiz_id"`
CourseID uint `json:"course_id"`
Message string `json:"message"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ID uint `json:"quiz_id"`
CourseID uint `json:"course_id"`
Message string `json:"message"`
Questions QuizQuestions `json:"questions"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

func (Quiz) TableName() string {
Expand Down
17 changes: 10 additions & 7 deletions backend/src/quiz/models/quizQuestion.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package models

import "time"
import (
"time"
)

// QuizQuestion represents the "quiz_question" table
type QuizQuestion struct {
ID uint `json:"-"`
QuizID uint `json:"quiz_id"`
Message string `json:"message"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ID uint `json:"-"`
QuizID uint `json:"quiz_id"`
Message string `json:"message"`
Options QuizQuestionOptions `json:"options"`
Question string `json:"question"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

// QuizQuestions is a slice of QuizQuestion
Expand Down
2 changes: 1 addition & 1 deletion backend/src/quiz/routes/quiz.routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func SetupRoutes(r *gin.Engine, controller controllers.QuizFunctions) {
quizRoutes := r.Group("quiz")
quizRoutes.GET("getquiz", controller.GetQuiz)
quizRoutes.POST("quiz", controller.GetQuiz)
quizRoutes.POST("quiz", controller.AddQuiz)
quizRoutes.DELETE("quiz", controller.GetQuiz)

}

0 comments on commit 9cad34f

Please sign in to comment.