Skip to content
Snippets Groups Projects
Commit cb818a67 authored by Luke O'mahony's avatar Luke O'mahony
Browse files

upd: moved business logic from user controller to service

parent f65192eb
Branches
No related merge requests found
package com.backend.glowhouse.controller; package com.backend.glowhouse.controller;
import com.backend.glowhouse.model.exception.ResourceNotFoundException; import com.backend.glowhouse.model.response.UserAvailability;
import com.backend.glowhouse.model.response.UserIdentityAvailability;
import com.backend.glowhouse.model.response.UserProfile;
import com.backend.glowhouse.model.response.UserSummary; import com.backend.glowhouse.model.response.UserSummary;
import com.backend.glowhouse.model.User; import com.backend.glowhouse.repository.user.UserRepository;
import com.backend.glowhouse.repository.UserRepository;
import com.backend.glowhouse.security.UserPrincipal; import com.backend.glowhouse.security.UserPrincipal;
import com.backend.glowhouse.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -19,38 +17,34 @@ import org.springframework.web.bind.annotation.*; ...@@ -19,38 +17,34 @@ import org.springframework.web.bind.annotation.*;
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api")
public class UserController { public class UserController {
ObjectMapper mapper = new ObjectMapper(); private ObjectMapper mapper = new ObjectMapper();
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
@Autowired
private UserService userService;
private static final Logger logger = LoggerFactory.getLogger(UserController.class); private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@GetMapping("/user/details") @GetMapping("/user/details")
@PreAuthorize("hasRole('USER')") @PreAuthorize("hasRole('USER')")
public String getCurrentUserDetails(@AuthenticationPrincipal UserPrincipal currentUser) throws JsonProcessingException { public String getCurrentUserDetails(@AuthenticationPrincipal UserPrincipal currentUser) throws JsonProcessingException {
UserSummary userSummary = new UserSummary(currentUser.getId(), currentUser.getUsername(), currentUser.getName()); return userService.getCurrentUser(currentUser);
return mapper.writeValueAsString(userSummary);
} }
@GetMapping("/user/checkUsernameAvailability") @GetMapping("/user/checkUsernameAvailability")
public UserIdentityAvailability checkUsernameAvailability(@RequestParam(value = "username") String username) { public UserAvailability checkUsernameAvailability(@RequestParam(value = "username") String username) {
Boolean isAvailable = !userRepository.existsByUsername(username); return userService.checkUsernameAvailability(username);
return new UserIdentityAvailability(isAvailable);
} }
@GetMapping("/user/checkEmailAvailability") @GetMapping("/user/checkEmailAvailability")
public UserIdentityAvailability checkEmailAvailability(@RequestParam(value = "email") String email) { public UserAvailability checkEmailAvailability(@RequestParam(value = "email") String email) {
Boolean isAvailable = !userRepository.existsByEmail(email); return userService.checkEmailAvailability(email);
return new UserIdentityAvailability(isAvailable);
} }
@GetMapping("/users/{username}") @GetMapping("/user/getProfile")
public UserProfile getUserProfile(@PathVariable(value = "username") String username) { @PreAuthorize("hasRole('USER')")
User user = userRepository.findByUsername(username) public String getUserProfile(@RequestParam(value = "username") String username) throws JsonProcessingException {
.orElseThrow(() -> new ResourceNotFoundException("User", "username", username)); return userService.retrieveUserProfile(username);
return new UserProfile(user.getId(), user.getUsername(), user.getName());
} }
} }
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment