From 4997c10b97fa7340428fbb8fbe2b2ddf5bcf44d6 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Thu, 30 May 2024 23:37:20 +0530 Subject: [PATCH] Add user profile APIs and update UI. --- cmd/users.go | 36 +++++++++++ frontend/src/App.vue | 12 ++-- frontend/src/api/index.js | 6 ++ frontend/src/assets/style.scss | 45 +++++++------- frontend/src/router/index.js | 6 ++ frontend/src/views/UserForm.vue | 2 +- frontend/src/views/UserProfile.vue | 96 ++++++++++++++++++++++++++++++ i18n/en.json | 2 +- internal/core/users.go | 16 +++++ models/queries.go | 15 ++--- queries.sql | 5 ++ 11 files changed, 206 insertions(+), 35 deletions(-) create mode 100644 frontend/src/views/UserProfile.vue diff --git a/cmd/users.go b/cmd/users.go index a6124948..fcaccd80 100644 --- a/cmd/users.go +++ b/cmd/users.go @@ -210,3 +210,39 @@ func handleGetUserProfile(c echo.Context) error { return c.JSON(http.StatusOK, okResp{user}) } + +// handleUpdateUserProfile update's the current user's profile. +func handleUpdateUserProfile(c echo.Context) error { + var ( + app = c.Get("app").(*App) + user = c.Get(auth.UserKey).(models.User) + ) + + u := models.User{} + if err := c.Bind(&u); err != nil { + return err + } + u.PasswordLogin = user.PasswordLogin + u.Name = strings.TrimSpace(u.Name) + email := strings.TrimSpace(u.Email.String) + + // Validate fields. + if !utils.ValidateEmail(email) { + return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "email")) + } + u.Email = null.String{String: email, Valid: true} + + if u.PasswordLogin && u.Password.String != "" { + if !strHasLen(u.Password.String, 8, stdInputMaxLen) { + return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "password")) + } + } + + out, err := app.core.UpdateUser(user.ID, u) + if err != nil { + return err + } + out.Password = null.String{} + + return c.JSON(http.StatusOK, okResp{out}) +} diff --git a/frontend/src/App.vue b/frontend/src/App.vue index b4f15f38..0eb5c2c5 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -14,15 +14,17 @@ @toggleGroup="toggleGroup" @doLogout="doLogout" /> -