From 33e6a158a2e605f50ef02c7ddc30761ae7e4eb26 Mon Sep 17 00:00:00 2001 From: Wu Changjun Date: Sat, 11 Nov 2023 00:32:46 +0800 Subject: [PATCH 1/3] add update user in apigateway --- backend/api-gateway/handlers/constants.go | 1 + backend/api-gateway/handlers/user.go | 43 +++ backend/api-gateway/main.go | 1 + backend/api-gateway/models/user.go | 19 + backend/api-gateway/utils/path/path.go | 1 + .../user-service/handlers/users_handlers.go | 2 +- backend/user-service/main.go | 2 +- frontend/app/profile/page.tsx | 332 +++++++++--------- 8 files changed, 236 insertions(+), 165 deletions(-) diff --git a/backend/api-gateway/handlers/constants.go b/backend/api-gateway/handlers/constants.go index 20c293a..260f499 100644 --- a/backend/api-gateway/handlers/constants.go +++ b/backend/api-gateway/handlers/constants.go @@ -21,6 +21,7 @@ const ( SUCCESS_USER_FOUND = "User Found!" SUCCESS_USER_CREATED = "User Created Successfully!" SUCCESS_USER_DELETED = "User Deleted Successfully!" + SUCCESS_USER_UPDATED = "User Updated Successfully!" SUCCESS_LOGIN = "Login Successfully!" SUCCESS_LOGOUT = "Logout Successfully!" SUCCESS_ROLE_UPGRADED = "User Role Upgraded Successfully!" diff --git a/backend/api-gateway/handlers/user.go b/backend/api-gateway/handlers/user.go index ad66b2b..3eebf1f 100644 --- a/backend/api-gateway/handlers/user.go +++ b/backend/api-gateway/handlers/user.go @@ -6,6 +6,7 @@ import ( "api-gateway/utils/client" "api-gateway/utils/cookie" "api-gateway/utils/message" + "bytes" "net/http" "github.com/go-playground/validator/v10" @@ -140,3 +141,45 @@ func CreateUser(c echo.Context) error { return c.JSON(http.StatusCreated, message.CreateSuccessUserMessage(SUCCESS_USER_CREATED, newUser)) } + +func UpdateUser(c echo.Context) error { + tokenClaims := c.Get(TOKEN_CLAIMS_CONTEXT_KEY).(*models.Claims) + + currentUser := tokenClaims.User + authId := currentUser.ID + + var user models.User + err := config.DB.Where("oauth_id = ? AND oauth_provider = ?", oauthId, oauthProvider).First(&user).Error + if err != nil { + if err == gorm.ErrRecordNotFound { + return c.JSON(http.StatusBadRequest, message.CreateErrorMessage(INVALID_USER_NOT_FOUND)) + } + return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage(INVALID_DB_ERROR)) + } + + requestBody := new(models.UpdateUser) + if err := c.Bind(requestBody); err != nil { + return c.JSON(http.StatusBadRequest, message.CreateErrorMessage(INVALID_JSON_REQUEST)) + } + + req, err := http.NewRequest(http.MethodPut, "http://localhost:8081/users/"+authId, bytes.NewBuffer([]byte(requestBody))) + if err != nil { + return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage("Error creating request")) + } + + req.Header.Set("Content-Type", "application/json") + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage("Error sending request")) + } + defer resp.Body.Close() + + var updateUserResponse models.UpdateUserResponse + err = json.NewDecoder(resp.Body).Decode(&updateUserResponse) + if err != nil { + return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage(INVALID_DB_ERROR)) + } + + return c.JSON(http.StatusOK, message.CreateSuccessUserMessage(SUCCESS_USER_UPDATED, updateUserResponse.User)) +} \ No newline at end of file diff --git a/backend/api-gateway/main.go b/backend/api-gateway/main.go index db6d1a3..ea448e3 100755 --- a/backend/api-gateway/main.go +++ b/backend/api-gateway/main.go @@ -38,6 +38,7 @@ func main() { API_GATEWAY.GET(path.AUTH_USER, handlers.GetUser) API_GATEWAY.DELETE(path.AUTH_USER, handlers.DeleteUser) + API_GATEWAY.PUT(path.AUTH_USER_UPDATE, handlers.UpdateUser) API_GATEWAY.POST(path.AUTH_USER_UPGRADE, handlers.UpgradeUser) API_GATEWAY.POST(path.AUTH_USER_DOWNGRADE, handlers.DowngradeUser) API_GATEWAY.GET(path.AUTH_USER_UPGRADE_SUPER_ADMIN, handlers.UpgradeSuperAdmin) diff --git a/backend/api-gateway/models/user.go b/backend/api-gateway/models/user.go index 840acc2..9ecde40 100644 --- a/backend/api-gateway/models/user.go +++ b/backend/api-gateway/models/user.go @@ -28,3 +28,22 @@ type UserServiceCreateUserRequestBody struct { PhotoUrl string `json:"photo_url"` PreferredLanguage string `json:"preferred_language"` } + +type UpdateUser struct { + Username string `json:"username"` + PhotoUrl string `json:"photo_url"` + PreferredLanguage string `json:"preferred_language"` +} + +type UserServiceUser struct { + gorm.Model + AuthUserID uint `json:"auth_user_id"` + Username string `json:"username"` + PhotoUrl string `json:"photo_url"` + PreferredLanguage string `json:"preferred_language"` +} + +type UpdateUserResponse struct { + Message string `json:"message"` + User UserServiceUser `json:"user"` +} diff --git a/backend/api-gateway/utils/path/path.go b/backend/api-gateway/utils/path/path.go index ace6315..8ab5d9f 100644 --- a/backend/api-gateway/utils/path/path.go +++ b/backend/api-gateway/utils/path/path.go @@ -11,6 +11,7 @@ const ( AUTH_USER_UPGRADE = "/auth/user/upgrade" AUTH_USER_UPGRADE_SUPER_ADMIN = "/auth/user/upgrade-super-admin" AUTH_USER_DOWNGRADE = "/auth/user/downgrade" + AUTH_USER_UPDATE = "/auth/user/update" ALL_USER_SERVICE = "/users*" ALL_QUESTION_SERVICE = "/questions*" ALL_MATCHING_SERVICE = "/match*" diff --git a/backend/user-service/handlers/users_handlers.go b/backend/user-service/handlers/users_handlers.go index 74e915b..0c56459 100644 --- a/backend/user-service/handlers/users_handlers.go +++ b/backend/user-service/handlers/users_handlers.go @@ -79,7 +79,7 @@ func CreateUser(c echo.Context) error { } func UpdateUser(c echo.Context) error { - authUserID := c.Param("id") + authUserID := c.Param("authId") intAuthUserId, err := strconv.Atoi(authUserID) if err != nil { diff --git a/backend/user-service/main.go b/backend/user-service/main.go index 60382df..6cdd082 100755 --- a/backend/user-service/main.go +++ b/backend/user-service/main.go @@ -26,7 +26,7 @@ func main() { e.POST("/users", handlers.CreateUser) e.GET("/users", handlers.GetUsers) e.GET("/users/:id", handlers.GetUser) - e.PUT("/users/:id", handlers.UpdateUser) + e.PUT("/users/:authId", handlers.UpdateUser) e.DELETE("/users/:id", handlers.DeleteUser) e.Logger.Fatal(e.Start(":8080")) diff --git a/frontend/app/profile/page.tsx b/frontend/app/profile/page.tsx index 4b7e142..6acef6a 100644 --- a/frontend/app/profile/page.tsx +++ b/frontend/app/profile/page.tsx @@ -1,177 +1,183 @@ -"use client"; +'use client'; -import { Divider } from "@nextui-org/divider"; -import { Input } from "@nextui-org/input"; -import { Select, SelectItem } from "@nextui-org/select"; -import { LANGUAGES } from "../constants/languages"; -import { useForm, Controller } from "react-hook-form"; -import { Button } from "@nextui-org/button"; -import { UserState, updateUser } from "../libs/redux/slices/userSlice"; -import { Avatar } from "@nextui-org/avatar"; -import ImageUpload from "../components/form/ImageUpload"; -import useAuth from "../hooks/useAuth"; -import { GET, PUT } from "../libs/axios/axios"; -import { useDispatch } from "react-redux"; -import { AxiosResponse } from "axios"; -import { notifyError, notifySuccess } from "../components/toast/notifications"; -import { UserResponse } from "../(auth)/login/page"; -import DeleteAccountModal from "../components/modal/deleteAccountModal"; +import { Divider } from '@nextui-org/divider'; +import { Input } from '@nextui-org/input'; +import { Select, SelectItem } from '@nextui-org/select'; +import { LANGUAGES } from '../constants/languages'; +import { useForm, Controller } from 'react-hook-form'; +import { Button } from '@nextui-org/button'; +import { UserState, updateUser } from '../libs/redux/slices/userSlice'; +import { Avatar } from '@nextui-org/avatar'; +import ImageUpload from '../components/form/ImageUpload'; +import useAuth from '../hooks/useAuth'; +import { GET, PUT } from '../libs/axios/axios'; +import { useDispatch } from 'react-redux'; +import { AxiosResponse } from 'axios'; +import { notifyError, notifySuccess } from '../components/toast/notifications'; +import { UserResponse } from '../(auth)/login/page'; +import DeleteAccountModal from '../components/modal/deleteAccountModal'; interface UpdateUserRequest { - username: string; - photo_url: string; - preferred_language: string; + username: string; + photo_url: string; + preferred_language: string; } export default function Profile() { - const { authId, role } = useAuth(); - const dispatch = useDispatch(); + const { authId, role } = useAuth(); + const dispatch = useDispatch(); - const getUser = async () => { - try { - const userResponse: AxiosResponse = await GET(`/users/${authId}`) - const { user } = userResponse.data - dispatch(updateUser(user)); - return { - username: user.username, - photoUrl: user.photo_url, - preferredLanguage: user.preferred_language - } - } catch (error) { - const message = error.message.data.message; - notifyError(message); - } + const getUser = async () => { + try { + const userResponse: AxiosResponse = await GET(`/users/${authId}`); + const { user } = userResponse.data; + dispatch(updateUser(user)); + return { + username: user.username, + photoUrl: user.photo_url, + preferredLanguage: user.preferred_language, + }; + } catch (error) { + const message = error.message.data.message; + notifyError(message); } + }; - const { - control, - handleSubmit, - setValue, - watch, - reset, - formState: { isDirty, errors }, - } = useForm({ - defaultValues: getUser - }); + const { + control, + handleSubmit, + setValue, + watch, + reset, + formState: { isDirty, errors }, + } = useForm({ + defaultValues: getUser, + }); - const onSubmit = handleSubmit(async (data: UserState) => { - const requestBody: UpdateUserRequest = { - username: data.username, - photo_url: data.photoUrl, - preferred_language: data.preferredLanguage - } - try { - const response: AxiosResponse = await PUT(`/users/${authId}`, requestBody); - const {message, user} = response.data; - dispatch(updateUser(user)); - notifySuccess(message); - reset({}, {keepValues: true}) - } catch (error) { - const message = error.message.data.message; - notifyError(message); - } - }) + const onSubmit = handleSubmit(async (data: UserState) => { + const requestBody: UpdateUserRequest = { + username: data.username, + photo_url: data.photoUrl, + preferred_language: data.preferredLanguage, + }; + try { + const response: AxiosResponse = await PUT(`/auth/user`, requestBody); + const { message, user } = response.data; + dispatch(updateUser(user)); + notifySuccess(message); + reset({}, { keepValues: true }); + } catch (error) { + const message = error.message.data.message; + notifyError(message); + } + }); + + const photo = watch('photoUrl'); - const photo = watch('photoUrl'); - - const languages = LANGUAGES + const languages = LANGUAGES; - return ( - <> -
-
-
-

Profile Settings

- - You can update your profile here! - -
- -
- -
-

Username

- value.trim() !== '' - || 'Username cannot be empty or contain only whitespace', - } - }} - name="username" - control={control} - render={({field}) => ( - - )} - /> -
- -
-

Preferred Language

- ( - <> - - - )} - /> -
- -
-

Photo

-
- -
- ( - { - return setValue('photoUrl', value, { shouldDirty: true }); - }} - {...field} - /> - )} - /> -
-
-
- -
-
-

Delete Account

-

- Deleting your account will remove all your information from our database. -

-

- This cannot be undone. -

-
-
- -
-
+ return ( + <> +
+
+
+

Profile Settings

+ You can update your profile here! +
+ +
+ +
+

Username

+ + value.trim() !== '' || 'Username cannot be empty or contain only whitespace', + }, + }} + name="username" + control={control} + render={({ field }) => ( + + )} + /> +
+ +
+

Preferred Language

+ ( + <> + + + )} + /> +
+ +
+

Photo

+
+ +
+ ( + { + return setValue('photoUrl', value, { shouldDirty: true }); + }} + {...field} + /> + )} + />
- - ) +
+
+ +
+
+

Delete Account

+

+ Deleting your account will remove all your information from our database. +

+

This cannot be undone.

+
+
+ +
+
+
+ + ); } From 049e2b6fc9ec7431f3ef58fac0f778cf8c2d39a0 Mon Sep 17 00:00:00 2001 From: Wu Changjun Date: Sat, 11 Nov 2023 02:38:10 +0800 Subject: [PATCH 2/3] fix update user route --- backend/api-gateway/handlers/user.go | 24 +++++++------------ backend/api-gateway/models/message.go | 5 ++++ backend/api-gateway/utils/message/message.go | 7 ++++++ .../user-service/handlers/users_handlers.go | 1 - backend/user-service/main.go | 2 +- frontend/app/profile/page.tsx | 2 +- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/backend/api-gateway/handlers/user.go b/backend/api-gateway/handlers/user.go index 3eebf1f..de6ef4d 100644 --- a/backend/api-gateway/handlers/user.go +++ b/backend/api-gateway/handlers/user.go @@ -7,7 +7,10 @@ import ( "api-gateway/utils/cookie" "api-gateway/utils/message" "bytes" + "encoding/json" "net/http" + "os" + "strconv" "github.com/go-playground/validator/v10" "github.com/labstack/echo/v4" @@ -148,30 +151,19 @@ func UpdateUser(c echo.Context) error { currentUser := tokenClaims.User authId := currentUser.ID - var user models.User - err := config.DB.Where("oauth_id = ? AND oauth_provider = ?", oauthId, oauthProvider).First(&user).Error - if err != nil { - if err == gorm.ErrRecordNotFound { - return c.JSON(http.StatusBadRequest, message.CreateErrorMessage(INVALID_USER_NOT_FOUND)) - } - return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage(INVALID_DB_ERROR)) - } - requestBody := new(models.UpdateUser) if err := c.Bind(requestBody); err != nil { return c.JSON(http.StatusBadRequest, message.CreateErrorMessage(INVALID_JSON_REQUEST)) } - req, err := http.NewRequest(http.MethodPut, "http://localhost:8081/users/"+authId, bytes.NewBuffer([]byte(requestBody))) + requestBodyJSON, err := json.Marshal(requestBody) if err != nil { - return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage("Error creating request")) + return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage("Error marshalling request body")) } - req.Header.Set("Content-Type", "application/json") - client := &http.Client{} - resp, err := client.Do(req) + resp, err := http.Post(os.Getenv("USER_SERVICE_URL") + "/users/" + strconv.FormatUint(uint64(authId), 10), "application/json", bytes.NewBuffer(requestBodyJSON)) if err != nil { - return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage("Error sending request")) + return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage("Error creating request")) } defer resp.Body.Close() @@ -181,5 +173,5 @@ func UpdateUser(c echo.Context) error { return c.JSON(http.StatusInternalServerError, message.CreateErrorMessage(INVALID_DB_ERROR)) } - return c.JSON(http.StatusOK, message.CreateSuccessUserMessage(SUCCESS_USER_UPDATED, updateUserResponse.User)) + return c.JSON(http.StatusOK, message.CreateSuccessUupdateUserMessage(SUCCESS_USER_UPDATED, updateUserResponse.User)) } \ No newline at end of file diff --git a/backend/api-gateway/models/message.go b/backend/api-gateway/models/message.go index 73a408c..6916881 100644 --- a/backend/api-gateway/models/message.go +++ b/backend/api-gateway/models/message.go @@ -17,3 +17,8 @@ type SuccessUsersMessage struct { type SuccessMessage struct { Message string `json:"message"` } + +type SuccessUpdateUserMessage struct { + Message string `json:"message"` + User UserServiceUser `json:"user,omitempty"` +} diff --git a/backend/api-gateway/utils/message/message.go b/backend/api-gateway/utils/message/message.go index ddb47b7..7533b32 100644 --- a/backend/api-gateway/utils/message/message.go +++ b/backend/api-gateway/utils/message/message.go @@ -33,3 +33,10 @@ func CreateSuccessUsersMessage(message string, users []models.User) models.Succe Users: users, } } + +func CreateSuccessUupdateUserMessage(message string, user models.UserServiceUser) models.SuccessUpdateUserMessage { + return models.SuccessUpdateUserMessage{ + Message: message, + User: user, + } +} diff --git a/backend/user-service/handlers/users_handlers.go b/backend/user-service/handlers/users_handlers.go index 0c56459..92f06e9 100644 --- a/backend/user-service/handlers/users_handlers.go +++ b/backend/user-service/handlers/users_handlers.go @@ -87,7 +87,6 @@ func UpdateUser(c echo.Context) error { } uintAuthUserId := uint(intAuthUserId) - var user model.User if err := config.DB.Where("auth_user_id = ?", authUserID).First(&user).Error; err != nil { return c.JSON(http.StatusNotFound, message.CreateErrorMessage(INVALID_USER_NOT_FOUND)) diff --git a/backend/user-service/main.go b/backend/user-service/main.go index 6cdd082..caa31cd 100755 --- a/backend/user-service/main.go +++ b/backend/user-service/main.go @@ -26,7 +26,7 @@ func main() { e.POST("/users", handlers.CreateUser) e.GET("/users", handlers.GetUsers) e.GET("/users/:id", handlers.GetUser) - e.PUT("/users/:authId", handlers.UpdateUser) + e.POST("/users/:authId", handlers.UpdateUser) e.DELETE("/users/:id", handlers.DeleteUser) e.Logger.Fatal(e.Start(":8080")) diff --git a/frontend/app/profile/page.tsx b/frontend/app/profile/page.tsx index 6acef6a..b241aa5 100644 --- a/frontend/app/profile/page.tsx +++ b/frontend/app/profile/page.tsx @@ -61,7 +61,7 @@ export default function Profile() { preferred_language: data.preferredLanguage, }; try { - const response: AxiosResponse = await PUT(`/auth/user`, requestBody); + const response: AxiosResponse = await PUT(`/auth/user/update`, requestBody); const { message, user } = response.data; dispatch(updateUser(user)); notifySuccess(message); From c12678ecabd3546b65be6a66a9d3a88539f2ae20 Mon Sep 17 00:00:00 2001 From: Wu Changjun Date: Sat, 11 Nov 2023 02:58:22 +0800 Subject: [PATCH 3/3] fix lint --- backend/api-gateway/handlers/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/api-gateway/handlers/user.go b/backend/api-gateway/handlers/user.go index de6ef4d..9ccb09c 100644 --- a/backend/api-gateway/handlers/user.go +++ b/backend/api-gateway/handlers/user.go @@ -174,4 +174,4 @@ func UpdateUser(c echo.Context) error { } return c.JSON(http.StatusOK, message.CreateSuccessUupdateUserMessage(SUCCESS_USER_UPDATED, updateUserResponse.User)) -} \ No newline at end of file +}