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 UserController interface { | |
| GetProfile(ctx *gin.Context) | |
| GetUserDetails(ctx *gin.Context) | |
| UpdateAboutYou(ctx *gin.Context) | |
| AssignRoles(ctx *gin.Context) | |
| } | |
| type userController struct { | |
| userService services.UserService | |
| } | |
| func NewUserController(userService services.UserService) UserController { | |
| return &userController{ | |
| userService: userService, | |
| } | |
| } | |
| // GetProfile godoc | |
| // @Summary Get My Profile | |
| // @Description Get current logged-in user profile, including roles and about details. | |
| // @Tags Users | |
| // @Accept json | |
| // @Produce json | |
| // @Success 200 {object} dto.UserProfileResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/users/me [get] | |
| func (c *userController) GetProfile(ctx *gin.Context) { | |
| userId := ParseUserId(ctx) | |
| res, err := c.userService.GetProfile(ctx.Request.Context(), userId) | |
| ResponseJSON(ctx, "", res, err) | |
| } | |
| // GetUserDetails godoc | |
| // @Summary Get User Details | |
| // @Description Get public profile and roles of another user by their ID. | |
| // @Tags Users | |
| // @Accept json | |
| // @Produce json | |
| // @Param id path string true "User ID" | |
| // @Success 200 {object} dto.UserProfileResponse | |
| // @Failure 404 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/users/{id} [get] | |
| func (c *userController) GetUserDetails(ctx *gin.Context) { | |
| userId := ctx.Param("id") | |
| res, err := c.userService.GetProfile(ctx.Request.Context(), userId) | |
| ResponseJSON(ctx, "", res, err) | |
| } | |
| // UpdateAboutYou godoc | |
| // @Summary Update About You | |
| // @Description Update user's personal details like gender, dance level, and description. | |
| // @Tags Users | |
| // @Accept json | |
| // @Produce json | |
| // @Param request body dto.UpdateAboutYouRequest true "Update Profile Data" | |
| // @Success 200 {object} dto.UserProfileResponse | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/users/me/about [put] | |
| func (c *userController) UpdateAboutYou(ctx *gin.Context) { | |
| req, err := RequestJSON[dto.UpdateAboutYouRequest](ctx) | |
| if err != nil { | |
| return | |
| } | |
| userId := ParseUserId(ctx) | |
| if userId == "" { | |
| return | |
| } | |
| res, err := c.userService.UpdateAboutYou(ctx.Request.Context(), userId, req) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |
| // AssignRoles godoc | |
| // @Summary Assign Role | |
| // @Description Update user's primary role (member, instructor, venue_owner). | |
| // @Tags Users | |
| // @Accept json | |
| // @Produce json | |
| // @Param request body dto.UpdateUserRolesRequest true "Role Assignment Data" | |
| // @Success 200 {object} dto.UserProfileResponse | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/users/me/roles [put] | |
| func (c *userController) AssignRoles(ctx *gin.Context) { | |
| req, err := RequestJSON[dto.UpdateUserRolesRequest](ctx) | |
| if err != nil { | |
| return | |
| } | |
| userId := ParseUserId(ctx) | |
| if userId == "" { | |
| return | |
| } | |
| res, err := c.userService.AssignRoles(ctx.Request.Context(), userId, req.Role) | |
| ResponseJSON(ctx, req, res, err) | |
| } | |