memos/store/user.go

138 lines
3.9 KiB
Go
Raw Normal View History

2021-12-08 18:43:14 +03:00
package store
import (
"database/sql"
"fmt"
2021-12-12 09:39:28 +03:00
"memos/utils"
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"`
GithubName string `json:"githubName"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
2021-12-12 16:49:46 +03:00
func CreateNewUser(username string, password string, githubName string) (User, error) {
2021-12-12 09:39:28 +03:00
nowDateTimeStr := utils.GetNowDateTimeStr()
2021-12-08 18:43:14 +03:00
newUser := User{
2021-12-12 09:39:28 +03:00
Id: utils.GenUUID(),
2021-12-08 18:43:14 +03:00
Username: username,
Password: password,
GithubName: githubName,
CreatedAt: nowDateTimeStr,
UpdatedAt: nowDateTimeStr,
}
2021-12-12 16:49:46 +03:00
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)
2021-12-08 18:43:14 +03:00
return newUser, err
}
2021-12-09 17:02:57 +03:00
type UserPatch struct {
Username *string
Password *string
GithubName *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-12 09:39:28 +03:00
set, args = append(set, "updated_at=?"), append(args, utils.GetNowDateTimeStr())
2021-12-09 17:02:57 +03:00
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) {
2021-12-12 16:49:46 +03:00
query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE id=?`
2021-12-09 17:02:57 +03:00
user := User{}
2021-12-12 16:49:46 +03:00
err := DB.QueryRow(query, id).Scan(&user.Id, &user.Username, &user.Password, &user.GithubName, &user.CreatedAt, &user.UpdatedAt)
2021-12-08 18:43:14 +03:00
return user, err
}
func GetUserByUsernameAndPassword(username string, password string) (User, error) {
2021-12-12 16:49:46 +03:00
query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE username=? AND password=?`
2021-12-09 17:02:57 +03:00
user := User{}
2021-12-12 16:49:46 +03:00
err := DB.QueryRow(query, username, password).Scan(&user.Id, &user.Username, &user.Password, &user.GithubName, &user.CreatedAt, &user.UpdatedAt)
2021-12-08 18:43:14 +03:00
return user, err
}
func GetUserByGithubName(githubName string) (User, error) {
2021-12-12 16:49:46 +03:00
query := `SELECT id, username, password, github_name, created_at, updated_at FROM users WHERE github_name=?`
2021-12-09 17:02:57 +03:00
user := User{}
2021-12-12 16:49:46 +03:00
err := DB.QueryRow(query, githubName).Scan(&user.Id, &user.Username, &user.Password, &user.GithubName, &user.CreatedAt, &user.UpdatedAt)
2021-12-08 18:43:14 +03:00
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
}
}