Stack of changes to get gin, scs, nosurf running.

This commit is contained in:
2025-10-28 11:56:42 +00:00
parent 07117ba35e
commit 86be6479f1
65 changed files with 1890 additions and 1503 deletions

View File

@@ -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
}