Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bencodes-fixes #54

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/main/java/com/MeetMate/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,23 +52,25 @@ public ResponseEntity<?> getAllUsers() {

@PostMapping(path = "signup")
@ResponseBody
public ResponseEntity<?> registerNewUser(@RequestParam MultiValueMap<String, String> data) {
public ResponseEntity<?> registerNewUser(
@RequestParam String email,
@RequestParam String password,
@RequestParam String name) {

System.out.println("Received signup request for email: " + email);

try {
MultiValueMap<String, String> 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<? extends Throwable> 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()));
}
}

Expand Down
38 changes: 12 additions & 26 deletions src/main/java/com/MeetMate/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,53 +53,39 @@ public List<User> getAllUsers() {

@Transactional
public void registerNewUser(MultiValueMap<String, String> 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);
Expand Down
Loading