Stack of changes to get gin, scs, nosurf running.
This commit is contained in:
@@ -1,22 +1,27 @@
|
||||
package storage
|
||||
package usersStorage
|
||||
|
||||
// ToDo.. "errors" should this not be using my custom log wrapper
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UsersRepo struct{ db *sql.DB }
|
||||
const CreateUserSQL = `
|
||||
INSERT INTO users (username, email, password_hash, created_at, updated_at)
|
||||
VALUES (?, ?, ?, UTC_TIMESTAMP(), UTC_TIMESTAMP())`
|
||||
|
||||
func NewUsersRepo(db *sql.DB) *UsersRepo {
|
||||
return &UsersRepo{db: db}
|
||||
}
|
||||
func CreateUser(db *sql.DB, username, email, passwordHash string) (int64, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// ToDo: should the function be in sql?
|
||||
func (r *UsersRepo) Create(ctx context.Context, username, passwordHash string, isAdmin bool) error {
|
||||
_, err := r.db.ExecContext(ctx,
|
||||
`INSERT INTO users (username, password_hash, is_admin) VALUES (?, ?, ?)`,
|
||||
username, passwordHash, isAdmin,
|
||||
)
|
||||
return err
|
||||
res, err := db.ExecContext(ctx, CreateUserSQL, username, email, passwordHash)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := res.LastInsertId()
|
||||
if err != nil || id == 0 {
|
||||
return 0, errors.New("could not get insert id")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user