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) }