Add game member deletion when user is deleted

This commit is contained in:
Miguel de la Cruz 2021-09-13 23:21:31 +02:00
parent e0da55c63d
commit 104068e172
2 changed files with 22 additions and 9 deletions

View file

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

View file

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