29 lines
609 B
Go
29 lines
609 B
Go
|
package store
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
func (s *Store) UpsertPlayer(ctx context.Context, player *Player) error {
|
||
|
return errors.WithStack(s.Do(ctx, func(db *gorm.DB) error {
|
||
|
var existing *Player
|
||
|
err := db.Find(&existing, "user_id = ? and user_provider = ?", player.UserID, player.UserProvider).Error
|
||
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||
|
return errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
if existing != nil {
|
||
|
player.Model = existing.Model
|
||
|
}
|
||
|
|
||
|
if err := db.Save(player).Error; err != nil {
|
||
|
return errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}))
|
||
|
}
|