File size: 3,433 Bytes
d8cdd09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12690b4
 
 
 
 
 
 
 
 
 
d8cdd09
 
 
 
 
 
12690b4
 
 
 
 
 
 
 
 
 
 
d8cdd09
 
 
 
 
 
12690b4
 
 
 
 
 
 
 
 
 
 
 
d8cdd09
4a66f20
 
 
 
d8cdd09
4a66f20
 
 
d8cdd09
 
 
 
12690b4
 
 
 
 
 
 
 
 
 
 
 
d8cdd09
4a66f20
 
 
 
d8cdd09
4a66f20
 
 
d8cdd09
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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)
}