slapid and player now links when registering

This commit is contained in:
2026-02-17 18:33:22 +11:00
parent 9362448f22
commit 42282d05b1
11 changed files with 137 additions and 28 deletions

View File

@@ -3,7 +3,6 @@ package db
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/uptrace/bun"
@@ -78,7 +77,6 @@ func (a *AuditLogFilter) UserIDs(ids []int) *AuditLogFilter {
}
func (a *AuditLogFilter) Actions(actions []string) *AuditLogFilter {
fmt.Println(actions)
if len(actions) > 0 {
a.In("al.action", actions)
}

View File

@@ -3,7 +3,6 @@ package db
import (
"context"
"database/sql"
"fmt"
"github.com/pkg/errors"
"github.com/uptrace/bun"
@@ -110,7 +109,6 @@ func (l *listgetter[T]) Filter(filters ...Filter) *listgetter[T] {
l.q = l.q.Where("? ? ?", bun.Ident(filter.Field), bun.Safe(filter.Comparator), filter.Value)
}
}
fmt.Println(l.q.String())
return l
}

View File

@@ -11,13 +11,15 @@ type Player struct {
bun.BaseModel `bun:"table:players,alias:p"`
ID int `bun:"id,pk,autoincrement" json:"id"`
SlapID *string `bun:"slap_id,unique" json:"slap_id"`
SlapID *uint32 `bun:"slap_id,unique" json:"slap_id"`
DiscordID string `bun:"discord_id,unique,notnull" json:"discord_id"`
UserID *int `bun:"user_id,unique" json:"user_id"`
User *User `bun:"rel:belongs-to,join:user_id=id" json:"-"`
}
// NewPlayer creates a new player in the database. If there is an existing user with the same
// discordID, it will automatically link that user to the player
func NewPlayer(ctx context.Context, tx bun.Tx, discordID string, audit *AuditMeta) (*Player, error) {
player := &Player{DiscordID: discordID}
user, err := GetUserByDiscordID(ctx, tx, discordID)
@@ -35,11 +37,46 @@ func NewPlayer(ctx context.Context, tx bun.Tx, discordID string, audit *AuditMet
return player, nil
}
// ConnectPlayer links the user to an existing player, or creates a new player to link if not found
// Populates User.Player on success
func (u *User) ConnectPlayer(ctx context.Context, tx bun.Tx, audit *AuditMeta) error {
player, err := GetByField[Player](tx, "p.discord_id", u.DiscordID).
Relation("User").Get(ctx)
if err != nil {
if !IsBadRequest(err) {
// Unexpected error occured
return errors.Wrap(err, "GetByField")
}
// Player doesn't exist, create a new one
player, err = NewPlayer(ctx, tx, u.DiscordID, audit)
if err != nil {
return errors.Wrap(err, "NewPlayer")
}
// New player should automatically get linked to the user
u.Player = player
return nil
}
// Player was found
if player.UserID != nil {
if player.UserID == &u.ID {
return nil
}
return errors.New("player with that discord_id already linked to a user")
}
player.UserID = &u.ID
err = UpdateByID(tx, player.ID, player).Column("user_id").Exec(ctx)
if err != nil {
return errors.Wrap(err, "UpdateByID")
}
u.Player = player
return nil
}
func GetPlayer(ctx context.Context, tx bun.Tx, playerID int) (*Player, error) {
return GetByID[Player](tx, playerID).Relation("User").Get(ctx)
}
func UpdatePlayerSlapID(ctx context.Context, tx bun.Tx, playerID int, slapID string, audit *AuditMeta) error {
func UpdatePlayerSlapID(ctx context.Context, tx bun.Tx, playerID int, slapID uint32, audit *AuditMeta) error {
player, err := GetPlayer(ctx, tx, playerID)
if err != nil {
return errors.Wrap(err, "GetPlayer")

View File

@@ -56,7 +56,7 @@ func CreateUser(ctx context.Context, tx bun.Tx, username string, discorduser *di
// GetUserByID queries the database for a user matching the given ID
// Returns a BadRequestNotFound error if no user is found
func GetUserByID(ctx context.Context, tx bun.Tx, id int) (*User, error) {
return GetByID[User](tx, id).Get(ctx)
return GetByID[User](tx, id).Relation("Player").Get(ctx)
}
// GetUserByUsername queries the database for a user matching the given username
@@ -65,7 +65,7 @@ func GetUserByUsername(ctx context.Context, tx bun.Tx, username string) (*User,
if username == "" {
return nil, errors.New("username not provided")
}
return GetByField[User](tx, "username", username).Get(ctx)
return GetByField[User](tx, "username", username).Relation("Player").Get(ctx)
}
// GetUserByDiscordID queries the database for a user matching the given discord id
@@ -74,7 +74,7 @@ func GetUserByDiscordID(ctx context.Context, tx bun.Tx, discordID string) (*User
if discordID == "" {
return nil, errors.New("discord_id not provided")
}
return GetByField[User](tx, "discord_id", discordID).Get(ctx)
return GetByField[User](tx, "u.discord_id", discordID).Relation("Player").Get(ctx)
}
// GetRoles loads all the roles for this user
@@ -142,7 +142,7 @@ func (u *User) IsAdmin(ctx context.Context, tx bun.Tx) (bool, error) {
func GetUsers(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*List[User], error) {
defaults := &PageOpts{1, 50, bun.OrderAsc, "id"}
return GetList[User](tx).GetPaged(ctx, pageOpts, defaults)
return GetList[User](tx).Relation("Player").GetPaged(ctx, pageOpts, defaults)
}
// GetUsersWithRoles queries the database for users with their roles preloaded