mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-03-21 17:44:58 -05:00
86 lines
2.5 KiB
Go
86 lines
2.5 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"math/rand"
|
|
"wwfc/common"
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
)
|
|
|
|
const (
|
|
InsertUser = `INSERT INTO users (user_id, gsbrcd, password, email, unique_nick) VALUES ($1, $2, $3, $4, $5) RETURNING profile_id`
|
|
UpdateUserTable = `UPDATE users SET firstname = $1, lastname = $2 WHERE user_id = $3`
|
|
GetUser = `SELECT user_id, gsbrcd, password, email, unique_nick, firstname, lastname FROM users WHERE profile_id = $1`
|
|
CreateUserSession = `INSERT INTO sessions (session_key, profile_id, login_ticket) VALUES ($1, $2, $3)`
|
|
DoesUserExist = `SELECT EXISTS(SELECT 1 FROM users WHERE user_id = $1 AND gsbrcd = $2)`
|
|
DeleteUserSession = `DELETE FROM sessions WHERE profile_id = $1`
|
|
GetUserProfileID = `SELECT profile_id FROM users WHERE user_id = $1 AND gsbrcd = $2`
|
|
)
|
|
|
|
type User struct {
|
|
ProfileId int64
|
|
UserId int64
|
|
GsbrCode string
|
|
Password string
|
|
Email string
|
|
UniqueNick string
|
|
FirstName string
|
|
LastName string
|
|
}
|
|
|
|
func (user *User) CreateUser(pool *pgxpool.Pool, ctx context.Context) {
|
|
err := pool.QueryRow(ctx, InsertUser, user.UserId, user.GsbrCode, user.Password, user.Email, user.UniqueNick).Scan(&user.ProfileId)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func GetUniqueUserID() int64 {
|
|
// Not guaranteed unique but doesn't matter in practice if multiple people have the same user ID.
|
|
return rand.Int63n(0x80000000000)
|
|
}
|
|
|
|
func UpdateUser(pool *pgxpool.Pool, ctx context.Context, firstName string, lastName string, userId int64) User {
|
|
user := User{}
|
|
_, err := pool.Exec(ctx, UpdateUserTable, firstName, lastName, userId)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return user
|
|
}
|
|
|
|
func CreateSession(pool *pgxpool.Pool, ctx context.Context, profileId int64, loginTicket string) string {
|
|
// Delete session first.
|
|
deleteSession(pool, ctx, profileId)
|
|
|
|
sessionKey := common.RandomString(8)
|
|
_, err := pool.Exec(ctx, CreateUserSession, sessionKey, profileId, loginTicket)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return sessionKey
|
|
}
|
|
|
|
func deleteSession(pool *pgxpool.Pool, ctx context.Context, profileId int64) {
|
|
_, err := pool.Exec(ctx, DeleteUserSession, profileId)
|
|
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func GetProfile(pool *pgxpool.Pool, ctx context.Context, profileId int64) User {
|
|
user := User{}
|
|
row := pool.QueryRow(ctx, GetUser, profileId)
|
|
err := row.Scan(&user.UserId, &user.GsbrCode, &user.Password, &user.Email, &user.UniqueNick, &user.FirstName, &user.LastName)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return user
|
|
}
|