Spaces:
Configuration error
Configuration error
| package academy_controller | |
| import ( | |
| "net/http" | |
| "strconv" | |
| "api.qobiltu.id/middleware" | |
| "api.qobiltu.id/models" | |
| "api.qobiltu.id/response" | |
| "api.qobiltu.id/services" | |
| "github.com/gin-gonic/gin" | |
| ) | |
| type AcademyController interface { | |
| // === ADMIN === | |
| AdminCreateAcademy(ctx *gin.Context) | |
| AdminGetAcademy(ctx *gin.Context) | |
| AdminUpdateAcademy(ctx *gin.Context) | |
| AdminDeleteAcademy(ctx *gin.Context) | |
| AdminListAcademy(ctx *gin.Context) | |
| AdminReorderAcademy(ctx *gin.Context) | |
| AdminCreateAcademyMaterial(ctx *gin.Context) | |
| AdminGetAcademyMaterial(ctx *gin.Context) | |
| AdminUpdateAcademyMaterial(ctx *gin.Context) | |
| AdminDeleteAcademyMaterial(ctx *gin.Context) | |
| AdminListAcademyMaterial(ctx *gin.Context) | |
| AdminReorderAcademyMaterial(ctx *gin.Context) | |
| // === USER === | |
| UserListAcademy(ctx *gin.Context) | |
| UserListAcademyMaterial(ctx *gin.Context) | |
| UserGetAcademyMaterialBySlug(ctx *gin.Context) | |
| UserToggleAcademyMaterialProgress(ctx *gin.Context) | |
| } | |
| type academyController struct { | |
| academyService services.AcademyService | |
| } | |
| func NewAcademyController(academyService services.AcademyService) AcademyController { | |
| return &academyController{ | |
| academyService: academyService, | |
| } | |
| } | |
| func (c *academyController) AdminListAcademy(ctx *gin.Context) { | |
| req := models.NewListAcademyRequest() | |
| if err := ctx.ShouldBindQuery(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| res, paging, err := c.academyService.AdminListAcademy(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy list retrieved successfully", res, paging) | |
| } | |
| func (c *academyController) AdminCreateAcademy(ctx *gin.Context) { | |
| var req models.CreateAcademyRequest | |
| if err := ctx.ShouldBindJSON(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| accountData := middleware.GetAccountData(ctx) | |
| req.AccountID = int64(accountData.UserID) | |
| res, err := c.academyService.AdminCreateAcademy(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy created successfully", res, nil) | |
| } | |
| func (c *academyController) AdminGetAcademy(ctx *gin.Context) { | |
| id := ctx.Param("id") | |
| idInt, err := strconv.ParseInt(id, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| res, err := c.academyService.AdminGetAcademyByID(ctx, idInt) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy retrieved successfully", res, nil) | |
| } | |
| func (c *academyController) AdminUpdateAcademy(ctx *gin.Context) { | |
| id := ctx.Param("id") | |
| idInt, err := strconv.ParseInt(id, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| var req models.UpdateAcademyRequest | |
| if err := ctx.ShouldBindJSON(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| req.ID = idInt | |
| res, err := c.academyService.AdminUpdateAcademy(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy updated successfully", res, nil) | |
| } | |
| func (c *academyController) AdminDeleteAcademy(ctx *gin.Context) { | |
| id := ctx.Param("id") | |
| idInt, err := strconv.ParseInt(id, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| err = c.academyService.AdminDeleteAcademy(ctx, idInt) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy deleted successfully", nil, nil) | |
| } | |
| func (c *academyController) AdminReorderAcademy(ctx *gin.Context) { | |
| var req models.ReorderAcademyRequest | |
| if err := ctx.ShouldBindJSON(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| err := c.academyService.AdminReorderAcademy(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy reordered successfully", nil, nil) | |
| } | |
| func (c *academyController) AdminCreateAcademyMaterial(ctx *gin.Context) { | |
| var req models.CreateAcademyMaterialRequest | |
| if err := ctx.ShouldBindJSON(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| academyID := ctx.Param("id") | |
| academyIDInt, err := strconv.ParseInt(academyID, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| req.AcademyID = academyIDInt | |
| res, err := c.academyService.AdminCreateAcademyMaterial(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy material created successfully", res, nil) | |
| } | |
| func (c *academyController) AdminGetAcademyMaterial(ctx *gin.Context) { | |
| id := ctx.Param("materialId") | |
| idInt, err := strconv.ParseInt(id, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| res, err := c.academyService.AdminGetAcademyMaterialByID(ctx, idInt) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy material retrieved successfully", res, nil) | |
| } | |
| func (c *academyController) AdminUpdateAcademyMaterial(ctx *gin.Context) { | |
| id := ctx.Param("materialId") | |
| idInt, err := strconv.ParseInt(id, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| var req models.UpdateAcademyMaterialRequest | |
| if err := ctx.ShouldBindJSON(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| req.ID = idInt | |
| res, err := c.academyService.AdminUpdateAcademyMaterial(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy material updated successfully", res, nil) | |
| } | |
| func (c *academyController) AdminDeleteAcademyMaterial(ctx *gin.Context) { | |
| id := ctx.Param("materialId") | |
| idInt, err := strconv.ParseInt(id, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| err = c.academyService.AdminDeleteAcademyMaterial(ctx, idInt) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy material deleted successfully", nil, nil) | |
| } | |
| func (c *academyController) AdminReorderAcademyMaterial(ctx *gin.Context) { | |
| req := models.ReorderAcademyMaterialRequest{} | |
| if err := ctx.ShouldBindJSON(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| academyID := ctx.Param("id") | |
| academyIDInt, err := strconv.ParseInt(academyID, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| req.AcademyID = academyIDInt | |
| err = c.academyService.AdminReorderAcademyMaterial(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy material reordered successfully", nil, nil) | |
| } | |
| func (c *academyController) AdminListAcademyMaterial(ctx *gin.Context) { | |
| req := models.NewListAcademyMaterialRequest() | |
| if err := ctx.ShouldBindQuery(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| academyID := ctx.Param("id") | |
| academyIDInt, err := strconv.ParseInt(academyID, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| accountData := middleware.GetAccountData(ctx) | |
| req.AccountID = int64(accountData.UserID) | |
| req.AcademyID = academyIDInt | |
| res, paging, err := c.academyService.AdminListAcademyMaterial(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy material list retrieved successfully", res, paging) | |
| } | |
| // === USER === | |
| func (c *academyController) UserListAcademy(ctx *gin.Context) { | |
| req := models.NewListAcademyRequest() | |
| if err := ctx.ShouldBindQuery(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| accountData := middleware.GetAccountData(ctx) | |
| req.AccountID = int64(accountData.UserID) | |
| res, paging, err := c.academyService.UserListAcademy(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy list retrieved successfully", res, paging) | |
| } | |
| func (c *academyController) UserListAcademyMaterial(ctx *gin.Context) { | |
| req := models.NewListAcademyMaterialRequest() | |
| if err := ctx.ShouldBindQuery(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| accountData := middleware.GetAccountData(ctx) | |
| req.AccountID = int64(accountData.UserID) | |
| slug := ctx.Param("slug") | |
| req.Slug = slug | |
| res, paging, err := c.academyService.UserListAcademyMaterial(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy material list retrieved successfully", res, paging) | |
| } | |
| func (c *academyController) UserGetAcademyMaterialBySlug(ctx *gin.Context) { | |
| slug := ctx.Param("materialSlug") | |
| accountData := middleware.GetAccountData(ctx) | |
| accountID := int64(accountData.UserID) | |
| res, err := c.academyService.UserGetAcademyMaterialBySlug(ctx, slug, accountID) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy material retrieved successfully", res, nil) | |
| } | |
| func (c *academyController) UserToggleAcademyMaterialProgress(ctx *gin.Context) { | |
| var req models.ToggleAcademyMaterialProgressRequest | |
| if err := ctx.ShouldBindJSON(&req); err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| academySlug := ctx.Param("slug") | |
| materialSlug := ctx.Param("materialSlug") | |
| accountData := middleware.GetAccountData(ctx) | |
| accountID := int64(accountData.UserID) | |
| req.AccountID = accountID | |
| req.AcademySlug = academySlug | |
| req.MaterialSlug = materialSlug | |
| err := c.academyService.UserToggleAcademyMaterialProgress(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Academy material progress toggled successfully", nil, nil) | |
| } | |