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 QuizPaperController interface { List(*gin.Context) Find(*gin.Context) Create(*gin.Context) Update(*gin.Context) Copy(*gin.Context) } type quizPaperController struct { service services.QuizPaperService quizService services.QuizService } func NewQuizPaperController(service services.QuizPaperService, quizService services.QuizService) QuizPaperController { return &quizPaperController{ service: service, quizService: quizService, } } // QuizPaper List // // @Summary 퀴즈페이퍼 목록 가져오기 // @Description 퀴즈페이퍼 목록을 가져옵니다. // @Tags 퀴즈페이퍼 // // @Accept json // @Produce json // // @Security Bearer // // @Param tag query string false "태그" // @Param q query string false "검색어" // @Param page query int false "페이지" // @Param limit query int false "페이지 사이즈" // // @Success 200 {object} models.QuizPaperListResponse // @Router /quizpaper [get] func (controller *quizPaperController) List(c *gin.Context) { tag := c.DefaultQuery("tag", "") q := c.DefaultQuery("q", "") 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(q, tag, page, limit) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } totalNumber, err := controller.service.Total(q, tag) if err != nil { fmt.Println("error : list") c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } totalPage := int64(totalNumber/int64(limit) + 1) response := models.QuizPaperListResponse{ Data: *result, Total: totalPage, Page: page, TotalPage: totalPage, PageSize: limit, } c.JSON(http.StatusOK, response) } // Get quizPaper // // @Summary 퀴즈 퀴즈페이퍼 가져오기 // @Description ID로 퀴즈 퀴즈페이퍼을 가져옵니다. // @Tags 퀴즈페이퍼 // // @Accept json // @Produce json // // @Security Bearer // // @Param id path string true "퀴즈 퀴즈페이퍼 ID" // // @Success 200 {object} models.QuizPaperResponse // @Router /quizpaper/{id} [get] func (controller *quizPaperController) 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 QuizPaper // // @Summary 퀴즈 퀴즈페이퍼 생성 // @Description 퀴즈 퀴즈페이퍼을 만듭니다. // @Tags 퀴즈페이퍼 // // @Accept json // @Produce json // // @Security Bearer // // @Param quizPaperBody body models.QuizPaperRequest true "QuizPaper Body" // // @Success 200 {object} models.QuizPaperResponse // @Router /quizpaper [post] func (controller *quizPaperController) Create(c *gin.Context) { var request models.QuizPaperRequest if err := c.ShouldBindJSON(&request); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } user_id := c.GetInt64("sub") var tag datatypes.JSON tag, err := json.Marshal(request.Tag) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } quizPaper := &models.QuizPaper{ GUID: uuid.NewString(), CenterID: request.CenterID, ManagerID: user_id, Title: request.Title, Content: request.Content, Category: request.Category, Tag: tag, Status: request.Status, CreatedAt: time.Now(), UpdatedAt: time.Now(), } result, err := controller.service.Create(quizPaper) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, result) } // Update QuizPaper // // @Summary 퀴즈 퀴즈페이퍼 수정 // @Description 퀴즈 퀴즈페이퍼을 수정합니다. // @Tags 퀴즈페이퍼 // // @Accept json // @Produce json // // @Security Bearer // // @Param id path string true "퀴즈 퀴즈페이퍼 ID" // @Param quizPaperUpdateBody body models.QuizPaperRequest true "QuizPaper Body" // // @Success 200 {object} models.QuizPaperResponse // @Router /quizpaper/{id} [put] func (controller *quizPaperController) 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.QuizPaperRequest if err := c.ShouldBindJSON(&request); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } quizPaper, err := controller.service.Find(id) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var tag datatypes.JSON tag, err = json.Marshal(request.Tag) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } user_id := c.GetInt64("sub") quizPaper.ManagerID = user_id quizPaper.CenterID = request.CenterID quizPaper.Title = request.Title quizPaper.Content = request.Content quizPaper.Category = request.Category quizPaper.Tag = tag quizPaper.Status = request.Status quizPaper.UpdatedAt = time.Now() result, err := controller.service.Update(quizPaper) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, result) } // Copy QuizPaper // // @Summary 퀴즈 퀴즈페이퍼 복사 // @Description 퀴즈 퀴즈페이퍼와 퀴즈를 복사합니다. // @Tags 퀴즈페이퍼 // // @Accept json // @Produce json // // @Security Bearer // // @Param id path string true "퀴즈페이퍼 ID" // @Param quizPaperCopyBody body models.QuizPaperCopyRequest true "QuizPaper Copy Body" // // @Success 200 {object} models.QuizPaperCopyResponse // @Router /quizpaper/{id}/copy [post] func (controller *quizPaperController) Copy(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) fmt.Println("id : ", c.Param("id")) if err != nil { fmt.Println("id convert err") c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var request models.QuizPaperCopyRequest if err := c.ShouldBindJSON(&request); err != nil { fmt.Println("req err") c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } user_id := c.GetInt64("sub") template, err := controller.service.Find(id) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } newQuizPaper := &models.QuizPaper{ GUID: uuid.NewString(), CenterID: request.CenterID, ManagerID: user_id, Title: request.Title, Content: template.Content, Category: template.Category, Tag: template.Tag, Status: template.Status, CreatedAt: time.Now(), UpdatedAt: time.Now(), } quizPaper, err := controller.service.Create(newQuizPaper) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } quizTemplates, err := controller.quizService.List(id, 1, 10000) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // iterate quizTemplate var quizzes []*models.Quiz for _, quiz := range *quizTemplates { newQuiz := &models.Quiz{ GUID: uuid.NewString(), CenterID: request.CenterID, QuizPaperID: quizPaper.ID, No: quiz.No, QuestionType: quiz.QuestionType, Question: quiz.Question, Content: quiz.Content, Answer: quiz.Answer, UpdatedAt: time.Now(), CreatedAt: time.Now(), } result, err := controller.quizService.Create(newQuiz) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } quizzes = append(quizzes, result) } c.JSON(http.StatusOK, gin.H{"quiz_paper": quizPaper, "quiz": quizzes}) }