Spaces:
Sleeping
Sleeping
| package controllers | |
| import ( | |
| "slices" | |
| "strconv" | |
| "time" | |
| "net/http" | |
| "abdanhafidz.com/go-boilerplate/models/dto" | |
| entity "abdanhafidz.com/go-boilerplate/models/entity" | |
| http_error "abdanhafidz.com/go-boilerplate/models/error" | |
| "abdanhafidz.com/go-boilerplate/services" | |
| "github.com/gin-gonic/gin" | |
| "github.com/google/uuid" | |
| "github.com/lib/pq" | |
| ) | |
| type AdminProblemSetController interface { | |
| ListProblemSets(ctx *gin.Context) | |
| CreateProblemSet(ctx *gin.Context) | |
| UpdateProblemSet(ctx *gin.Context) | |
| DeleteProblemSet(ctx *gin.Context) | |
| ListProblemSetsByExam(ctx *gin.Context) | |
| ListCandidateProblemSetsByExam(ctx *gin.Context) | |
| AssignProblemSetToExam(ctx *gin.Context) | |
| UnassignProblemSetFromExam(ctx *gin.Context) | |
| ListQuestions(ctx *gin.Context) | |
| AddQuestion(ctx *gin.Context) | |
| BulkAddQuestions(ctx *gin.Context) | |
| UpdateQuestion(ctx *gin.Context) | |
| BulkUpdateQuestions(ctx *gin.Context) | |
| DeleteQuestion(ctx *gin.Context) | |
| GetQuestionDetail(ctx *gin.Context) | |
| } | |
| type adminProblemSetController struct { | |
| problemSetService services.ProblemSetService | |
| } | |
| func NewAdminProblemSetController(problemSetService services.ProblemSetService) AdminProblemSetController { | |
| return &adminProblemSetController{problemSetService: problemSetService} | |
| } | |
| // ListProblemSets godoc | |
| // @Summary Admin: List Problem Sets | |
| // @Description List all problem sets | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/problemsets [get] | |
| func (c *adminProblemSetController) ListProblemSets(ctx *gin.Context) { | |
| limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10")) | |
| page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) | |
| search := ctx.DefaultQuery("search", "") | |
| sortBy := ctx.DefaultQuery("sortBy", "") | |
| if sortBy == "" { | |
| sortBy = ctx.DefaultQuery("sortby", "") | |
| } | |
| if sortBy == "" { | |
| sortBy = ctx.DefaultQuery("sorby", "") | |
| } | |
| order := ctx.DefaultQuery("orderBy", "") | |
| if order == "" { | |
| order = ctx.DefaultQuery("orderby", "") | |
| } | |
| if order == "" { | |
| order = ctx.DefaultQuery("order", "") | |
| } | |
| if limit < 1 { | |
| limit = 10 | |
| } else if limit > 100 { | |
| limit = 100 | |
| } | |
| if page < 1 { | |
| page = 1 | |
| } | |
| offset := (page - 1) * limit | |
| p := entity.Pagination{Limit: limit, Offset: offset, Search: search, SortBy: sortBy, Order: order} | |
| list, total, err := c.problemSetService.ListProblemSetsWithPagination(ctx.Request.Context(), p) | |
| if err != nil { | |
| ResponseJSON[any, any](ctx, nil, nil, err) | |
| return | |
| } | |
| totalPages := int((total + int64(limit) - 1) / int64(limit)) | |
| if total == 0 { | |
| totalPages = 1 | |
| } | |
| if page > totalPages { | |
| page = totalPages | |
| } | |
| meta := gin.H{ | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // CreateProblemSet godoc | |
| // @Summary Admin: Create Problem Set | |
| // @Description Create a new problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param request body dto.CreateProblemSetRequest true "Create Problem Set Request" | |
| // @Router /api/v1/admin/problemsets [post] | |
| func (c *adminProblemSetController) CreateProblemSet(ctx *gin.Context) { | |
| req := RequestJSON[dto.CreateProblemSetRequest](ctx) | |
| ps := entity.ProblemSet{ | |
| Id: uuid.New(), | |
| Title: req.Title, | |
| Description: req.Description, | |
| } | |
| err := c.problemSetService.CreateProblemSet(ctx.Request.Context(), ps) | |
| if err != nil { | |
| ResponseJSON(ctx, req, entity.ProblemSet{}, err) | |
| return | |
| } | |
| created, err := c.problemSetService.GetProblemSet(ctx.Request.Context(), ps.Id) | |
| ResponseJSON(ctx, req, created, err) | |
| } | |
| // UpdateProblemSet godoc | |
| // @Summary Admin: Update Problem Set | |
| // @Description Update an existing problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param id path string true "Problem Set ID" | |
| // @Param request body dto.UpdateProblemSetRequest true "Update Problem Set Request" | |
| // @Router /api/v1/admin/problemsets/{id} [put] | |
| func (c *adminProblemSetController) UpdateProblemSet(ctx *gin.Context) { | |
| id, err := uuid.Parse(ctx.Param("id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": ctx.Param("id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| req := RequestJSON[dto.UpdateProblemSetRequest](ctx) | |
| ps := entity.ProblemSet{ | |
| Id: id, | |
| Title: req.Title, | |
| Description: req.Description, | |
| } | |
| err = c.problemSetService.UpdateProblemSet(ctx.Request.Context(), ps) | |
| if err != nil { | |
| ResponseJSON(ctx, req, entity.ProblemSet{}, err) | |
| return | |
| } | |
| updated, err := c.problemSetService.GetProblemSet(ctx.Request.Context(), id) | |
| ResponseJSON(ctx, req, updated, err) | |
| } | |
| // DeleteProblemSet godoc | |
| // @Summary Admin: Delete Problem Set | |
| // @Description Delete a problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param id path string true "Problem Set ID" | |
| // @Router /api/v1/admin/problemsets/{id} [delete] | |
| func (c *adminProblemSetController) DeleteProblemSet(ctx *gin.Context) { | |
| id, err := uuid.Parse(ctx.Param("id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": ctx.Param("id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| delErr := c.problemSetService.DeleteProblemSet(ctx.Request.Context(), id) | |
| ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": delErr == nil}, delErr) | |
| } | |
| // ListQuestions godoc | |
| // @Summary Admin: List Questions in Problem Set | |
| // @Description List questions that belong to a problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param id path string true "Problem Set ID" | |
| // @Router /api/v1/admin/problemsets/{id}/questions [get] | |
| func (c *adminProblemSetController) ListQuestions(ctx *gin.Context) { | |
| problemSetId, err := uuid.Parse(ctx.Param("id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": ctx.Param("id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10")) | |
| page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) | |
| search := ctx.DefaultQuery("search", "") | |
| sortBy := ctx.DefaultQuery("sortBy", "") | |
| if sortBy == "" { | |
| sortBy = ctx.DefaultQuery("sortby", "") | |
| } | |
| if sortBy == "" { | |
| sortBy = ctx.DefaultQuery("sorby", "") | |
| } | |
| order := ctx.DefaultQuery("orderBy", "") | |
| if order == "" { | |
| order = ctx.DefaultQuery("orderby", "") | |
| } | |
| if order == "" { | |
| order = ctx.DefaultQuery("order", "") | |
| } | |
| if limit < 1 { | |
| limit = 10 | |
| } else if limit > 100 { | |
| limit = 100 | |
| } | |
| if page < 1 { | |
| page = 1 | |
| } | |
| offset := (page - 1) * limit | |
| p := entity.Pagination{Limit: limit, Offset: offset, Search: search, SortBy: sortBy, Order: order} | |
| list, total, listErr := c.problemSetService.ListQuestionsWithPagination(ctx.Request.Context(), problemSetId, p) | |
| if listErr != nil { | |
| ResponseJSON[any, any](ctx, nil, nil, listErr) | |
| return | |
| } | |
| totalPages := int((total + int64(limit) - 1) / int64(limit)) | |
| if total == 0 { | |
| totalPages = 1 | |
| } | |
| if page > totalPages { | |
| page = totalPages | |
| } | |
| meta := gin.H{ | |
| "problemset_id": problemSetId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // ListProblemSetsByExam godoc | |
| // @Summary Admin: List Problem Sets by Exam | |
| // @Description List problem sets assigned to an exam | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Router /api/v1/admin/exams/{exam_id}/problemsets [get] | |
| func (c *adminProblemSetController) ListProblemSetsByExam(ctx *gin.Context) { | |
| examId, err := uuid.Parse(ctx.Param("exam_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"exam_id": ctx.Param("exam_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10")) | |
| page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) | |
| search := ctx.DefaultQuery("search", "") | |
| sortBy := ctx.DefaultQuery("sortBy", "") | |
| order := ctx.DefaultQuery("order", "") | |
| if limit < 1 { | |
| limit = 10 | |
| } else if limit > 100 { | |
| limit = 100 | |
| } | |
| if page < 1 { | |
| page = 1 | |
| } | |
| offset := (page - 1) * limit | |
| p := entity.Pagination{Limit: limit, Offset: offset, Search: search, SortBy: sortBy, Order: order} | |
| list, total, listErr := c.problemSetService.ListProblemSetsByExam(ctx.Request.Context(), examId, p) | |
| if listErr != nil { | |
| ResponseJSON[any, any](ctx, nil, nil, listErr) | |
| return | |
| } | |
| totalPages := int((total + int64(limit) - 1) / int64(limit)) | |
| if total == 0 { | |
| totalPages = 1 | |
| } | |
| if page > totalPages { | |
| page = totalPages | |
| } | |
| meta := gin.H{ | |
| "exam_id": examId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // ListCandidateProblemSetsByExam godoc | |
| // @Summary Admin: List Candidate Problem Sets by Exam | |
| // @Description List problem sets that can be assigned to an exam | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Router /api/v1/admin/exams/{exam_id}/problemsets/candidate [get] | |
| func (c *adminProblemSetController) ListCandidateProblemSetsByExam(ctx *gin.Context) { | |
| examId, err := uuid.Parse(ctx.Param("exam_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"exam_id": ctx.Param("exam_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10")) | |
| page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) | |
| search := ctx.DefaultQuery("search", "") | |
| sortBy := ctx.DefaultQuery("sortBy", "") | |
| if sortBy == "" { | |
| sortBy = ctx.DefaultQuery("sortby", "") | |
| } | |
| order := ctx.DefaultQuery("orderBy", "") | |
| if order == "" { | |
| order = ctx.DefaultQuery("orderby", "") | |
| } | |
| if order == "" { | |
| order = ctx.DefaultQuery("order", "") | |
| } | |
| if limit < 1 { | |
| limit = 10 | |
| } else if limit > 100 { | |
| limit = 100 | |
| } | |
| if page < 1 { | |
| page = 1 | |
| } | |
| offset := (page - 1) * limit | |
| p := entity.Pagination{Limit: limit, Offset: offset, Search: search, SortBy: sortBy, Order: order} | |
| list, total, listErr := c.problemSetService.ListCandidateProblemSetsByExam(ctx.Request.Context(), examId, p) | |
| if listErr != nil { | |
| ResponseJSON[any, any](ctx, nil, nil, listErr) | |
| return | |
| } | |
| totalPages := int((total + int64(limit) - 1) / int64(limit)) | |
| if total == 0 { | |
| totalPages = 1 | |
| } | |
| if page > totalPages { | |
| page = totalPages | |
| } | |
| meta := gin.H{ | |
| "exam_id": examId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // AssignProblemSetToExam godoc | |
| // @Summary Admin: Assign Problem Set to Exam | |
| // @Description Assign a problem set to an exam | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Param problemset_id path string true "Problem Set ID" | |
| // @Router /api/v1/admin/exams/{exam_id}/problemsets/{problemset_id} [post] | |
| func (c *adminProblemSetController) AssignProblemSetToExam(ctx *gin.Context) { | |
| examId, err := uuid.Parse(ctx.Param("exam_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"exam_id": ctx.Param("exam_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| problemSetId, err := uuid.Parse(ctx.Param("problemset_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"problemset_id": ctx.Param("problemset_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| assignErr := c.problemSetService.AssignProblemSetToExam(ctx.Request.Context(), examId, problemSetId) | |
| ResponseJSON(ctx, gin.H{"exam_id": examId, "problemset_id": problemSetId}, gin.H{"assigned": assignErr == nil}, assignErr) | |
| } | |
| // UnassignProblemSetFromExam godoc | |
| // @Summary Admin: Unassign Problem Set from Exam | |
| // @Description Remove a problem set assignment from an exam | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Param problemset_id path string true "Problem Set ID" | |
| // @Router /api/v1/admin/exams/{exam_id}/problemsets/{problemset_id} [delete] | |
| func (c *adminProblemSetController) UnassignProblemSetFromExam(ctx *gin.Context) { | |
| examId, err := uuid.Parse(ctx.Param("exam_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"exam_id": ctx.Param("exam_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| problemSetId, err := uuid.Parse(ctx.Param("problemset_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"problemset_id": ctx.Param("problemset_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| delErr := c.problemSetService.RemoveAssignedProblemSetByExam(ctx.Request.Context(), examId, problemSetId) | |
| ResponseJSON(ctx, gin.H{"exam_id": examId, "problemset_id": problemSetId}, gin.H{"removed": delErr == nil}, delErr) | |
| } | |
| // AddQuestion godoc | |
| // @Summary Admin: Add Question to Problem Set | |
| // @Description Create a question inside a problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param id path string true "Problem Set ID" | |
| // @Param request body dto.CreateQuestionRequest true "Create Question Request" | |
| // @Router /api/v1/admin/problemsets/{id}/questions [post] | |
| func (c *adminProblemSetController) AddQuestion(ctx *gin.Context) { | |
| problemSetId, err := uuid.Parse(ctx.Param("id")) | |
| if err != nil { | |
| ctx.JSON(http.StatusBadRequest, gin.H{ | |
| "status": "error", | |
| "message": http_error.INVALID_TOKEN.Error(), | |
| }) | |
| return | |
| } | |
| req := RequestJSON[dto.CreateQuestionRequest](ctx) | |
| if !slices.Contains(entity.QuestionTypes, req.Type) { | |
| ctx.JSON(http.StatusBadRequest, gin.H{ | |
| "status": "error", | |
| "message": http_error.INVALID_QUESTION_TYPE.Error(), | |
| }) | |
| return | |
| } | |
| q := entity.Questions{ | |
| Id: uuid.New(), | |
| ProblemSetId: problemSetId, | |
| Type: req.Type, | |
| Question: req.Question, | |
| Options: pq.StringArray(req.Options), | |
| AnsKey: pq.StringArray(req.AnsKey), | |
| CorrMark: req.CorrMark, | |
| IncorrMark: req.IncorrMark, | |
| CreatedAt: time.Now(), | |
| NullMark: req.NullMark, | |
| Solution: req.Solution, | |
| } | |
| err = c.problemSetService.AddQuestion(ctx.Request.Context(), q) | |
| if err != nil { | |
| ResponseJSON(ctx, req, entity.Questions{}, err) | |
| return | |
| } | |
| created, err := c.problemSetService.GetQuestionById(ctx.Request.Context(), q.Id) | |
| ResponseJSON(ctx, req, created, err) | |
| } | |
| // BulkAddQuestions godoc | |
| // @Summary Admin: Bulk Add Questions to Problem Set | |
| // @Description Create multiple questions inside a problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param id path string true "Problem Set ID" | |
| // @Param request body dto.BulkCreateQuestionsRequest true "Bulk Create Questions Request" | |
| // @Router /api/v1/admin/problemsets/{id}/questions/bulk [post] | |
| func (c *adminProblemSetController) BulkAddQuestions(ctx *gin.Context) { | |
| problemSetId, err := uuid.Parse(ctx.Param("id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": ctx.Param("id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| req := RequestJSON[dto.BulkCreateQuestionsRequest](ctx) | |
| if ctx.IsAborted() { | |
| return | |
| } | |
| questions := make([]entity.Questions, 0, len(req.Questions)) | |
| ticker := time.NewTicker(1 * time.Millisecond) | |
| defer ticker.Stop() | |
| for idx, item := range req.Questions { | |
| questions = append(questions, entity.Questions{ | |
| Id: uuid.New(), | |
| ProblemSetId: problemSetId, | |
| Type: item.Type, | |
| Question: item.Question, | |
| Options: pq.StringArray(item.Options), | |
| AnsKey: pq.StringArray(item.AnsKey), | |
| CorrMark: item.CorrMark, | |
| IncorrMark: item.IncorrMark, | |
| CreatedAt: time.Now(), | |
| NullMark: item.NullMark, | |
| Solution: item.Solution, | |
| }) | |
| if idx+1 < len(req.Questions) { | |
| <-ticker.C | |
| } | |
| } | |
| created, err := c.problemSetService.BulkAddQuestions(ctx.Request.Context(), problemSetId, questions) | |
| if err != nil { | |
| ResponseJSON(ctx, req, []entity.Questions{}, err) | |
| return | |
| } | |
| ResponseJSON(ctx, gin.H{"problemset_id": problemSetId, "count": len(created)}, created, nil) | |
| } | |
| // UpdateQuestion godoc | |
| // @Summary Admin: Update Question in Problem Set | |
| // @Description Update a question in a problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param id path string true "Problem Set ID" | |
| // @Param question_id path string true "Question ID" | |
| // @Param request body dto.UpdateQuestionRequest true "Update Question Request" | |
| // @Router /api/v1/admin/problemsets/{id}/questions/{question_id} [put] | |
| func (c *adminProblemSetController) UpdateQuestion(ctx *gin.Context) { | |
| problemSetId, err := uuid.Parse(ctx.Param("id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": ctx.Param("id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| questionId, err := uuid.Parse(ctx.Param("question_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"question_id": ctx.Param("question_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| existing, err := c.problemSetService.GetQuestionById(ctx.Request.Context(), questionId) | |
| if err != nil { | |
| ResponseJSON(ctx, gin.H{"question_id": questionId}, entity.Questions{}, err) | |
| return | |
| } | |
| if existing.ProblemSetId != problemSetId { | |
| ResponseJSON(ctx, gin.H{"problemset_id": problemSetId, "question_id": questionId}, entity.Questions{}, http_error.QUESTION_NOT_FOUND) | |
| return | |
| } | |
| req := RequestJSON[dto.UpdateQuestionRequest](ctx) | |
| q := entity.Questions{ | |
| Id: questionId, | |
| ProblemSetId: problemSetId, | |
| Type: req.Type, | |
| Question: req.Question, | |
| Options: pq.StringArray(req.Options), | |
| AnsKey: pq.StringArray(req.AnsKey), | |
| CorrMark: req.CorrMark, | |
| IncorrMark: req.IncorrMark, | |
| NullMark: req.NullMark, | |
| Solution: req.Solution, | |
| } | |
| err = c.problemSetService.UpdateQuestion(ctx.Request.Context(), q) | |
| if err != nil { | |
| ResponseJSON(ctx, req, entity.Questions{}, err) | |
| return | |
| } | |
| updated, err := c.problemSetService.GetQuestionById(ctx.Request.Context(), questionId) | |
| ResponseJSON(ctx, req, updated, err) | |
| } | |
| // BulkUpdateQuestions godoc | |
| // @Summary Admin: Bulk Update Questions in Problem Set | |
| // @Description Update multiple questions in a problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param id path string true "Problem Set ID" | |
| // @Param request body dto.BulkUpdateQuestionsRequest true "Bulk Update Questions Request" | |
| // @Router /api/v1/admin/problemsets/{id}/questions/bulk [put] | |
| func (c *adminProblemSetController) BulkUpdateQuestions(ctx *gin.Context) { | |
| problemSetId, err := uuid.Parse(ctx.Param("id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": ctx.Param("id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| req := RequestJSON[dto.BulkUpdateQuestionsRequest](ctx) | |
| if ctx.IsAborted() { | |
| return | |
| } | |
| questions := make([]entity.Questions, 0, len(req.Questions)) | |
| for _, item := range req.Questions { | |
| questionId, parseErr := uuid.Parse(item.Id) | |
| if parseErr != nil { | |
| ResponseJSON(ctx, gin.H{"id_question": item.Id}, []entity.Questions{}, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| questions = append(questions, entity.Questions{ | |
| Id: questionId, | |
| ProblemSetId: problemSetId, | |
| Type: item.Type, | |
| Question: item.Question, | |
| Options: pq.StringArray(item.Options), | |
| AnsKey: pq.StringArray(item.AnsKey), | |
| CorrMark: item.CorrMark, | |
| IncorrMark: item.IncorrMark, | |
| NullMark: item.NullMark, | |
| Solution: item.Solution, | |
| }) | |
| } | |
| updated, err := c.problemSetService.BulkUpdateQuestions(ctx.Request.Context(), problemSetId, questions) | |
| if err != nil { | |
| ResponseJSON(ctx, req, []entity.Questions{}, err) | |
| return | |
| } | |
| ResponseJSON(ctx, gin.H{"problemset_id": problemSetId, "count": len(updated)}, updated, nil) | |
| } | |
| // DeleteQuestion godoc | |
| // @Summary Admin: Delete Question from Problem Set | |
| // @Description Delete a question from a problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param id path string true "Problem Set ID" | |
| // @Param question_id path string true "Question ID" | |
| // @Router /api/v1/admin/problemsets/{id}/questions/{question_id} [delete] | |
| func (c *adminProblemSetController) DeleteQuestion(ctx *gin.Context) { | |
| problemSetId, err := uuid.Parse(ctx.Param("id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": ctx.Param("id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| questionId, err := uuid.Parse(ctx.Param("question_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"question_id": ctx.Param("question_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| existing, err := c.problemSetService.GetQuestionById(ctx.Request.Context(), questionId) | |
| if err != nil { | |
| ResponseJSON(ctx, gin.H{"question_id": questionId}, entity.Questions{}, err) | |
| return | |
| } | |
| if existing.ProblemSetId != problemSetId { | |
| ResponseJSON(ctx, gin.H{"problemset_id": problemSetId, "question_id": questionId}, entity.Questions{}, http_error.QUESTION_NOT_FOUND) | |
| return | |
| } | |
| delErr := c.problemSetService.DeleteQuestion(ctx.Request.Context(), questionId) | |
| ResponseJSON(ctx, gin.H{"problemset_id": problemSetId, "question_id": questionId}, gin.H{"deleted": delErr == nil}, delErr) | |
| } | |
| // GetQuestionDetail godoc | |
| // @Summary Admin: Get Question Detail in Problem Set | |
| // @Description Get a single question from a problem set | |
| // @Tags Admin ProblemSet | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param id path string true "Problem Set ID" | |
| // @Param question_id path string true "Question ID" | |
| // @Router /api/v1/admin/problemsets/{id}/questions/{question_id} [get] | |
| func (c *adminProblemSetController) GetQuestionDetail(ctx *gin.Context) { | |
| _, err := uuid.Parse(ctx.Param("id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": ctx.Param("id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| questionId, err := uuid.Parse(ctx.Param("question_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"question_id": ctx.Param("question_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| question, err := c.problemSetService.GetQuestionById(ctx.Request.Context(), questionId) | |
| if err != nil { | |
| ResponseJSON(ctx, gin.H{"question_id": questionId}, entity.Questions{}, err) | |
| return | |
| } | |
| ResponseJSON(ctx, gin.H{"question_id": questionId}, question, nil) | |
| } | |