Skip to content

Commit

Permalink
remove joingrouptx, fix join group
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jul 8, 2024
1 parent d8af1a1 commit 03700f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
17 changes: 0 additions & 17 deletions internal/group/group.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type Repository interface {
UpdateConfirm(id string, group *model.Group) error
CreateTX(tx *gorm.DB, group *model.Group) error
MoveUserToNewGroupTX(tx *gorm.DB, userId string, groupId *uuid.UUID) error
JoinGroupTX(tx *gorm.DB, userId string, groupId *uuid.UUID) error
DeleteGroupTX(tx *gorm.DB, groupId *uuid.UUID) error
}

Expand Down Expand Up @@ -90,22 +89,6 @@ func (r *repositoryImpl) MoveUserToNewGroupTX(tx *gorm.DB, userId string, groupI
return nil
}

func (r *repositoryImpl) JoinGroupTX(tx *gorm.DB, userId string, groupId *uuid.UUID) error {
updateMap := map[string]interface{}{
"group_id": groupId,
}

result := tx.Model(&model.User{}).Where("id = ?", userId).Updates(updateMap)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return errors.New("no user found with the given ID")
}

return nil
}

func (r *repositoryImpl) DeleteGroupTX(tx *gorm.DB, groupId *uuid.UUID) error {
result := tx.Delete(&model.Group{}, "id = ?", groupId)
if result.Error != nil {
Expand Down
35 changes: 21 additions & 14 deletions internal/group/group.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,29 @@ func (s *serviceImpl) Join(_ context.Context, in *proto.JoinGroupRequest) (*prot
return nil, status.Error(codes.PermissionDenied, "You are the group leader, so you must kick all other members before joining another group")
}

err = s.repo.WithTransaction(func(tx *gorm.DB) error {
joiningGroup := &model.Group{}
if err := s.repo.FindByToken(in.Token, joiningGroup); err != nil {
s.log.Named("Join").Error("FindByToken joiningGroup TX: ", zap.Error(err))
return fmt.Errorf("failed to find group by token: %w", err)
joiningGroup := &model.Group{}
if err := s.repo.FindByToken(in.Token, joiningGroup); err != nil {
s.log.Named("Join").Error("FindByToken joiningGroup TX: ", zap.Error(err))
return nil, status.Error(codes.Internal, "failed to find joining group by token")
}

for _, member := range joiningGroup.Members {
if member.ID.String() == in.UserId {
s.log.Named("Join").Error("User is already in the group", zap.String("user_id", in.UserId))
return nil, status.Error(codes.PermissionDenied, "user is already in the group")
}
}

if err := s.repo.JoinGroupTX(tx, in.UserId, &joiningGroup.ID); err != nil {
s.log.Named("Join").Error("JoinGroupTX: ", zap.Error(err))
return fmt.Errorf("failed to join group: %w", err)
if len(joiningGroup.Members) >= s.conf.Capacity {
s.log.Named("Join").Error("Group is full", zap.String("token", in.Token))
return nil, status.Error(codes.PermissionDenied, "group is full")
}

err = s.repo.WithTransaction(func(tx *gorm.DB) error {

if err := s.userRepo.AssignGroupTX(tx, in.UserId, &joiningGroup.ID); err != nil {
s.log.Named("findByUserId").Error("AssignGroupTX: ", zap.Error(err))
return fmt.Errorf("failed to assign user to group: %w", err)
}

if in.UserId == prevGroup.LeaderID.String() && len(prevGroup.Members) == 1 {
Expand All @@ -355,12 +368,6 @@ func (s *serviceImpl) Join(_ context.Context, in *proto.JoinGroupRequest) (*prot
return nil, status.Error(codes.Internal, fmt.Sprintf("transaction failed: %s", err.Error()))
}

joiningGroup := &model.Group{}
if err := s.repo.FindByToken(in.Token, joiningGroup); err != nil {
s.log.Named("Join").Error("FindByToken joiningGroup: ", zap.Error(err))
return nil, status.Error(codes.Internal, "failed to find updated group")
}

if err := s.updateGroupCacheByUserId(joiningGroup); err != nil {
s.log.Named("Join").Error("updateGroupCacheByUserId: joiningGroup", zap.Error(err))
return nil, status.Error(codes.Internal, "failed to update joiningGroup cache")
Expand Down

0 comments on commit 03700f8

Please sign in to comment.