migrated out more modules and refactored db system
This commit is contained in:
@@ -1,30 +1,31 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"projectreshoot/pkg/db"
|
||||
"database/sql"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int // Integer ID (index primary key)
|
||||
Username string // Username (unique)
|
||||
Password_hash string // Bcrypt password hash
|
||||
Created_at int64 // Epoch timestamp when the user was added to the database
|
||||
Bio string // Short byline set by the user
|
||||
ID int // Integer ID (index primary key)
|
||||
Username string // Username (unique)
|
||||
Created_at int64 // Epoch timestamp when the user was added to the database
|
||||
Bio string // Short byline set by the user
|
||||
}
|
||||
|
||||
// Uses bcrypt to set the users Password_hash from the given password
|
||||
func (user *User) SetPassword(ctx context.Context, tx *db.SafeWTX, password string) error {
|
||||
func (user *User) SetPassword(
|
||||
tx *sql.Tx,
|
||||
password string,
|
||||
) error {
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "bcrypt.GenerateFromPassword")
|
||||
}
|
||||
user.Password_hash = string(hashedPassword)
|
||||
newPassword := string(hashedPassword)
|
||||
query := `UPDATE users SET password_hash = ? WHERE id = ?`
|
||||
_, err = tx.Exec(ctx, query, user.Password_hash, user.ID)
|
||||
_, err = tx.Exec(query, newPassword, user.ID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "tx.Exec")
|
||||
}
|
||||
@@ -32,8 +33,15 @@ func (user *User) SetPassword(ctx context.Context, tx *db.SafeWTX, password stri
|
||||
}
|
||||
|
||||
// Uses bcrypt to check if the given password matches the users Password_hash
|
||||
func (user *User) CheckPassword(password string) error {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(user.Password_hash), []byte(password))
|
||||
func (user *User) CheckPassword(tx *sql.Tx, password string) error {
|
||||
query := `SELECT password_hash FROM users WHERE id = ? LIMIT 1`
|
||||
row := tx.QueryRow(query, user.ID)
|
||||
hashedPassword := ""
|
||||
err := row.Scan(&hashedPassword)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "row.Scan")
|
||||
}
|
||||
err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "bcrypt.CompareHashAndPassword")
|
||||
}
|
||||
@@ -41,9 +49,9 @@ func (user *User) CheckPassword(password string) error {
|
||||
}
|
||||
|
||||
// Change the user's username
|
||||
func (user *User) ChangeUsername(ctx context.Context, tx *db.SafeWTX, newUsername string) error {
|
||||
func (user *User) ChangeUsername(tx *sql.Tx, newUsername string) error {
|
||||
query := `UPDATE users SET username = ? WHERE id = ?`
|
||||
_, err := tx.Exec(ctx, query, newUsername, user.ID)
|
||||
_, err := tx.Exec(query, newUsername, user.ID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "tx.Exec")
|
||||
}
|
||||
@@ -51,9 +59,9 @@ func (user *User) ChangeUsername(ctx context.Context, tx *db.SafeWTX, newUsernam
|
||||
}
|
||||
|
||||
// Change the user's bio
|
||||
func (user *User) ChangeBio(ctx context.Context, tx *db.SafeWTX, newBio string) error {
|
||||
func (user *User) ChangeBio(tx *sql.Tx, newBio string) error {
|
||||
query := `UPDATE users SET bio = ? WHERE id = ?`
|
||||
_, err := tx.Exec(ctx, query, newBio, user.ID)
|
||||
_, err := tx.Exec(query, newBio, user.ID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "tx.Exec")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user