From 7014175652e4b192fa44943aa08a22ef0a7bb653 Mon Sep 17 00:00:00 2001 From: byung eun park Date: Thu, 29 Aug 2019 15:53:06 +0900 Subject: [PATCH] =?UTF-8?q?=ED=9A=8C=EC=9B=90=EC=88=98=EC=A0=95=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20?= =?UTF-8?q?SignupRequest=20=EC=9D=B4=EB=A9=94=EC=9D=BC=20=EC=96=B4?= =?UTF-8?q?=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthController.java | 1 + .../server/auth/payload/SignupRequest.java | 1 - .../user/controller/UserController.java | 44 +++++++--- .../user/payload/UserUpdateRequest.java | 36 ++++++++ .../modules/user/service/UserService.java | 83 +++++++++++++++++++ 5 files changed, 152 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/totopia/server/modules/user/payload/UserUpdateRequest.java diff --git a/src/main/java/com/totopia/server/auth/controller/AuthController.java b/src/main/java/com/totopia/server/auth/controller/AuthController.java index 5859546..8335bdc 100644 --- a/src/main/java/com/totopia/server/auth/controller/AuthController.java +++ b/src/main/java/com/totopia/server/auth/controller/AuthController.java @@ -98,6 +98,7 @@ public class AuthController { UserEntity result = userRepository.save(user); + URI location = ServletUriComponentsBuilder.fromCurrentContextPath().path("/users/{username}") .buildAndExpand(result.getUsername()).toUri(); diff --git a/src/main/java/com/totopia/server/auth/payload/SignupRequest.java b/src/main/java/com/totopia/server/auth/payload/SignupRequest.java index eb9f704..a128993 100644 --- a/src/main/java/com/totopia/server/auth/payload/SignupRequest.java +++ b/src/main/java/com/totopia/server/auth/payload/SignupRequest.java @@ -23,7 +23,6 @@ public class SignupRequest { @NotBlank @Size(max = 40) - @Email private String email; @NotBlank diff --git a/src/main/java/com/totopia/server/modules/user/controller/UserController.java b/src/main/java/com/totopia/server/modules/user/controller/UserController.java index 79960e1..acc9a95 100644 --- a/src/main/java/com/totopia/server/modules/user/controller/UserController.java +++ b/src/main/java/com/totopia/server/modules/user/controller/UserController.java @@ -1,7 +1,10 @@ package com.totopia.server.modules.user.controller; +import com.totopia.server.auth.payload.SignupRequest; +import com.totopia.server.commons.data.payload.ApiResponse; import com.totopia.server.commons.exception.ResourceNotFoundException; import com.totopia.server.modules.user.entity.UserEntity; +import com.totopia.server.modules.user.payload.UserUpdateRequest; import com.totopia.server.modules.user.repository.UserRepository; import com.totopia.server.modules.user.service.UserService; import org.springframework.beans.factory.annotation.Autowired; @@ -9,12 +12,11 @@ import org.springframework.data.domain.*; import org.springframework.data.web.SortDefault; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.session.SessionRegistry; import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; +import javax.validation.Valid; +import java.net.URI; @RestController public class UserController { @@ -24,10 +26,18 @@ public class UserController { @Autowired private UserService userService; - @PostMapping(value = "/users") + @PostMapping(value = "/regist") @ResponseStatus(code = HttpStatus.CREATED) - public UserEntity save(@RequestBody UserEntity user) { - return userRepository.save(user); + public ResponseEntity save(@Valid @RequestBody SignupRequest signupRequest) throws Exception{ // User user 파라미터는 추후에 수 + + if (this.userService.existUserByUsername(signupRequest.getUsername())) { + return new ResponseEntity( + ApiResponse.builder().success(false).message("Username is already taken!").build(), HttpStatus.BAD_REQUEST); + } + + this.userService.regist(signupRequest); + + return ResponseEntity.ok().body(new ApiResponse(true, "User registered successfully")); } @GetMapping(value = "/users") @@ -76,13 +86,23 @@ public class UserController { } @PutMapping(value = "/users/{userId}") - public ResponseEntity updateUser(@PathVariable Long userId, @RequestBody UserEntity newUser) { + public ResponseEntity updateUser(@PathVariable Long userId, @RequestBody UserUpdateRequest updateRequest) { - return userRepository.findById(userId).map(user -> { + UserEntity existrEntity = this.userService.getUserById(userId); - userRepository.save(user); - return ResponseEntity.ok(user); - }).orElseThrow(() -> new ResourceNotFoundException("User", "userId", userId)); + if (existrEntity == null) { + return new ResponseEntity( + ApiResponse.builder().success(false).message("User ID does not exist").build(), HttpStatus.BAD_REQUEST); + } + + UserEntity userEntity = this.userService.modifyByUser(existrEntity, updateRequest); + + return ResponseEntity.ok(userEntity); +// return userRepository.findById(userId).map(user -> { +// +// userRepository.save(user); +// return ResponseEntity.ok(user); +// }).orElseThrow(() -> new ResourceNotFoundException("User", "userId", userId)); } diff --git a/src/main/java/com/totopia/server/modules/user/payload/UserUpdateRequest.java b/src/main/java/com/totopia/server/modules/user/payload/UserUpdateRequest.java new file mode 100644 index 0000000..777d2c1 --- /dev/null +++ b/src/main/java/com/totopia/server/modules/user/payload/UserUpdateRequest.java @@ -0,0 +1,36 @@ +package com.totopia.server.modules.user.payload; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class UserUpdateRequest { + @NotBlank + @Size(min = 4, max = 40) + private String username; + + @NotBlank + @Size(min = 3, max = 15) + private String nickname; + + private String email; + + @Size(min = 6, max = 20) + private String password; + + @Size(min = 6, max = 200) + private String descriptions; + + @NotBlank + @Size(min = 6, max = 20) + private String phone; +} diff --git a/src/main/java/com/totopia/server/modules/user/service/UserService.java b/src/main/java/com/totopia/server/modules/user/service/UserService.java index 3477dc5..488fbc9 100644 --- a/src/main/java/com/totopia/server/modules/user/service/UserService.java +++ b/src/main/java/com/totopia/server/modules/user/service/UserService.java @@ -1,19 +1,34 @@ package com.totopia.server.modules.user.service; +import com.totopia.server.auth.payload.SignupRequest; import com.totopia.server.commons.exception.ResourceNotFoundException; +import com.totopia.server.modules.user.entity.RoleEntity; import com.totopia.server.modules.user.entity.UserEntity; +import com.totopia.server.modules.user.payload.UserUpdateRequest; +import com.totopia.server.modules.user.repository.RoleRepository; import com.totopia.server.modules.user.repository.UserRepository; +import com.totopia.server.modules.user.type.RoleName; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import java.util.Collections; +import java.util.Date; + @Service public class UserService { @Autowired private UserRepository userRepository; + @Autowired + RoleRepository roleRepository; + + @Autowired + PasswordEncoder passwordEncoder; + public Page getUsersByPageable(String username, Pageable pageable) { Page entities = null; @@ -26,6 +41,10 @@ public class UserService { return entities; } + public Boolean existUserByUsername(String username) { + return this.userRepository.existsByUsername(username); + } + public UserEntity getUserById(Long id) { UserEntity userEntity = this.userRepository.findById(id) .orElseThrow( () -> new ResourceNotFoundException("User", "userId", id)); @@ -39,4 +58,68 @@ public class UserService { return userId; }).orElseThrow(() -> new ResourceNotFoundException("User", "userId", userId)); } + + public void regist(SignupRequest signupRequest) throws Exception{ + + + // Creating user's account + UserEntity user = UserEntity.builder().username(signupRequest.getUsername()).email(signupRequest.getEmail()) + .nickname(signupRequest.getNickname()).phone(signupRequest.getPhone()).descriptions(signupRequest.getDescriptions()) + .password(signupRequest.getPassword()).block(false).resetCount(0L).sendEmail(true).build(); + + user.setPassword(passwordEncoder.encode(user.getPassword())); + + RoleEntity userRole = roleRepository.findByName(RoleName.ROLE_USER) + .orElseThrow(() -> new Exception("User Role not set.")); + + user.setRoles(Collections.singleton(userRole)); + + UserEntity result = userRepository.save(user); + } + + public UserEntity modifyByUser(UserEntity existEntity, UserUpdateRequest updateRequest) { + + String existNickname = existEntity .getNickname(); + String existPhone = existEntity .getPhone(); + String existEmail = existEntity .getEmail(); + String existPassword = existEntity .getPassword(); + String existDescriptions = existEntity .getDescriptions(); + + String newNickname = updateRequest.getNickname(); + String newPhone = updateRequest.getPhone(); + String newEmail = updateRequest.getEmail(); + String newPassword = updateRequest.getPassword(); + String newDescriptions = updateRequest.getDescriptions(); + + if (newNickname.equals(existNickname) + && newPhone.equals(existPhone) + && newEmail.equals(existEmail) + && this.passwordEncoder.matches(newPassword,existPassword) + && newDescriptions.equals(existDescriptions) + ) { + return existEntity; + } + + if (!newNickname.equals("") && !newNickname.equals(existNickname)) { + existEntity.setNickname(existEntity.getNickname()); + } + if (!newPhone.equals("") && !newPhone.equals(existPhone)) { + existEntity.setPhone(existEntity.getPhone()); + } + if (!newEmail.equals("") && !newEmail.equals(existEmail)) { + existEntity.setEmail(existEntity.getEmail()); + } + + if (!newPassword.equals("") && !this.passwordEncoder.matches(newPassword,existPassword)) { + existEntity.setPassword(passwordEncoder.encode(newPassword)); + existEntity.setLastResetTime(new Date()); + } + if (!newDescriptions.equals("") && !newDescriptions.equals(existDescriptions)) { + existEntity.setDescriptions(existEntity.getDescriptions()); + } + + existEntity.setUpdatedAt(new Date()); + + this.userRepository.save(existEntity); + } }