Spaces:
Runtime error
Runtime error
| package controllers | |
| import ( | |
| "abdanhafidz.com/go-boilerplate/models/dto" | |
| "abdanhafidz.com/go-boilerplate/services" | |
| "github.com/gin-gonic/gin" | |
| ) | |
| type ClassController interface { | |
| GetClassDetail(ctx *gin.Context) | |
| CreateClass(ctx *gin.Context) | |
| UpdateClass(ctx *gin.Context) | |
| DeleteClass(ctx *gin.Context) | |
| } | |
| type classController struct { | |
| classService services.ClassService | |
| } | |
| func NewClassController(classService services.ClassService) ClassController { | |
| return &classController{classService: classService} | |
| } | |
| // GetClassDetail godoc | |
| // @Summary Get Class Details | |
| // @Description Return comprehensive class information needed to render the detail page. | |
| // @Tags Classes | |
| // @Accept json | |
| // @Produce json | |
| // @Param class_id path string true "Class ID" | |
| // @Success 200 {object} dto.ClassDetailResponse | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 404 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/classes/{class_id} [get] | |
| func (c *classController) GetClassDetail(ctx *gin.Context) { | |
| classID := ctx.Param("class_id") | |
| userID := ParseUserId(ctx) | |
| // Validate path parameter | |
| params, err := dto.ValidateClassDetailParams(classID) | |
| if err != nil { | |
| ResponseJSON(ctx, "", dto.ClassDetailResponse{}, err) | |
| return | |
| } | |
| res, err := c.classService.GetClassDetail(ctx.Request.Context(), params, userID) | |
| ResponseJSON(ctx, "", res, err) | |
| } | |
| // CreateClass godoc | |
| // @Summary Host a Class | |
| // @Description Allows an instructor to create/host a new class. Automatically creates an instructor profile if one doesn't exist. | |
| // @Tags Classes | |
| // @Accept json | |
| // @Produce json | |
| // @Param request body dto.CreateClassRequest true "Class Creation Data" | |
| // @Success 201 {object} entities.Class | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/classes [post] | |
| func (c *classController) CreateClass(ctx *gin.Context) { | |
| req, err := RequestJSON[dto.CreateClassRequest](ctx) | |
| if err != nil { | |
| return | |
| } | |
| userID := ParseUserId(ctx) | |
| if userID == "" { | |
| return | |
| } | |
| res, err := c.classService.HostClass(ctx.Request.Context(), userID, req) | |
| if err != nil { | |
| ResponseJSON(ctx, req, res, err) | |
| return | |
| } | |
| ctx.JSON(201, gin.H{ | |
| "success": true, | |
| "message": "Class created successfully", | |
| "data": res, | |
| }) | |
| } | |
| // UpdateClass godoc | |
| // @Summary Update a Class | |
| // @Description Update details of an existing class | |
| // @Tags Classes | |
| // @Accept json | |
| // @Produce json | |
| // @Param class_id path string true "Class ID" | |
| // @Param request body dto.UpdateClassRequest true "Class Update Data" | |
| // @Success 200 {object} entities.Class | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 404 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/classes/{class_id} [put] | |
| func (c *classController) UpdateClass(ctx *gin.Context) { | |
| classID := ctx.Param("class_id") | |
| if classID == "" { | |
| ctx.JSON(400, gin.H{"error": "class_id is required"}) | |
| return | |
| } | |
| req, err := RequestJSON[dto.UpdateClassRequest](ctx) | |
| if err != nil { | |
| return | |
| } | |
| res, err := c.classService.UpdateClass(ctx.Request.Context(), classID, req) | |
| if err != nil { | |
| ResponseJSON(ctx, req, res, err) | |
| return | |
| } | |
| ctx.JSON(200, gin.H{ | |
| "success": true, | |
| "message": "Class updated successfully", | |
| "data": res, | |
| }) | |
| } | |
| // DeleteClass godoc | |
| // @Summary Delete a Class | |
| // @Description Delete an existing class | |
| // @Tags Classes | |
| // @Accept json | |
| // @Produce json | |
| // @Param class_id path string true "Class ID" | |
| // @Success 200 {object} map[string]interface{} | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 404 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/classes/{class_id} [delete] | |
| func (c *classController) DeleteClass(ctx *gin.Context) { | |
| classID := ctx.Param("class_id") | |
| if classID == "" { | |
| ctx.JSON(400, gin.H{"error": "class_id is required"}) | |
| return | |
| } | |
| err := c.classService.DeleteClass(ctx.Request.Context(), classID) | |
| if err != nil { | |
| ctx.JSON(400, gin.H{"success": false, "message": err.Error()}) | |
| return | |
| } | |
| ctx.JSON(200, gin.H{"success": true, "message": "Class deleted successfully"}) | |
| } | |