Skip to content

Commit 9e53004

Browse files
committed
feat: Change update mcAccount to upsert
MCAccount records for users will now be upserted to avoid missing mc_accounts records
1 parent 206ee30 commit 9e53004

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

‎api/cmd/migrations/20210613042045_init.up.sql‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ CREATE TABLE IF NOT EXISTS mc_accounts (
3232
id uuid PRIMARY KEY,
3333
username varchar(16) NOT NULL,
3434
skin text,
35-
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
35+
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
3636
created_at timestamptz NOT NULL DEFAULT now(),
3737
updated_at timestamptz NOT NULL DEFAULT now(),
3838
deleted_at timestamptz
3939
);
4040

41+
CREATE UNIQUE INDEX mc_accounts_user_id_key ON mc_accounts (user_id) WHERE deleted_at IS NULL;
42+
CREATE UNIQUE INDEX mc_accounts_key ON mc_accounts (id) WHERE deleted_at IS NULL;
43+
4144
--gopg:split
4245

4346
CREATE TABLE IF NOT EXISTS games (

‎api/controllers/api/v1/user.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ func GetMe(c *gin.Context) {
6767
}
6868

6969
func EditMe(c *gin.Context) {
70-
userId := uuid.MustParse(c.GetString(jwt.ContextKey))
70+
v, _ := c.Get(session.SessionUserIDKey)
71+
userId := v.(uuid.UUID)
7172

7273
var body userv1Resource.EditUserBody
7374
if err := c.ShouldBindJSON(&body); err != nil {
@@ -91,16 +92,15 @@ func EditMe(c *gin.Context) {
9192
}
9293

9394
user := userv1Model.User{
94-
Model: model.Model{
95-
ID: userId,
96-
},
9795
MCAccount: &mcv1Model.MCAccount{
9896
Model: model.Model{ID: mcUser.UUID},
97+
UserID: userId,
9998
Username: mcUser.Username,
99+
Skin: mcUser.Textures.Skin.URL,
100100
},
101101
}
102102

103-
if err := userv1Service.EditMCAccount(user.MCAccount, user.ID); err != nil {
103+
if err := userv1Service.UpsertUserMCAccount(&user, userId); err != nil {
104104
if err == pg.ErrNoRows {
105105
c.Error(apiErr.NewNotFoundError(ErrUserNotFound, v1Err.ErrUserNotFound))
106106
} else {
@@ -121,6 +121,7 @@ func EditMe(c *gin.Context) {
121121
MCAccount: &userv1Resource.MCAccount{
122122
MCUsername: user.MCAccount.Username,
123123
MCUUID: user.MCAccount.ID,
124+
Skin: user.MCAccount.Skin,
124125
},
125126
LastLogin: user.LastLogin,
126127
CreatedAt: user.CreatedAt,

‎api/resources/api/v1/user/user.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type TwitchAccount struct {
2626
type MCAccount struct {
2727
MCUsername string `json:"mcUsername"`
2828
MCUUID uuid.UUID `json:"mcUuid"`
29+
Skin string `json:"skin"`
2930
}
3031

3132
type EditUserBody struct {

‎api/services/api/v1/user/mc.go‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package user
33
import (
44
"agones-minecraft/db"
55
"agones-minecraft/models/v1/mc"
6+
"context"
67
"time"
78

9+
userv1Model "agones-minecraft/models/v1/user"
10+
811
"github.com/go-pg/pg/v10"
912
"github.com/google/uuid"
1013
)
@@ -14,8 +17,27 @@ func CreateMCAccount(tx *pg.Tx, account *mc.MCAccount) error {
1417
return err
1518
}
1619

17-
func EditMCAccount(account *mc.MCAccount, userId uuid.UUID) error {
18-
account.UpdatedAt = time.Now()
19-
_, err := db.DB().Model(account).Where("user_id = ?", userId).Update()
20-
return err
20+
func UpsertUserMCAccount(user *userv1Model.User, userId uuid.UUID) error {
21+
return db.DB().RunInTransaction(context.Background(), func(t *pg.Tx) error {
22+
updatedAt := time.Now()
23+
24+
_, err := t.Model(user.MCAccount).
25+
OnConflict("(user_id) WHERE deleted_at IS NULL DO UPDATE").
26+
Set("id = EXCLUDED.id, skin = EXCLUDED.skin, username = EXCLUDED.username, updated_at = ?", updatedAt).
27+
Insert()
28+
if err != nil {
29+
return err
30+
}
31+
32+
updateUser := t.Model(user).
33+
Set("updated_at = ?", updatedAt).
34+
Where("id = ?", userId).
35+
Returning("*")
36+
37+
return t.Model(user).
38+
Relation("TwitchAccount").
39+
Relation("MCAccount").
40+
WithUpdate("updateUser", updateUser).
41+
First()
42+
})
2143
}

0 commit comments

Comments
 (0)