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 AdminAcademyController interface { | |
| ListAcademies(ctx *gin.Context) | |
| CreateAcademy(ctx *gin.Context) | |
| GetAcademyDetail(ctx *gin.Context) | |
| UpdateAcademy(ctx *gin.Context) | |
| DeleteAcademy(ctx *gin.Context) | |
| ListMaterialsByAcademy(ctx *gin.Context) | |
| GetMaterialDetail(ctx *gin.Context) | |
| CreateMaterial(ctx *gin.Context) | |
| UpdateMaterial(ctx *gin.Context) | |
| DeleteMaterial(ctx *gin.Context) | |
| ListContentsByMaterial(ctx *gin.Context) | |
| GetContentDetail(ctx *gin.Context) | |
| CreateContent(ctx *gin.Context) | |
| UpdateContent(ctx *gin.Context) | |
| DeleteContent(ctx *gin.Context) | |
| ListExamsByAcademy(ctx *gin.Context) | |
| ListExamCandidatesByAcademy(ctx *gin.Context) | |
| ListContentsByAcademy(ctx *gin.Context) | |
| AssignAccountToAcademy(ctx *gin.Context) | |
| UnassignAccountFromAcademy(ctx *gin.Context) | |
| ListParticipants(ctx *gin.Context) | |
| ListCandidates(ctx *gin.Context) | |
| GetAttemptReview(ctx *gin.Context) | |
| } | |
| type adminAcademyController struct { | |
| service services.AdminAcademyService | |
| } | |
| func NewAdminAcademyController(service services.AdminAcademyService) AdminAcademyController { | |
| return &adminAcademyController{service: service} | |
| } | |
| // ListAcademies godoc | |
| // @Summary Admin: List Academies | |
| // @Description List all academies (private and public) | |
| // @Tags Admin Academy | |
| // @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 / slug / code" | |
| // @Param sortBy query string false "Sort field (title, slug, code, created_at, is_public)" | |
| // @Param orderBy query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[[]entity.Academy] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/academy [get] | |
| func (c *adminAcademyController) ListAcademies(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("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.service.ListAcademies(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) | |
| } | |
| // GetAttemptReview godoc | |
| // @Summary Admin: Review Academy Exam Attempt | |
| // @Description Review a specific participant attempt for an academy exam | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param academy_id path string true "Academy ID" | |
| // @Param attempt_id path string true "Attempt ID" | |
| // @Param user_id path string true "User ID" | |
| // @Success 200 {object} dto.SuccessResponse[dto.AdminExamReviewResponse] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/academy/{academy_id}/attempts/{attempt_id}/{user_id}/review [get] | |
| func (c *adminAcademyController) GetAttemptReview(ctx *gin.Context) { | |
| 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 | |
| } | |
| attemptId, err := uuid.Parse(ctx.Param("attempt_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"attempt_id": ctx.Param("attempt_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| userId, err := uuid.Parse(ctx.Param("user_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"user_id": ctx.Param("user_id")}, nil, http_error.INVALID_TOKEN) | |
| return | |
| } | |
| review, reviewErr := c.service.GetAttemptReview(ctx.Request.Context(), academyId, attemptId, userId) | |
| ResponseJSON(ctx, gin.H{"academy_id": academyId, "attempt_id": attemptId, "user_id": userId}, review, reviewErr) | |
| } | |
| // CreateAcademy godoc | |
| // @Summary Create Academy | |
| // @Description Create a new academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param request body dto.CreateAcademyRequest true "Create Academy Request" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy [post] | |
| func (c *adminAcademyController) CreateAcademy(ctx *gin.Context) { | |
| req := RequestJSON[dto.CreateAcademyRequest](ctx) | |
| res, err := c.service.CreateAcademy(ctx.Request.Context(), req) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |
| // GetAcademyDetail godoc | |
| // @Summary Get Academy Detail by ID | |
| // @Description Retrieve detailed academy information using its ID | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param academy_id path string true "Academy ID" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/id/{academy_id}/detail [get] | |
| func (c *adminAcademyController) GetAcademyDetail(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "academy_id") | |
| res, err := c.service.GetAcademyDetail(ctx.Request.Context(), id) | |
| ResponseJSON(ctx, gin.H{"id": id}, res, err) | |
| } | |
| // UpdateAcademy godoc | |
| // @Summary Update Academy | |
| // @Description Update an existing academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param academy_id path string true "Academy ID" | |
| // @Param request body dto.UpdateAcademyRequest true "Update Academy Request" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/id/{academy_id} [put] | |
| func (c *adminAcademyController) UpdateAcademy(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "academy_id") | |
| req := RequestJSON[dto.UpdateAcademyRequest](ctx) | |
| res, err := c.service.UpdateAcademy(ctx.Request.Context(), id, req) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |
| // DeleteAcademy godoc | |
| // @Summary Delete Academy | |
| // @Description Delete an existing academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param academy_id path string true "Academy ID" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/id/{academy_id} [delete] | |
| func (c *adminAcademyController) DeleteAcademy(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "academy_id") | |
| err := c.service.DeleteAcademy(ctx.Request.Context(), id) | |
| ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": true}, err) | |
| } | |
| // ListMaterialsByAcademy godoc | |
| // @Summary List Materials by Academy | |
| // @Description Retrieve a list of materials for a specific academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param academy_id path string true "Academy 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 title / slug / description" | |
| // @Param sortBy query string false "Sort field (title, slug, order, created_at)" | |
| // @Param orderBy query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/id/{academy_id}/materials [get] | |
| func (c *adminAcademyController) ListMaterialsByAcademy(ctx *gin.Context) { | |
| academyId := ParseUUID(ctx, "academy_id") | |
| 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("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.service.ListMaterialsByAcademy(ctx.Request.Context(), academyId, 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{ | |
| "academy_id": academyId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // GetMaterialDetail godoc | |
| // @Summary Get Material Detail by ID | |
| // @Description Retrieve detailed material information using its ID | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param material_id path string true "Material ID" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/materials/id/{material_id}/detail [get] | |
| func (c *adminAcademyController) GetMaterialDetail(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "material_id") | |
| res, err := c.service.GetMaterialDetail(ctx.Request.Context(), id) | |
| ResponseJSON(ctx, gin.H{"id": id}, res, err) | |
| } | |
| // CreateMaterial godoc | |
| // @Summary Create Material | |
| // @Description Create a new material for an academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param request body dto.CreateMaterialRequest true "Create Material Request" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/materials [post] | |
| func (c *adminAcademyController) CreateMaterial(ctx *gin.Context) { | |
| req := RequestJSON[dto.CreateMaterialRequest](ctx) | |
| res, err := c.service.CreateMaterial(ctx.Request.Context(), req) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |
| // UpdateMaterial godoc | |
| // @Summary Update Material | |
| // @Description Update an existing material | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param material_id path string true "Material ID" | |
| // @Param request body dto.UpdateMaterialRequest true "Update Material Request" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/materials/id/{material_id} [put] | |
| func (c *adminAcademyController) UpdateMaterial(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "material_id") | |
| req := RequestJSON[dto.UpdateMaterialRequest](ctx) | |
| res, err := c.service.UpdateMaterial(ctx.Request.Context(), id, req) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |
| // DeleteMaterial godoc | |
| // @Summary Delete Material | |
| // @Description Delete an existing material | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param material_id path string true "Material ID" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/materials/id/{material_id} [delete] | |
| func (c *adminAcademyController) DeleteMaterial(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "material_id") | |
| err := c.service.DeleteMaterial(ctx.Request.Context(), id) | |
| ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": true}, err) | |
| } | |
| // ListContentsByMaterial godoc | |
| // @Summary List Contents by Material | |
| // @Description Retrieve a list of contents for a specific material | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param material_id path string true "Material 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 title / contents" | |
| // @Param sortBy query string false "Sort field (title, order, created_at)" | |
| // @Param orderBy query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/materials/id/{material_id}/contents [get] | |
| func (c *adminAcademyController) ListContentsByMaterial(ctx *gin.Context) { | |
| materialId := ParseUUID(ctx, "material_id") | |
| 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("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.service.ListContentsByMaterial(ctx.Request.Context(), materialId, 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{ | |
| "material_id": materialId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // GetContentDetail godoc | |
| // @Summary Get Content Detail by ID | |
| // @Description Retrieve detailed content information using its ID | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param content_id path string true "Content ID" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/contents/id/{content_id}/detail [get] | |
| func (c *adminAcademyController) GetContentDetail(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "content_id") | |
| res, err := c.service.GetContentDetail(ctx.Request.Context(), id) | |
| ResponseJSON(ctx, gin.H{"id": id}, res, err) | |
| } | |
| // CreateContent godoc | |
| // @Summary Create Content | |
| // @Description Create a new content for a material | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param request body dto.CreateContentRequest true "Create Content Request" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/contents [post] | |
| func (c *adminAcademyController) CreateContent(ctx *gin.Context) { | |
| req := RequestJSON[dto.CreateContentRequest](ctx) | |
| res, err := c.service.CreateContent(ctx.Request.Context(), req) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |
| // UpdateContent godoc | |
| // @Summary Update Content | |
| // @Description Update an existing content | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param content_id path string true "Content ID" | |
| // @Param request body dto.UpdateContentRequest true "Update Content Request" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/contents/id/{content_id} [put] | |
| func (c *adminAcademyController) UpdateContent(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "content_id") | |
| req := RequestJSON[dto.UpdateContentRequest](ctx) | |
| res, err := c.service.UpdateContent(ctx.Request.Context(), id, req) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |
| // DeleteContent godoc | |
| // @Summary Delete Content | |
| // @Description Delete an existing content | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param content_id path string true "Content ID" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/contents/id/{content_id} [delete] | |
| func (c *adminAcademyController) DeleteContent(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "content_id") | |
| err := c.service.DeleteContent(ctx.Request.Context(), id) | |
| ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": true}, err) | |
| } | |
| // AssignAccountToAcademy godoc | |
| // @Summary Assign Account to Academy | |
| // @Description Assign an account to an academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param request body dto.AssignRequest true "Assign Account to Academy Request" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/assign [post] | |
| func (c *adminAcademyController) AssignAccountToAcademy(ctx *gin.Context) { | |
| req := RequestJSON[dto.AssignRequest](ctx) | |
| academyId, errA := uuid.Parse(req.AcademyId) | |
| accountId, errB := uuid.Parse(req.AccountId) | |
| if errA != nil || errB != nil { | |
| ResponseJSON[any, any](ctx, nil, nil, http_error.BAD_REQUEST_ERROR) | |
| return | |
| } | |
| res, err := c.service.AssignAccountToAcademy(ctx.Request.Context(), academyId, accountId) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |
| // UnassignAccountFromAcademy godoc | |
| // @Summary Unassign Account from Academy | |
| // @Description Unassign an account from an academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Param assignment_id path string true "Assignment ID" | |
| // @Success 200 {object} dto.SuccessResponse[any] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/admin/academy/assign/{assignment_id} [delete] | |
| func (c *adminAcademyController) UnassignAccountFromAcademy(ctx *gin.Context) { | |
| id := ParseUUID(ctx, "assignment_id") | |
| err := c.service.UnassignAccountFromAcademy(ctx.Request.Context(), id) | |
| ResponseJSON(ctx, gin.H{"id": id}, gin.H{"deleted": true}, err) | |
| } | |
| // ListParticipants godoc | |
| // @Summary Admin: List Academy Participants | |
| // @Description List all participants assigned to an academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param academy_id query string true "Academy 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 username / full name" | |
| // @Param sortBy query string false "Sort field (created_at, username, email, full_name)" | |
| // @Param orderBy query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[[]entity.AcademyAssign] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/academy/participants [get] | |
| func (c *adminAcademyController) ListParticipants(ctx *gin.Context) { | |
| academyId, err := uuid.Parse(ctx.Query("academy_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"academy_id": ctx.Query("academy_id")}, nil, http_error.BAD_REQUEST_ERROR) | |
| 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("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.service.ListParticipants(ctx.Request.Context(), academyId, 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{ | |
| "academy_id": academyId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // ListCandidates godoc | |
| // @Summary Admin: List Academy Candidates | |
| // @Description List accounts that are not yet assigned to an academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param academy_id query string true "Academy 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 username / full name" | |
| // @Param sortBy query string false "Sort field (username, full_name, created_at)" | |
| // @Param orderBy query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[[]entity.Account] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/academy/candidates [get] | |
| func (c *adminAcademyController) ListCandidates(ctx *gin.Context) { | |
| academyId, err := uuid.Parse(ctx.Query("academy_id")) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"academy_id": ctx.Query("academy_id")}, nil, http_error.BAD_REQUEST_ERROR) | |
| 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("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.service.ListCandidates(ctx.Request.Context(), academyId, 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{ | |
| "academy_id": academyId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // ListExamsByAcademy godoc | |
| // @Summary Admin: List Exams by Academy | |
| // @Description List all exams assigned to a specific academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param academy_id path string true "Academy 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 title / slug / description" | |
| // @Param sortBy query string false "Sort field (title, slug, duration, created_at)" | |
| // @Param orderBy query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[[]entity.Exam] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/academy/id/{academy_id}/exams [get] | |
| func (c *adminAcademyController) ListExamsByAcademy(ctx *gin.Context) { | |
| academyId := ParseUUID(ctx, "academy_id") | |
| 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("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.service.ListExamsByAcademy(ctx.Request.Context(), academyId, 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{ | |
| "academy_id": academyId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // ListExamCandidatesByAcademy godoc | |
| // @Summary Admin: List Exam Candidates by Academy | |
| // @Description List all exams NOT assigned to a specific academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param academy_id path string true "Academy 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 title / slug / description" | |
| // @Param sortBy query string false "Sort field (title, slug, duration, created_at)" | |
| // @Param orderBy query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[[]entity.Exam] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/academy/id/{academy_id}/exam/candidates [get] | |
| func (c *adminAcademyController) ListExamCandidatesByAcademy(ctx *gin.Context) { | |
| academyId := ParseUUID(ctx, "academy_id") | |
| 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("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.service.ListExamCandidatesByAcademy(ctx.Request.Context(), academyId, 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{ | |
| "academy_id": academyId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |
| // ListContentsByAcademy godoc | |
| // @Summary Admin: List Contents by Academy | |
| // @Description List all contents in a specific academy | |
| // @Tags Admin Academy | |
| // @Accept json | |
| // @Produce json | |
| // @Security BearerAuth | |
| // @Param academy_id path string true "Academy 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 title / contents / material_title" | |
| // @Param sortBy query string false "Sort field (title, order, created_at, material_title)" | |
| // @Param orderBy query string false "Sort direction (asc / desc)" | |
| // @Success 200 {object} dto.SuccessResponse[[]entity.AcademyContent] | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Router /api/v1/admin/academy/id/{academy_id}/contents [get] | |
| func (c *adminAcademyController) ListContentsByAcademy(ctx *gin.Context) { | |
| academyId := ParseUUID(ctx, "academy_id") | |
| 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("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.service.ListContentsByAcademy(ctx.Request.Context(), academyId, 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{ | |
| "academy_id": academyId, | |
| "totalItems": total, | |
| "totalPages": totalPages, | |
| "currentPage": page, | |
| "limit": limit, | |
| } | |
| ResponseJSON(ctx, meta, list, nil) | |
| } | |