Database: Only set last name field when needed

This commit is contained in:
mkwcat 2023-12-16 13:56:49 -05:00
parent 6f44c73da2
commit 4039013344
No known key found for this signature in database
GPG Key ID: 7A505679CE9E7AA9
3 changed files with 25 additions and 11 deletions

View File

@ -37,11 +37,21 @@ func LoginUserToGPCM(pool *pgxpool.Pool, ctx context.Context, userId uint64, gsb
logging.Notice("DATABASE", "Created new GPCM user:", aurora.Cyan(userId), aurora.Cyan(gsbrcd), aurora.Cyan(user.ProfileId))
} else {
var expectedNgId *uint32
err := pool.QueryRow(ctx, GetUserProfileID, userId, gsbrcd).Scan(&user.ProfileId, &expectedNgId, &user.Email, &user.UniqueNick, &user.FirstName, &user.LastName)
var firstName *string
var lastName *string
err := pool.QueryRow(ctx, GetUserProfileID, userId, gsbrcd).Scan(&user.ProfileId, &expectedNgId, &user.Email, &user.UniqueNick, &firstName, &lastName)
if err != nil {
panic(err)
}
if firstName != nil {
user.FirstName = *firstName
}
if lastName != nil {
user.LastName = *lastName
}
if expectedNgId != nil {
user.NgDeviceId = *expectedNgId
if ngDeviceId != 0 && user.NgDeviceId != ngDeviceId {
@ -68,8 +78,9 @@ func LoginUserToGPCM(pool *pgxpool.Pool, ctx context.Context, userId uint64, gsb
logging.Notice("DATABASE", "Log in GPCM user:", aurora.Cyan(userId), aurora.Cyan(user.GsbrCode), "-", aurora.Cyan(user.ProfileId))
}
if user.LastName == "" {
user = UpdateProfile(pool, ctx, user.ProfileId, map[string]string{
// This should be set if the user already knows its own profile ID
if profileId != 0 && user.LastName == "" {
user.UpdateProfile(pool, ctx, map[string]string{
"lastname": "000000000" + gsbrcd,
})
}

View File

@ -10,7 +10,7 @@ import (
const (
InsertUser = `INSERT INTO users (user_id, gsbrcd, password, ng_device_id, email, unique_nick) VALUES ($1, $2, $3, $4, $5, $6) RETURNING profile_id`
InsertUserWithProfileID = `INSERT INTO users (profile_id, user_id, gsbrcd, password, ng_device_id, email, unique_nick) VALUES ($1, $2, $3, $4, $5, $6, $7)`
UpdateUserTable = `UPDATE users SET firstname = CASE WHEN $3 THEN $2 ELSE firstname END, lastname = CASE WHEN $5 THEN $4 ELSE lastname END WHERE profile_id = $1 RETURNING user_id, gsbrcd, email, unique_nick, firstname, lastname`
UpdateUserTable = `UPDATE users SET firstname = CASE WHEN $3 THEN $2 ELSE firstname END, lastname = CASE WHEN $5 THEN $4 ELSE lastname END WHERE profile_id = $1`
UpdateUserProfileID = `UPDATE users SET profile_id = $3 WHERE user_id = $1 AND gsbrcd = $2`
UpdateUserNGDeviceID = `UPDATE users SET ng_device_id = $2 WHERE profile_id = $1`
GetUser = `SELECT user_id, gsbrcd, email, unique_nick, firstname, lastname FROM users WHERE profile_id = $1`
@ -90,19 +90,22 @@ func GetUniqueUserID() uint64 {
return uint64(rand.Int63n(0x80000000000))
}
func UpdateProfile(pool *pgxpool.Pool, ctx context.Context, profileId uint32, data map[string]string) User {
func (user *User) UpdateProfile(pool *pgxpool.Pool, ctx context.Context, data map[string]string) {
firstName, firstNameExists := data["firstname"]
lastName, lastNameExists := data["lastname"]
user := User{}
row := pool.QueryRow(ctx, UpdateUserTable, profileId, firstName, firstNameExists, lastName, lastNameExists)
err := row.Scan(&user.UserId, &user.GsbrCode, &user.Email, &user.UniqueNick, &user.FirstName, &user.LastName)
_, err := pool.Exec(ctx, UpdateUserTable, user.ProfileId, firstName, firstNameExists, lastName, lastNameExists)
if err != nil {
panic(err)
}
user.ProfileId = profileId
return user
if firstNameExists {
user.FirstName = firstName
}
if lastNameExists {
user.LastName = lastName
}
}
func GetProfile(pool *pgxpool.Pool, ctx context.Context, profileId uint32) (User, bool) {

View File

@ -61,5 +61,5 @@ func (g *GameSpySession) getProfile(command common.GameSpyCommand) {
}
func (g *GameSpySession) updateProfile(command common.GameSpyCommand) {
g.User = database.UpdateProfile(pool, ctx, g.User.ProfileId, command.OtherValues)
g.User.UpdateProfile(pool, ctx, command.OtherValues)
}