diff --git a/src/main/java/com/backend/glowhouse/controller/UserController.java b/src/main/java/com/backend/glowhouse/controller/UserController.java index a98dda7acda80f13e4ed73def75957675dddf00a..9e0bd3cba4389c4a48eec16e85a689b9d3de1b26 100644 --- a/src/main/java/com/backend/glowhouse/controller/UserController.java +++ b/src/main/java/com/backend/glowhouse/controller/UserController.java @@ -1,12 +1,10 @@ package com.backend.glowhouse.controller; -import com.backend.glowhouse.model.exception.ResourceNotFoundException; -import com.backend.glowhouse.model.response.UserIdentityAvailability; -import com.backend.glowhouse.model.response.UserProfile; +import com.backend.glowhouse.model.response.UserAvailability; import com.backend.glowhouse.model.response.UserSummary; -import com.backend.glowhouse.model.User; -import com.backend.glowhouse.repository.UserRepository; +import com.backend.glowhouse.repository.user.UserRepository; import com.backend.glowhouse.security.UserPrincipal; +import com.backend.glowhouse.service.UserService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; @@ -19,38 +17,34 @@ import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api") public class UserController { - ObjectMapper mapper = new ObjectMapper(); + private ObjectMapper mapper = new ObjectMapper(); @Autowired private UserRepository userRepository; + @Autowired + private UserService userService; private static final Logger logger = LoggerFactory.getLogger(UserController.class); @GetMapping("/user/details") @PreAuthorize("hasRole('USER')") public String getCurrentUserDetails(@AuthenticationPrincipal UserPrincipal currentUser) throws JsonProcessingException { - UserSummary userSummary = new UserSummary(currentUser.getId(), currentUser.getUsername(), currentUser.getName()); - - return mapper.writeValueAsString(userSummary); + return userService.getCurrentUser(currentUser); } @GetMapping("/user/checkUsernameAvailability") - public UserIdentityAvailability checkUsernameAvailability(@RequestParam(value = "username") String username) { - Boolean isAvailable = !userRepository.existsByUsername(username); - return new UserIdentityAvailability(isAvailable); + public UserAvailability checkUsernameAvailability(@RequestParam(value = "username") String username) { + return userService.checkUsernameAvailability(username); } @GetMapping("/user/checkEmailAvailability") - public UserIdentityAvailability checkEmailAvailability(@RequestParam(value = "email") String email) { - Boolean isAvailable = !userRepository.existsByEmail(email); - return new UserIdentityAvailability(isAvailable); + public UserAvailability checkEmailAvailability(@RequestParam(value = "email") String email) { + return userService.checkEmailAvailability(email); } - @GetMapping("/users/{username}") - public UserProfile getUserProfile(@PathVariable(value = "username") String username) { - User user = userRepository.findByUsername(username) - .orElseThrow(() -> new ResourceNotFoundException("User", "username", username)); - - return new UserProfile(user.getId(), user.getUsername(), user.getName()); + @GetMapping("/user/getProfile") + @PreAuthorize("hasRole('USER')") + public String getUserProfile(@RequestParam(value = "username") String username) throws JsonProcessingException { + return userService.retrieveUserProfile(username); } } \ No newline at end of file