Fixes one endpoint (deleting of user still need to be fixed)

This commit is contained in:
szsa
2022-03-26 21:59:54 +01:00
parent 3e427cb1f0
commit 82fe7e5e74
2 changed files with 11 additions and 12 deletions

View File

@@ -79,17 +79,18 @@ public class TeamService {
.findById(teamId)
.flatMap(team -> {
// at first, I'm changing teamId of all team members to null and save it in db
// at first, I'm changing teamId of all team members to null
var membersToUpdate = TeamUtils.toMembers.apply(team)
.stream()
.map(member -> member.withTeamId(null))
.toList();
userRepository.saveAll(membersToUpdate);
// then I'm deleting team and returning mono of DTO
// then I'm saving all updated members, deleting team and returning mono of DTO
return teamRepository.delete(teamId).map(Team::toGetTeamDto);
return userRepository.saveAll(membersToUpdate)
.then(teamRepository.delete(teamId))
.then(Mono.just(team.toGetTeamDto()));
})
.switchIfEmpty(Mono.error(new TeamServiceException("cannot find team to delete")));
}

View File

@@ -56,21 +56,19 @@ public class UserService {
return userRepository
.findById(userId)
.flatMap(user -> {
// when teamId is not null then list of members in related team will be updated
String teamId = UserUtils.toTeamId.apply(user);
if (teamId != null) {
teamRepository
return teamRepository
.findById(teamId)
.flatMap(team -> {
TeamUtils.toMembers.apply(team).remove(user);
return teamRepository.save(team);
});
}
userRepository.delete(userId);
return Mono.just(user.toGetUserDto());
})
.then(userRepository.delete(userId))
.then(Mono.just(user.toGetUserDto());
.switchIfEmpty(Mono.error(new UserServiceException("cannot find user to delete")));
}
}