From 104068e1725747313e6dd664d075963f68b21716 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 13 Sep 2021 23:21:31 +0200 Subject: [PATCH] Add game member deletion when user is deleted --- server/services/store/user.go | 20 ++++++++++++++++++-- server/services/store/user_test.go | 11 ++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/server/services/store/user.go b/server/services/store/user.go index 0ccea42..17b0f30 100644 --- a/server/services/store/user.go +++ b/server/services/store/user.go @@ -110,8 +110,24 @@ func (us *UserStore) List() ([]*model.User, error) { } func (us *UserStore) DeleteByUsername(username string) error { - _, err := us.Q().Delete("users"). + user, err := us.GetByUsername(username) + if err != nil { + return fmt.Errorf("cannot get user for deletion: %w", err) + } + + _, err = us.Q().Delete("users"). Where(sq.Eq{"username": username}). Exec() - return err + if err != nil { + return fmt.Errorf("cannot delete user: %w", err) + } + + _, err = us.Q().Delete("gamemembers"). + Where(sq.Eq{"user_id": user.ID}). + Exec() + if err != nil { + return fmt.Errorf("cannot delete game members when deleting user: %w", err) + } + + return nil } diff --git a/server/services/store/user_test.go b/server/services/store/user_test.go index 9d13a74..831371a 100644 --- a/server/services/store/user_test.go +++ b/server/services/store/user_test.go @@ -56,12 +56,9 @@ func TestDeleteUsername(t *testing.T) { require.NoError(t, err) require.NotZero(t, gameMember) - // ToDo: implement member deletion - /* - err = th.store.User().DeleteByUsername(user.Username) - require.NoError(t, err) + err = th.store.User().DeleteByUsername(user.Username) + require.NoError(t, err) - _, err = th.store.Game().GetMember(game.ID, user.ID) - require.Error(t, err) - */ + _, err = th.store.Game().GetMember(game.ID, user.ID) + require.Error(t, err) }