slapid and player now links when registering
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user