memos/store/user.go

152 lines
4.6 KiB
Go
Raw Normal View History

2021-12-08 18:43:14 +03:00
package store
import (
"database/sql"
"fmt"
"memos/common"
2021-12-09 17:02:57 +03:00
"strings"
2021-12-08 18:43:14 +03:00
)
type User struct {
Id string `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
WxOpenId string `json:"wxOpenId"`
GithubName string `json:"githubName"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
func CreateNewUser(username string, password string, githubName string, wxOpenId string) (User, error) {
nowDateTimeStr := common.GetNowDateTimeStr()
newUser := User{
Id: common.GenUUID(),
Username: username,
Password: password,
WxOpenId: wxOpenId,
GithubName: githubName,
CreatedAt: nowDateTimeStr,
UpdatedAt: nowDateTimeStr,
}
query := `INSERT INTO users (id, username, password, wx_open_id, github_name, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)`
_, err := DB.Exec(query, newUser.Id, newUser.Username, newUser.Password, newUser.WxOpenId, newUser.GithubName, newUser.CreatedAt, newUser.UpdatedAt)
return newUser, err
}
2021-12-09 17:02:57 +03:00
type UserPatch struct {
Username *string
Password *string
GithubName *string
WxOpenId *string
}
func UpdateUser(id string, userPatch *UserPatch) (User, error) {
2021-12-08 18:43:14 +03:00
user, _ := GetUserById(id)
2021-12-09 17:02:57 +03:00
set, args := []string{}, []interface{}{}
2021-12-08 18:43:14 +03:00
2021-12-09 17:02:57 +03:00
if v := userPatch.Username; v != nil {
user.Username = *v
set, args = append(set, "username=?"), append(args, *v)
2021-12-08 18:43:14 +03:00
}
2021-12-09 17:02:57 +03:00
if v := userPatch.Password; v != nil {
user.Password = *v
set, args = append(set, "password=?"), append(args, *v)
2021-12-08 18:43:14 +03:00
}
2021-12-09 17:02:57 +03:00
if v := userPatch.GithubName; v != nil {
user.GithubName = *v
set, args = append(set, "github_name=?"), append(args, *v)
2021-12-08 18:43:14 +03:00
}
2021-12-09 17:02:57 +03:00
if v := userPatch.WxOpenId; v != nil {
user.WxOpenId = *v
set, args = append(set, "wx_open_id=?"), append(args, *v)
2021-12-08 18:43:14 +03:00
}
2021-12-09 17:02:57 +03:00
set, args = append(set, "updated_at=?"), append(args, common.GetNowDateTimeStr())
args = append(args, id)
2021-12-08 18:43:14 +03:00
2021-12-09 17:02:57 +03:00
sqlQuery := `UPDATE users SET ` + strings.Join(set, ",") + ` WHERE id=?`
_, err := DB.Exec(sqlQuery, args...)
2021-12-08 18:43:14 +03:00
return user, err
}
func GetUserById(id string) (User, error) {
query := `SELECT id, username, password, wx_open_id, github_name, created_at, updated_at FROM users WHERE id=?`
2021-12-09 17:02:57 +03:00
user := User{}
2021-12-08 18:43:14 +03:00
err := DB.QueryRow(query, id).Scan(&user.Id, &user.Username, &user.Password, &user.WxOpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt)
return user, err
}
func GetUserByUsernameAndPassword(username string, password string) (User, error) {
query := `SELECT id, username, password, wx_open_id, github_name, created_at, updated_at FROM users WHERE username=? AND password=?`
2021-12-09 17:02:57 +03:00
user := User{}
2021-12-08 18:43:14 +03:00
err := DB.QueryRow(query, username, password).Scan(&user.Id, &user.Username, &user.Password, &user.WxOpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt)
return user, err
}
func GetUserByGithubName(githubName string) (User, error) {
query := `SELECT id, username, password, wx_open_id, github_name, created_at, updated_at FROM users WHERE github_name=?`
2021-12-09 17:02:57 +03:00
user := User{}
2021-12-08 18:43:14 +03:00
err := DB.QueryRow(query, githubName).Scan(&user.Id, &user.Username, &user.Password, &user.WxOpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt)
return user, err
}
func GetUserByWxOpenId(wxOpenId string) (User, error) {
query := `SELECT id, username, password, wx_open_id, github_name, created_at, updated_at FROM users WHERE id=?`
2021-12-09 17:02:57 +03:00
user := User{}
2021-12-08 18:43:14 +03:00
err := DB.QueryRow(query, wxOpenId).Scan(&user.Id, &user.Username, &user.Password, &user.WxOpenId, &user.GithubName, &user.CreatedAt, &user.UpdatedAt)
return user, err
}
func CheckUsernameUsable(username string) (bool, error) {
query := `SELECT * FROM users WHERE username=?`
query = fmt.Sprintf("SELECT COUNT(*) FROM (%s)", query)
var count uint
err := DB.QueryRow(query, username).Scan(&count)
if err != nil && err != sql.ErrNoRows {
return false, FormatDBError(err)
}
if count > 0 {
return false, nil
} else {
return true, nil
}
}
func CheckGithubNameUsable(githubName string) (bool, error) {
query := `SELECT * FROM users WHERE github_name=?`
query = fmt.Sprintf("SELECT COUNT(*) FROM (%s)", query)
var count uint
err := DB.QueryRow(query, githubName).Scan(&count)
if err != nil && err != sql.ErrNoRows {
return false, FormatDBError(err)
}
if count > 0 {
return false, nil
} else {
return true, nil
}
}
func CheckPasswordValid(id string, password string) (bool, error) {
query := `SELECT * FROM users WHERE id=? AND password=?`
query = fmt.Sprintf("SELECT COUNT(*) FROM (%s)", query)
var count uint
err := DB.QueryRow(query, id, password).Scan(&count)
if err != nil && err != sql.ErrNoRows {
return false, FormatDBError(err)
}
if count > 0 {
return true, nil
} else {
return false, nil
}
}