From 825bea59f0a29df60887d65224d9de7e315652c3 Mon Sep 17 00:00:00 2001 From: email Date: Fri, 4 Feb 2022 21:24:21 +0800 Subject: [PATCH] fix: user api --- api/memo.go | 1 + api/user.go | 5 ++-- server/memo.go | 11 ++++++++ server/user.go | 45 ++++++++++++++++++++++++++++++--- store/memo.go | 7 +++-- store/shortcut.go | 3 ++- store/user.go | 13 +++++++--- web/src/helpers/api.ts | 10 ++++---- web/src/services/userService.ts | 8 +++--- 9 files changed, 82 insertions(+), 21 deletions(-) diff --git a/api/memo.go b/api/memo.go index 19945404a..f1255f24f 100644 --- a/api/memo.go +++ b/api/memo.go @@ -25,6 +25,7 @@ type MemoPatch struct { type MemoFind struct { Id *int CreatorId *int + RowStatus *string } type MemoDelete struct { diff --git a/api/user.go b/api/user.go index 45839538f..7c93aa0b0 100644 --- a/api/user.go +++ b/api/user.go @@ -29,8 +29,9 @@ type UserPatch struct { type UserFind struct { Id *int `json:"id"` - Name *string `json:"name"` - OpenId *string + Name *string `json:"name"` + Password *string + OpenId *string } type UserRenameCheck struct { diff --git a/server/memo.go b/server/memo.go index 133df923f..a2be2d1b4 100644 --- a/server/memo.go +++ b/server/memo.go @@ -63,6 +63,17 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { memoFind := &api.MemoFind{ CreatorId: &userId, } + showHiddenMemo, err := strconv.ParseBool(c.QueryParam("hidden")) + if err != nil { + showHiddenMemo = false + } + + rowStatus := "NORMAL" + if showHiddenMemo { + rowStatus = "HIDDEN" + } + memoFind.RowStatus = &rowStatus + list, err := s.MemoService.FindMemoList(memoFind) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err) diff --git a/server/user.go b/server/user.go index 75ba07b51..f8ade7cf9 100644 --- a/server/user.go +++ b/server/user.go @@ -51,14 +51,51 @@ func (s *Server) registerUserRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) } + isUsable := true + if user != nil { + isUsable = false + } + c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) - if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal user response").SetInternal(err) + if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(isUsable)); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal rename check response").SetInternal(err) } return nil }) - g.PATCH("user/me", func(c echo.Context) error { + g.POST("/user/password_check", func(c echo.Context) error { + userId := c.Get(getUserIdContextKey()).(int) + userPasswordCheck := &api.UserPasswordCheck{} + if err := json.NewDecoder(c.Request().Body).Decode(userPasswordCheck); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user password check request").SetInternal(err) + } + + if userPasswordCheck.Password == "" { + return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user password check request") + } + + userFind := &api.UserFind{ + Id: &userId, + Password: &userPasswordCheck.Password, + } + user, err := s.UserService.FindUser(userFind) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + } + + isValid := false + if user != nil { + isValid = true + } + + c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) + if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(isValid)); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal password check response").SetInternal(err) + } + + return nil + }) + g.PATCH("/user/me", func(c echo.Context) error { userId := c.Get(getUserIdContextKey()).(int) userPatch := &api.UserPatch{ Id: userId, @@ -67,7 +104,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err) } - if *userPatch.ResetOpenId { + if userPatch.ResetOpenId != nil && *userPatch.ResetOpenId { openId := common.GenUUID() userPatch.OpenId = &openId } diff --git a/store/memo.go b/store/memo.go index 0e2eb71c1..ebf615017 100644 --- a/store/memo.go +++ b/store/memo.go @@ -104,10 +104,10 @@ func patchMemo(db *DB, patch *api.MemoPatch) (*api.Memo, error) { set, args := []string{}, []interface{}{} if v := patch.Content; v != nil { - set, args = append(set, "content = ?"), append(args, v) + set, args = append(set, "content = ?"), append(args, *v) } if v := patch.RowStatus; v != nil { - set, args = append(set, "row_status = ?"), append(args, v) + set, args = append(set, "row_status = ?"), append(args, *v) } args = append(args, patch.Id) @@ -150,6 +150,9 @@ func findMemoList(db *DB, find *api.MemoFind) ([]*api.Memo, error) { if v := find.CreatorId; v != nil { where, args = append(where, "creator_id = ?"), append(args, *v) } + if v := find.RowStatus; v != nil { + where, args = append(where, "row_status = ?"), append(args, *v) + } rows, err := db.Db.Query(` SELECT diff --git a/store/shortcut.go b/store/shortcut.go index 3e3ae3ea6..3fbb939e0 100644 --- a/store/shortcut.go +++ b/store/shortcut.go @@ -69,7 +69,7 @@ func createShortcut(db *DB, create *api.ShortcutCreate) (*api.Shortcut, error) { INSERT INTO shortcut ( title, payload, - creator_id, + creator_id ) VALUES (?, ?, ?) RETURNING id, title, payload, creator_id, created_ts, updated_ts, row_status @@ -103,6 +103,7 @@ func createShortcut(db *DB, create *api.ShortcutCreate) (*api.Shortcut, error) { func patchShortcut(db *DB, patch *api.ShortcutPatch) (*api.Shortcut, error) { set, args := []string{}, []interface{}{} + if v := patch.Title; v != nil { set, args = append(set, "title = ?"), append(args, *v) } diff --git a/store/user.go b/store/user.go index 2ef3f1d0e..08450abf9 100644 --- a/store/user.go +++ b/store/user.go @@ -57,7 +57,11 @@ func createUser(db *DB, create *api.UserCreate) (*api.User, error) { ) VALUES (?, ?, ?) RETURNING id, name, password, open_id, created_ts, updated_ts - `) + `, + create.Name, + create.Password, + create.OpenId, + ) if err != nil { return nil, FormatError(err) } @@ -83,14 +87,15 @@ func patchUser(db *DB, patch *api.UserPatch) (*api.User, error) { set, args := []string{}, []interface{}{} if v := patch.Name; v != nil { - set, args = append(set, "name = ?"), append(args, *v) + set, args = append(set, "name = ?"), append(args, v) } if v := patch.Password; v != nil { - set, args = append(set, "password = ?"), append(args, *v) + set, args = append(set, "password = ?"), append(args, v) } if v := patch.OpenId; v != nil { - set, args = append(set, "open_id = ?"), append(args, *v) + set, args = append(set, "open_id = ?"), append(args, v) } + args = append(args, patch.Id) row, err := db.Db.Query(` diff --git a/web/src/helpers/api.ts b/web/src/helpers/api.ts index 1e2c66960..e75946d69 100644 --- a/web/src/helpers/api.ts +++ b/web/src/helpers/api.ts @@ -78,7 +78,7 @@ namespace api { export function checkUsernameUsable(name: string) { return request({ method: "POST", - url: "/api/user/checkusername", + url: "/api/user/rename_check", data: { name, }, @@ -88,15 +88,15 @@ namespace api { export function checkPasswordValid(password: string) { return request({ method: "POST", - url: "/api/user/validpassword", + url: "/api/user/password_check", data: { password, }, }); } - export function updateUserinfo(userinfo: Partial<{ name: string; password: string }>) { - return request({ + export function updateUserinfo(userinfo: Partial<{ name: string; password: string; resetOpenId: boolean }>) { + return request({ method: "PATCH", url: "/api/user/me", data: userinfo, @@ -120,7 +120,7 @@ namespace api { export function getMyDeletedMemos() { return request({ method: "GET", - url: "/api/memo/?hidden=true", + url: "/api/memo?hidden=true", }); } diff --git a/web/src/services/userService.ts b/web/src/services/userService.ts index 73bcc0ff2..d6a2ffd66 100644 --- a/web/src/services/userService.ts +++ b/web/src/services/userService.ts @@ -55,12 +55,14 @@ class UserService { } public async resetOpenId(): Promise { - const openId = await api.resetOpenId(); + const user = await api.updateUserinfo({ + resetOpenId: true, + }); appStore.dispatch({ type: "RESET_OPENID", - payload: openId, + payload: user.openId, }); - return openId; + return user.openId; } private convertResponseModelUser(user: Model.User): Model.User {