Spaces:
Sleeping
Sleeping
| package controllers | |
| import ( | |
| "strconv" | |
| "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" | |
| ) | |
| type AdminExamController interface { | |
| CreateExam(ctx *gin.Context) | |
| UpdateExam(ctx *gin.Context) | |
| DeleteExam(ctx *gin.Context) | |
| GetExamDetail(ctx *gin.Context) | |
| ListExams(ctx *gin.Context) | |
| AssignToEvent(ctx *gin.Context) | |
| UnassignFromEvent(ctx *gin.Context) | |
| AssignToAcademy(ctx *gin.Context) | |
| UnassignFromAcademy(ctx *gin.Context) | |
| ListEventsByExam(ctx *gin.Context) | |
| ListAcademiesByExam(ctx *gin.Context) | |
| } | |
| type adminExamController struct { | |
| adminExamService services.AdminExamService | |
| } | |
| func NewAdminExamController(adminExamService services.AdminExamService) AdminExamController { | |
| return &adminExamController{adminExamService: adminExamService} | |
| } | |
| // CreateExam godoc | |
| // @Summary Admin: Create Exam | |
| // @Description Create a new exam with configuration and proctoring settings | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param request body dto.CreateExamRequest true "Create Exam Request" | |
| // @Success 200 {object} dto.SuccessResponse[entity.Exam] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams [post] | |
| func (c *adminExamController) CreateExam(ctx *gin.Context) { | |
| req := RequestJSON[dto.CreateExamRequest](ctx) | |
| res, err := c.adminExamService.CreateExam(ctx.Request.Context(), req) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |
| // UpdateExam godoc | |
| // @Summary Admin: Update Exam | |
| // @Description Update an existing exam including configuration and proctoring settings | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Param request body dto.CreateExamRequest true "Update Exam Request" | |
| // @Success 200 {object} dto.SuccessResponse[entity.Exam] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams/{exam_id} [put] | |
| func (c *adminExamController) UpdateExam(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 | |
| } | |
| req := RequestJSON[dto.CreateExamRequest](ctx) | |
| res, updateErr := c.adminExamService.UpdateExam(ctx.Request.Context(), examId, req) | |
| ResponseJSON(ctx, req, res, updateErr) | |
| } | |
| // DeleteExam godoc | |
| // @Summary Admin: Delete Exam | |
| // @Description Soft delete an exam by ID | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Success 200 {object} dto.SuccessResponse[map[string]bool] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams/{exam_id} [delete] | |
| func (c *adminExamController) DeleteExam(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 | |
| } | |
| delErr := c.adminExamService.DeleteExam(ctx.Request.Context(), examId) | |
| ResponseJSON(ctx, gin.H{"exam_id": examId}, gin.H{"deleted": delErr == nil}, delErr) | |
| } | |
| // GetExamDetail godoc | |
| // @Summary Admin: Get Exam Detail | |
| // @Description Retrieve full exam details including configuration and proctoring | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Success 200 {object} dto.SuccessResponse[entity.Exam] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams/{exam_id} [get] | |
| func (c *adminExamController) GetExamDetail(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 | |
| } | |
| res, getErr := c.adminExamService.GetExamDetail(ctx.Request.Context(), examId) | |
| ResponseJSON(ctx, gin.H{"exam_id": examId}, res, getErr) | |
| } | |
| // ListExams godoc | |
| // @Summary Admin: List Exams | |
| // @Description Retrieve a paginated list of exams with event and academy assignment counts | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param limit query int false "Items per page" default(10) | |
| // @Param page query int false "Page number" default(1) | |
| // @Param search query string false "Search by title or slug" | |
| // @Param sortBy query string false "Sort field (title, slug, created_at, duration, event_count, academy_count)" | |
| // @Param order query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[[]dto.AdminExamResponse] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams [get] | |
| func (c *adminExamController) ListExams(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", "") | |
| 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.adminExamService.ListExams(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) | |
| } | |
| // AssignToEvent godoc | |
| // @Summary Admin: Assign Exam to Event | |
| // @Description Assign an exam to an event | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Param event_id path string true "Event ID" | |
| // @Success 200 {object} dto.SuccessResponse[map[string]bool] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams/{exam_id}/events/{event_id} [post] | |
| func (c *adminExamController) AssignToEvent(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 | |
| } | |
| eventId, err := uuid.Parse(ctx.Param("event_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| assignErr := c.adminExamService.AssignToEvent(ctx.Request.Context(), examId, eventId) | |
| ResponseJSON(ctx, gin.H{"exam_id": examId, "event_id": eventId}, gin.H{"assigned": assignErr == nil}, assignErr) | |
| } | |
| // UnassignFromEvent godoc | |
| // @Summary Admin: Unassign Exam from Event | |
| // @Description Remove an exam assignment from an event | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Param event_id path string true "Event ID" | |
| // @Success 200 {object} dto.SuccessResponse[map[string]bool] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams/{exam_id}/events/{event_id} [delete] | |
| func (c *adminExamController) UnassignFromEvent(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 | |
| } | |
| eventId, err := uuid.Parse(ctx.Param("event_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"event_id": ctx.Param("event_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| delErr := c.adminExamService.UnassignFromEvent(ctx.Request.Context(), examId, eventId) | |
| ResponseJSON(ctx, gin.H{"exam_id": examId, "event_id": eventId}, gin.H{"removed": delErr == nil}, delErr) | |
| } | |
| // AssignToAcademy godoc | |
| // @Summary Admin: Assign Exam to Academy | |
| // @Description Assign an exam to an academy | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Param academy_id path string true "Academy ID" | |
| // @Success 200 {object} dto.SuccessResponse[map[string]bool] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams/{exam_id}/academies/{academy_id} [post] | |
| func (c *adminExamController) AssignToAcademy(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 | |
| } | |
| academyId, err := uuid.Parse(ctx.Param("academy_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"academy_id": ctx.Param("academy_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| assignErr := c.adminExamService.AssignToAcademy(ctx.Request.Context(), examId, academyId) | |
| ResponseJSON(ctx, gin.H{"exam_id": examId, "academy_id": academyId}, gin.H{"assigned": assignErr == nil}, assignErr) | |
| } | |
| // UnassignFromAcademy godoc | |
| // @Summary Admin: Unassign Exam from Academy | |
| // @Description Remove an exam assignment from an academy | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Param academy_id path string true "Academy ID" | |
| // @Success 200 {object} dto.SuccessResponse[map[string]bool] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams/{exam_id}/academies/{academy_id} [delete] | |
| func (c *adminExamController) UnassignFromAcademy(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 | |
| } | |
| academyId, err := uuid.Parse(ctx.Param("academy_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"academy_id": ctx.Param("academy_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| delErr := c.adminExamService.UnassignFromAcademy(ctx.Request.Context(), examId, academyId) | |
| ResponseJSON(ctx, gin.H{"exam_id": examId, "academy_id": academyId}, gin.H{"removed": delErr == nil}, delErr) | |
| } | |
| // ListEventsByExam godoc | |
| // @Summary Admin: List Events by Exam | |
| // @Description List all events that have this exam assigned | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Param limit query int false "Items per page" default(10) | |
| // @Param page query int false "Page number" default(1) | |
| // @Param search query string false "Search by event title / slug / event code" | |
| // @Param sortBy query string false "Sort field (title, slug, event_code, start_event, end_event, created_at)" | |
| // @Param order query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[[]entity.EventExamAssign] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams/{exam_id}/events [get] | |
| func (c *adminExamController) ListEventsByExam(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.adminExamService.ListEventsByExam(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) | |
| } | |
| // ListAcademiesByExam godoc | |
| // @Summary Admin: List Academies by Exam | |
| // @Description List all academies that have this exam assigned | |
| // @Tags AdminExam | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param exam_id path string true "Exam ID" | |
| // @Param limit query int false "Items per page" default(10) | |
| // @Param page query int false "Page number" default(1) | |
| // @Param search query string false "Search by academy title / slug / code" | |
| // @Param sortBy query string false "Sort field (title, slug, code, created_at)" | |
| // @Param order query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[[]entity.AcademyExamAssign] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/exams/{exam_id}/academies [get] | |
| func (c *adminExamController) ListAcademiesByExam(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.adminExamService.ListAcademiesByExam(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) | |
| } | |