Skip to content

Commit 83c81aa

Browse files
committed
refactor: update GetUserProfile to use user data from context and enhance FindById query
1 parent d8ee513 commit 83c81aa

2 files changed

Lines changed: 4 additions & 21 deletions

File tree

app/users/delivery/http/user.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"net/http"
55
"strconv"
66

7-
"github.com/hammer-code/lms-be/config"
87
"github.com/hammer-code/lms-be/domain"
9-
"github.com/hammer-code/lms-be/pkg/jwt"
8+
contextkey "github.com/hammer-code/lms-be/pkg/context_key"
109
"github.com/hammer-code/lms-be/utils"
1110
"github.com/sirupsen/logrus"
1211
)
@@ -103,25 +102,9 @@ func (h Handler) GetUserById(w http.ResponseWriter, r *http.Request) {
103102
// @Success 200 {object} domain.User
104103
// @Router /api/v1/users [get]
105104
func (h Handler) GetUserProfile(w http.ResponseWriter, r *http.Request) {
106-
authorizationHeader := r.Header.Get("Authorization")
107-
if authorizationHeader == "" {
108-
utils.Response(domain.HttpResponse{
109-
Code: 401,
110-
Message: "Not permission",
111-
}, w)
112-
return
113-
}
114-
115-
claims, err := jwt.ParseToken(authorizationHeader, config.GetConfig().JWT_SECRET_KEY)
116-
if err != nil {
117-
utils.Response(domain.HttpResponse{
118-
Code: 500,
119-
Message: err.Error(),
120-
}, w)
121-
return
122-
}
105+
userData := r.Context().Value(contextkey.UserKey).(domain.User)
123106

124-
user, err := h.usecase.GetUserById(r.Context(), int8(claims.ID))
107+
user, err := h.usecase.GetUserById(r.Context(), int8(userData.ID))
125108

126109
if err != nil {
127110
logrus.Error("userUsecase: failed to get user")

app/users/repository/find_users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (repo *repository) FindByEmail(ctx context.Context, email string) (user dom
1717
}
1818

1919
func (repo *repository) FindById(ctx context.Context, id int8) (user domain.User, err error) {
20-
err = repo.db.DB(ctx).Where("id = ?", id).Take(&user).Error
20+
err = repo.db.DB(ctx).Select("id", "username", "email", "role", "fullname", "date_of_birth", "gender", "phone_number", "address", "github", "linkedin", "personal_web").Where("id = ?", id).Take(&user).Error
2121
if err != nil {
2222
logrus.Error("repo.FindById: failed to find user")
2323
return

0 commit comments

Comments
 (0)