From 5dc3d4a39e8842c261f809597776777f8193b957 Mon Sep 17 00:00:00 2001 From: STEVEN Date: Tue, 14 Dec 2021 20:40:24 +0800 Subject: [PATCH] feat: add open_id key to user (#10) --- api/user.go | 18 ++++++++++++++++++ resources/initial_db.sql | 1 + resources/memos.db | Bin 245760 -> 245760 bytes store/user.go | 27 +++++++++++++++++++-------- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/api/user.go b/api/user.go index a5757e2f..a4db8b3e 100644 --- a/api/user.go +++ b/api/user.go @@ -75,6 +75,23 @@ func handleUpdateMyUserInfo(w http.ResponseWriter, r *http.Request) { }) } +func handleRefreshUserOpenId(w http.ResponseWriter, r *http.Request) { + userId, _ := GetUserIdInSession(r) + + openId, err := store.UpdateUserOpenId(userId) + + if err != nil { + e.ErrorHandler(w, "DATABASE_ERROR", err.Error()) + return + } + + json.NewEncoder(w).Encode(Response{ + Succeed: true, + Message: "", + Data: openId, + }) +} + type CheckUsername struct { Username string } @@ -138,6 +155,7 @@ func RegisterUserRoutes(r *mux.Router) { userRouter.HandleFunc("/me", handleGetMyUserInfo).Methods("GET") userRouter.HandleFunc("/me", handleUpdateMyUserInfo).Methods("PATCH") + userRouter.HandleFunc("/open_id/new", handleRefreshUserOpenId).Methods("POST") userRouter.HandleFunc("/checkusername", handleCheckUsername).Methods("POST") userRouter.HandleFunc("/validpassword", handleValidPassword).Methods("POST") } diff --git a/resources/initial_db.sql b/resources/initial_db.sql index 3ed854d5..b62eb404 100644 --- a/resources/initial_db.sql +++ b/resources/initial_db.sql @@ -7,6 +7,7 @@ CREATE TABLE `users` ( `id` TEXT NOT NULL PRIMARY KEY, `username` TEXT NOT NULL, `password` TEXT NOT NULL, + `open_id` TEXT NOT NULL DEFAULT '', `github_name` TEXT NOT NULL DEFAULT '', `created_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), `updated_at` TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')), diff --git a/resources/memos.db b/resources/memos.db index f3240385cf747f13305a6bdfba5dc8835c1fedc5..dcd93ca84970ff32bbf71bb55c8d39de654e4420 100644 GIT binary patch delta 291 zcmZo@;BRQ)pCB#xn}LDB28cO<*oc9FNqnM?vEpwAz2F31{vQmSoT?0b68vZR7x9Gi zzU11&qr<7n)yaEuv!K8V_Q@I?e>Zz`erDlnW@ls<7Z+!2&7JJQ*TSA(keU~tnKJnR zuiWO-eAbL?oeca(_&YZXD%A3`TQYI*i%RlNo+vLPZ)|2|Vq|1sWTs~IV~?MflJLMg+=~2B`5F7*TX5fso=bTAUDvzTtEz`n;$f^KWJbCVy5j6 I8kiOA0qT22&Hw-a delta 280 zcmZo@;BRQ)pCB#xk%58128cO<*oc9FiGQMwvEoMtz2F31{vQmSoZbw4cKm1g7xC2c z@pC=m3F7qTTF-lOv!K8V_Q@I?ezIVpeId$*c6D(fY`kGK|}k421X!e+Ww${S-~Cv DV$wq* diff --git a/store/user.go b/store/user.go index 8b876bbc..eaa4f8e4 100644 --- a/store/user.go +++ b/store/user.go @@ -11,6 +11,7 @@ type User struct { Id string `json:"id"` Username string `json:"username"` Password string `json:"password"` + OpenId string `json:"openId"` GithubName string `json:"githubName"` CreatedAt string `json:"createdAt"` UpdatedAt string `json:"updatedAt"` @@ -22,13 +23,14 @@ func CreateNewUser(username string, password string, githubName string) (User, e Id: utils.GenUUID(), Username: username, Password: password, + OpenId: utils.GenUUID(), GithubName: githubName, CreatedAt: nowDateTimeStr, UpdatedAt: nowDateTimeStr, } - query := `INSERT INTO users (id, username, password, github_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)` - _, err := DB.Exec(query, newUser.Id, newUser.Username, newUser.Password, newUser.GithubName, newUser.CreatedAt, newUser.UpdatedAt) + query := `INSERT INTO users (id, username, password, open_id, github_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)` + _, err := DB.Exec(query, newUser.Id, newUser.Username, newUser.Password, newUser.OpenId, newUser.GithubName, newUser.CreatedAt, newUser.UpdatedAt) return newUser, err } @@ -64,24 +66,33 @@ func UpdateUser(id string, userPatch *UserPatch) (User, error) { return user, err } +func UpdateUserOpenId(userId string) (string, error) { + openId := utils.GenUUID() + + query := `UPDATE users SET open_id=? WHERE id=?` + _, err := DB.Exec(query, openId, userId) + + return openId, err +} + func GetUserById(id string) (User, error) { - query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE id=?` + query := `SELECT id, username, password, open_id, github_name, created_at, updated_at FROM users WHERE id=?` user := User{} - err := DB.QueryRow(query, id).Scan(&user.Id, &user.Username, &user.Password, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) + err := DB.QueryRow(query, id).Scan(&user.Id, &user.Username, &user.Password, &user.OpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) return user, err } func GetUserByUsernameAndPassword(username string, password string) (User, error) { - query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE username=? AND password=?` + query := `SELECT id, username, password, open_id, github_name, created_at, updated_at FROM users WHERE username=? AND password=?` user := User{} - err := DB.QueryRow(query, username, password).Scan(&user.Id, &user.Username, &user.Password, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) + err := DB.QueryRow(query, username, password).Scan(&user.Id, &user.Username, &user.Password, &user.OpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) return user, err } func GetUserByGithubName(githubName string) (User, error) { - query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE github_name=?` + query := `SELECT id, username, password, open_id, github_name, created_at, updated_at FROM users WHERE github_name=?` user := User{} - err := DB.QueryRow(query, githubName).Scan(&user.Id, &user.Username, &user.Password, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) + err := DB.QueryRow(query, githubName).Scan(&user.Id, &user.Username, &user.Password, &user.OpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt) return user, err }