I'm new to Java and this noob function has been bothering me. I have downloaded sonarLint and lo and behold it throws this issue on my face Cognitive Complexity of methods should not be too high. I know it looks ugly but can anyone point out how I can reformat to have dry concept and also not have high complexity as mentioned by the SonarLint.
@PostMapping("/adduser")
// @PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<MessageResponse> registerUser(@Valid @RequestBody SignupRequest signUpRequest) {
/*
* This controller Creates new user based on all the entities for the user
*
*/
if(dbValue)
{
if (repository.existsByUsername(signUpRequest.getUsername())) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: Username is already taken!"));
}
if (repository.existsByEmail(signUpRequest.getEmail())) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: Email is already in use!"));
}
// Create new user's account
User2 user = new User2(signUpRequest.getUsername(),
signUpRequest.getEmail(),
signUpRequest.getCustomername(),
signUpRequest.getCustomerid(),
signUpRequest.getDescription(),
encoder.encode(signUpRequest.getPassword()));
Set<String> strRoles = signUpRequest.getRoles();
Set<Role2> roles = new HashSet<>();
Role2 e = new Role2();
e.setName("ROLE_ADMIN");
roles.add(e);
user.setRoles(roles);
repository.save(user);
}
else {
if (repository.existsByUsername(signUpRequest.getUsername())) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: Username is already taken!"));
}
if (repository.existsByEmail(signUpRequest.getEmail())) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: Email is already in use!"));
}
// Create new user's account
User1 user = new User1(signUpRequest.getUsername(),
signUpRequest.getEmail(),
signUpRequest.getCustomername(),
signUpRequest.getCustomerid(),
signUpRequest.getDescription(),
encoder.encode(signUpRequest.getPassword()));
Set<String> strRoles = signUpRequest.getRoles();
Set<Role> roles = new HashSet<>();
if (strRoles == null) {
Role userRole = roleRepository1.findByName(URole.ROLE_USER)
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
roles.add(userRole);
} else {
strRoles.forEach(role -> {
switch (role) {
case "admin":
Role adminRole = roleRepository1.findByName(URole.ROLE_ADMIN)
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
roles.add(adminRole);
break;
default:
Role userRole = roleRepository1.findByName(URole.ROLE_USER)
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
roles.add(userRole);
}
});
}
user.setRoles(roles);
repository.save(user);
}
return ResponseEntity.ok(new MessageResponse("User Added successfully!"));
}
Appreciate criticism and any help. Thanks in Advance.
Optional check(Predicate condition, String message)
OR delegate this methods to a new validation utility class.signUpRequest.getRoles()
may returns an Optional, orCollections.emptyList()
if an empty, not a nullwith
roleRepository1.findByName(URole.ROLE_USER).ifPresent(roles::add);