This commit is contained in:
vipg
2026-02-09 16:49:18 +08:00
parent 6640f09639
commit bdb1065217
6 changed files with 261 additions and 141 deletions

View File

@@ -0,0 +1,47 @@
package repository
import (
"database/sql"
)
type Repo struct {
DB *sql.DB
}
func New(db *sql.DB) *Repo {
return &Repo{DB: db}
}
func (r *Repo) CreateUser(tx *sql.Tx) (string, error) {
var userID string
if err := tx.QueryRow(`INSERT INTO users DEFAULT VALUES RETURNING user_id`).Scan(&userID); err != nil {
return "", err
}
return userID, nil
}
func (r *Repo) CreateLoginAccount(tx *sql.Tx, userID, account string) error {
_, err := tx.Exec(`INSERT INTO user_login_accounts (user_id, value, deleted) VALUES ($1, $2, false)`, userID, account)
return err
}
func (r *Repo) CreateLoginPassword(tx *sql.Tx, userID, hashed string) error {
_, err := tx.Exec(`INSERT INTO user_login_passwords (user_id, value, deleted) VALUES ($1, $2, false)`, userID, hashed)
return err
}
func (r *Repo) GetUserIDByAccount(account string) (string, error) {
var userID string
if err := r.DB.QueryRow(`SELECT user_id FROM user_login_accounts WHERE value = $1 AND deleted = false`, account).Scan(&userID); err != nil {
return "", err
}
return userID, nil
}
func (r *Repo) GetHashedPassword(userID string) (string, error) {
var hashed string
if err := r.DB.QueryRow(`SELECT value FROM user_login_passwords WHERE user_id = $1 AND deleted = false`, userID).Scan(&hashed); err != nil {
return "", err
}
return hashed, nil
}