package controllers import ( "encoding/json" "fmt" "net/http" "strconv" "time" "learnsteam/cslms-api/internal/models" "learnsteam/cslms-api/internal/services" "github.com/gin-gonic/gin" "github.com/google/uuid" "gorm.io/datatypes" ) type QuizController interface { List(*gin.Context) Find(*gin.Context) Create(*gin.Context) Update(*gin.Context) Delete(*gin.Context) } type quizController struct { service services.QuizService } func NewQuizController(service services.QuizService) QuizController { return &quizController{ service: service, } } // Quiz List // // @Summary 퀴즈 목록 가져오기 // @Description 퀴즈 목록을 가져옵니다. // @Tags 퀴즈 // // @Accept json // @Produce json // // @Security Bearer // // @Param quiz_paper_id query string true "퀴즈페이퍼 ID" // @Param q query string false "검색어" // @Param page query int false "페이지" // @Param limit query int false "페이지 사이즈" // // @Success 200 {object} models.QuizListResponse // @Router /quiz [get] func (controller *quizController) List(c *gin.Context) { quiz_paper_id, err := strconv.ParseInt(c.DefaultQuery("quiz_paper_id", ""), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } page, err := strconv.Atoi(c.DefaultQuery("page", "1")) if err != nil { fmt.Println("error : page") c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } limit, err := strconv.Atoi(c.DefaultQuery("limit", "10")) if err != nil { fmt.Println("error : limit") c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } result, err := controller.service.List(quiz_paper_id, page, limit) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } totalNumber, err := controller.service.Total(quiz_paper_id) if err != nil { fmt.Println("error : list") c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } totalPage := int64(totalNumber/int64(limit) + 1) c.JSON(http.StatusOK, gin.H{"data": result, "total": totalNumber, "page": page, "totalPage": totalPage, "pageSize": limit}) } // Get Quiz // // @Summary 퀴즈 가져오기 // @Description ID로 퀴즈를 가져옵니다. // @Tags 퀴즈 // // @Accept json // @Produce json // // @Security Bearer // // @Param id path string true "퀴즈 ID" // // @Success 200 {object} models.QuizResponse // @Router /quiz/{id} [get] func (controller *quizController) Find(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } result, err := controller.service.Find(id) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, result) } // Create Quiz // // @Summary 퀴즈 생성 // @Description 퀴즈를 만듭니다. // @Tags 퀴즈 // // @Accept json // @Produce json // // @Security Bearer // // @Param quizBody body models.QuizRequest true "Quiz Body" // // @Success 200 {object} models.QuizResponse // @Router /quiz [post] func (controller *quizController) Create(c *gin.Context) { var request models.QuizRequest if err := c.ShouldBindJSON(&request); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var content datatypes.JSON content, err := json.Marshal(request.Content) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var answer datatypes.JSON answer, err = json.Marshal(request.Answer) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } quiz := &models.Quiz{ GUID: uuid.NewString(), CenterID: request.CenterID, QuizPaperID: request.QuizPaperID, No: request.No, QuestionType: request.QuestionType, Question: request.Question, Content: content, Answer: answer, UpdatedAt: time.Now(), CreatedAt: time.Now(), } result, err := controller.service.Create(quiz) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, result) } // Update Quiz // // @Summary 퀴즈 수정 // @Description 퀴즈를 수정합니다. // @Tags 퀴즈 // // @Accept json // @Produce json // // @Security Bearer // // @Param id path int64 true "Quiz ID" // @Param quizBody body models.QuizRequest true "Quiz Body" // // @Success 200 {object} models.QuizResponse // @Router /quiz/{id} [put] func (controller *quizController) Update(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var request models.QuizRequest if err := c.ShouldBindJSON(&request); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } quiz, err := controller.service.Find(id) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var content datatypes.JSON content, err = json.Marshal(request.Content) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var answer datatypes.JSON answer, err = json.Marshal(request.Answer) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } quiz.QuizPaperID = request.QuizPaperID quiz.No = request.No quiz.QuestionType = request.QuestionType quiz.Question = request.Question quiz.Content = content quiz.Answer = answer quiz.UpdatedAt = time.Now() result, err := controller.service.Update(quiz) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, result) } // Delete Quiz // // @Summary 퀴즈 삭제 // @Description 퀴즈를 삭제합니다. // @Tags 퀴즈 // // @Accept json // @Produce json // // @Security Bearer // // @Param id path int64 true "Quiz ID" // // @Router /quiz/{id} [delete] func (controller *quizController) Delete(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } _, err = controller.service.Find(id) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } err = controller.service.Delete(id) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "success"}) }