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;
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
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