From cb818a675a782550342a19c6dd14d2ffaa709850 Mon Sep 17 00:00:00 2001
From: Luke <lomah001@gold.ac.uk>
Date: Mon, 13 May 2019 12:40:33 +0100
Subject: [PATCH] upd: moved business logic from user controller to service

---
 .../glowhouse/controller/UserController.java  | 36 ++++++++-----------
 1 file changed, 15 insertions(+), 21 deletions(-)

diff --git a/src/main/java/com/backend/glowhouse/controller/UserController.java b/src/main/java/com/backend/glowhouse/controller/UserController.java
index a98dda7..9e0bd3c 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
-- 
GitLab