Skip to content

Commit

Permalink
refactor: POST Method API - ResponseEntity 생성 메서드 구현 #2
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed Apr 21, 2023
1 parent 9eecf45 commit fd2c3f0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class UserController {

@PostMapping("/save")
public ResponseEntity<?> saveUser(@RequestBody UserRequestDto userRequestDto) {
userService.saveUser(userRequestDto);
return new ResponseEntity("유저 저장 완료", HttpStatus.OK);
boolean saveResult = userService.saveUser(userRequestDto);

return generateSaveResponseEntity(saveResult);
}

@GetMapping("/{userId}")
Expand All @@ -46,6 +47,11 @@ public ResponseEntity<?> updateUser(@PathVariable Long userId) {



private ResponseEntity<?> generateSaveResponseEntity(boolean saveResult) {
return (saveResult) ?
new ResponseEntity<>("유저에 대한 정보가 성공적으로 저장 되었습니다.", HttpStatus.OK):
new ResponseEntity<>("유저 등록에 실패하였습니다.", HttpStatus.NO_CONTENT);
}

private ResponseEntity<?> generateUserResponseEntity(Optional<User> user, Long userId) {
return (user.isEmpty()) ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public interface UserService{

void saveUser(UserRequestDto userDto);
boolean saveUser(UserRequestDto userDto);

Optional<User> findUserById(Long userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ public class UserServiceImpl implements UserService {
private final UserRepository userRepository;

@Override
public void saveUser(UserRequestDto userDto) {
userRepository.save(userDto.from());
public boolean saveUser(UserRequestDto userDto) {
boolean saveResult = true;

try {
userRepository.save(userDto.from());
} catch (Exception e) {
saveResult = false;
}

return saveResult;
}

@Override
Expand Down

0 comments on commit fd2c3f0

Please sign in to comment.