From e32b0cf31da5db8670fe99f9ee73da12d99add10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ben=20B=C3=B6ckmann?= <75972296+bencodes07@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:35:07 +0200 Subject: [PATCH] refactor --- .../com/MeetMate/user/UserController.java | 29 ++++++++------ .../java/com/MeetMate/user/UserService.java | 38 ++++++------------- 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/MeetMate/user/UserController.java b/src/main/java/com/MeetMate/user/UserController.java index db06a1d..35da5cf 100644 --- a/src/main/java/com/MeetMate/user/UserController.java +++ b/src/main/java/com/MeetMate/user/UserController.java @@ -7,9 +7,12 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.InternalAuthenticationServiceException; +import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; +import java.util.Arrays; + @RestController @RequestMapping(path = "api/user") @RequiredArgsConstructor @@ -49,23 +52,25 @@ public ResponseEntity getAllUsers() { @PostMapping(path = "signup") @ResponseBody - public ResponseEntity registerNewUser(@RequestParam MultiValueMap data) { + public ResponseEntity registerNewUser( + @RequestParam String email, + @RequestParam String password, + @RequestParam String name) { + + System.out.println("Received signup request for email: " + email); + try { + MultiValueMap data = new LinkedMultiValueMap<>(); + data.add("email", email); + data.add("password", password); + data.add("name", name); + userService.registerNewUser(data); return ResponseEntity.ok().build(); - } catch (Throwable t) { - Class tc = t.getClass(); - - if (tc == IllegalArgumentException.class) - return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body("message: " + t.getMessage()); - - if (tc == NameAlreadyBoundException.class) - return ResponseEntity.status(HttpStatus.CONFLICT) - .body("message: " + t.getMessage()); - + System.out.println("Error in registerNewUser " + t); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body("message: " + t.getMessage()); + .body("message: " + t.getMessage() + "\nStack trace: " + Arrays.toString(t.getStackTrace())); } } diff --git a/src/main/java/com/MeetMate/user/UserService.java b/src/main/java/com/MeetMate/user/UserService.java index bc8dd4e..4d96c7a 100644 --- a/src/main/java/com/MeetMate/user/UserService.java +++ b/src/main/java/com/MeetMate/user/UserService.java @@ -53,53 +53,39 @@ public List getAllUsers() { @Transactional public void registerNewUser(MultiValueMap data) throws NameAlreadyBoundException { - System.out.println("Received registration data: " + data); - String email = data.getFirst("email"); String name = data.getFirst("name"); String password = data.getFirst("password"); String role = data.getFirst("role"); - System.out.println("Parsed email: " + email); - System.out.println("Parsed name: " + name); - System.out.println("Parsed password: " + password); - + // Make associatedCompany truly optional Long associatedCompany = null; String associatedCompanyStr = data.getFirst("associatedCompany"); - System.out.println("Associated company string: " + associatedCompanyStr); - if (associatedCompanyStr != null && !associatedCompanyStr.isEmpty()) { try { associatedCompany = Long.parseLong(associatedCompanyStr); - System.out.println("Parsed associatedCompany: " + associatedCompany); } catch (NumberFormatException e) { - System.out.println("ERROR: Failed to parse associatedCompany" + e); throw new IllegalArgumentException("Invalid associatedCompany value", e); } - } else { - System.out.println("No associatedCompany provided"); } - UserRole userRole = (role == null || role.isEmpty()) ? UserRole.CLIENT : UserRole.valueOf(role); + // Validate required fields if (email == null || email.isEmpty() || password == null || password.isEmpty() || name == null || name.isEmpty()) { - throw new IllegalArgumentException("Required argument is missing"); + throw new IllegalArgumentException("Email, password, and name are required"); } - if (userRepository.findUserByEmail(email).isPresent()) { - throw new NameAlreadyBoundException("Email already taken"); - } + // Set default role if not provided + UserRole userRole = (role == null || role.isEmpty()) ? UserRole.CLIENT : UserRole.valueOf(role); User user = new User(name, email, passwordEncoder.encode(password), userRole); - switch (userRole) { - case COMPANY_OWNER, COMPANY_MEMBER -> { - if (associatedCompany == null) { - throw new IllegalArgumentException("associatedCompany is required for COMPANY_OWNER and COMPANY_MEMBER roles"); - } - user.setAssociatedCompany(associatedCompany); - } - case CLIENT -> user.setAssociatedCompany(-1L); - default -> throw new IllegalStateException(role + " is invalid!"); + // Only set associatedCompany if it's provided + if (associatedCompany != null) { + user.setAssociatedCompany(associatedCompany); + } else if (userRole == UserRole.CLIENT) { + user.setAssociatedCompany(-1L); + } else if (userRole == UserRole.COMPANY_OWNER || userRole == UserRole.COMPANY_MEMBER) { + throw new IllegalArgumentException("associatedCompany is required for COMPANY_OWNER and COMPANY_MEMBER roles"); } userRepository.save(user);