회원수정 메소드 추가 및 SignupRequest 이메일 어노테이션 수정
This commit is contained in:
parent
4878ecae97
commit
7014175652
|
@ -98,6 +98,7 @@ public class AuthController {
|
|||
|
||||
UserEntity result = userRepository.save(user);
|
||||
|
||||
|
||||
URI location = ServletUriComponentsBuilder.fromCurrentContextPath().path("/users/{username}")
|
||||
.buildAndExpand(result.getUsername()).toUri();
|
||||
|
||||
|
|
|
@ -23,7 +23,6 @@ public class SignupRequest {
|
|||
|
||||
@NotBlank
|
||||
@Size(max = 40)
|
||||
@Email
|
||||
private String email;
|
||||
|
||||
@NotBlank
|
||||
|
|
|
@ -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>(
|
||||
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<UserEntity> 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>(
|
||||
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));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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<UserEntity> getUsersByPageable(String username, Pageable pageable) {
|
||||
Page<UserEntity> 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user