From 9d8fb34dc1efb30bab5e27f2f80accd7483be13f Mon Sep 17 00:00:00 2001 From: HeeSung98 Date: Sat, 18 Nov 2023 10:28:24 +0900 Subject: [PATCH] =?UTF-8?q?=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 19 +++++++------- .../user/repository/UserRepository.java | 4 +-- .../com/weather/user/service/UserService.java | 2 ++ .../weather/user/service/UserServiceImpl.java | 26 ++++++++++++++++++- .../com/weather/user/repository/UserTest.java | 1 + 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/user/src/main/java/com/weather/user/controller/UserController.java b/user/src/main/java/com/weather/user/controller/UserController.java index 8d6e476..aa64d47 100644 --- a/user/src/main/java/com/weather/user/controller/UserController.java +++ b/user/src/main/java/com/weather/user/controller/UserController.java @@ -8,10 +8,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PatchMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.*; import java.util.Optional; @@ -49,17 +46,19 @@ public ResponseEntity profile(@RequestBody UserDTO userDTO) { } @PatchMapping("/user/profile/modify") - public ResponseEntity modify(@RequestBody UserDTO userDTO) { + public ResponseEntity modify(@RequestBody UserDTO userDTO) { log.info("userDTO: " + userDTO); - return new ResponseEntity(HttpStatus.OK); + UserDTO result = userService.modify(userDTO); + + return new ResponseEntity<>(result, HttpStatus.OK); } - @DeleteMapping("/user/profile/remove") - public ResponseEntity remove(@RequestBody UserDTO userDTO) { - log.info("userDTO: " + userDTO); + @DeleteMapping("/user/profile/remove/{email}") + public ResponseEntity remove(@PathVariable String email) { + log.info("email: " + email); - userService.remove(userDTO.getEmail()); + userService.remove(email); return new ResponseEntity(HttpStatus.OK); } diff --git a/user/src/main/java/com/weather/user/repository/UserRepository.java b/user/src/main/java/com/weather/user/repository/UserRepository.java index 8dba0d3..ea1f264 100644 --- a/user/src/main/java/com/weather/user/repository/UserRepository.java +++ b/user/src/main/java/com/weather/user/repository/UserRepository.java @@ -8,10 +8,10 @@ import java.util.Optional; public interface UserRepository extends JpaRepository { - @Query("select m from User m where m.fromSocial = :social and m.email = :email") + @Query("select u from User u where u.fromSocial = :social and u.email = :email") Optional findByEmail(@Param("email") String email, @Param("social") boolean social); - @Query("select m from User m where m.email = :email") + @Query("select u from User u where u.email = :email") Optional findByEmail(@Param("email") String email); } diff --git a/user/src/main/java/com/weather/user/service/UserService.java b/user/src/main/java/com/weather/user/service/UserService.java index e631b44..ceafe7c 100644 --- a/user/src/main/java/com/weather/user/service/UserService.java +++ b/user/src/main/java/com/weather/user/service/UserService.java @@ -15,6 +15,8 @@ public interface UserService { UserDTO profile(String email); + UserDTO modify(UserDTO userDTO); + void remove(String email); default User dtoToEntity(UserDTO userDTO) { diff --git a/user/src/main/java/com/weather/user/service/UserServiceImpl.java b/user/src/main/java/com/weather/user/service/UserServiceImpl.java index b6511a3..591746e 100644 --- a/user/src/main/java/com/weather/user/service/UserServiceImpl.java +++ b/user/src/main/java/com/weather/user/service/UserServiceImpl.java @@ -29,6 +29,7 @@ public void signup(UserDTO userDTO) { } userDTO.setPassword(passwordEncoder.encode(userDTO.getPassword())); + userDTO.setStatus(true); User user = dtoToEntity(userDTO); userRepository.save(user); } @@ -63,7 +64,7 @@ public UserDTO profile(String email) { } User user = optionalUser.get(); - if(user.isStatus()) { + if(!user.isStatus()) { throw new Error("탈퇴 대기중인 유저입니다."); } @@ -71,6 +72,28 @@ public UserDTO profile(String email) { return result; } + @Override + public UserDTO modify(UserDTO userDTO) { + Optional optionalUser = userRepository.findByEmail(userDTO.getEmail()); + log.info("optionalUser: " + optionalUser); + + if(optionalUser.isEmpty()) { + throw new Error("존재하지 않는 유저입니다."); + } + User user = optionalUser.get(); + + if(user.isStatus()) { + user.changeNickname(userDTO.getNickname()); + user.changeImage(userDTO.getImage()); + userRepository.save(user); + + UserDTO result = entityToDTO(user); + return result; + } else { + throw new Error("잘못 된 접근입니다."); + } + } + @Override public void remove(String email) { Optional optionalUser = userRepository.findByEmail(email); @@ -83,6 +106,7 @@ public void remove(String email) { if(user.isStatus()) { user.changeStatus(); + userRepository.save(user); } else { throw new Error("잘못 된 접근입니다."); } diff --git a/user/src/test/java/com/weather/user/repository/UserTest.java b/user/src/test/java/com/weather/user/repository/UserTest.java index d71e7ec..8b07cd3 100644 --- a/user/src/test/java/com/weather/user/repository/UserTest.java +++ b/user/src/test/java/com/weather/user/repository/UserTest.java @@ -22,6 +22,7 @@ void insertUser() { .name("USER" + i) .nickname("테스터") .fromSocial(false) + .status(true) .build(); userRepository.save(user);