28 lines
651 B
Go
28 lines
651 B
Go
package usersStorage
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
const CreateUserSQL = `
|
|
INSERT INTO users (username, email, password_hash, created_at, updated_at)
|
|
VALUES (?, ?, ?, UTC_TIMESTAMP(), UTC_TIMESTAMP())`
|
|
|
|
func CreateUser(db *sql.DB, username, email, passwordHash string) (int64, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
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
|
|
}
|