This is my code and I am here findByPhoneNumber of UserDetails, if it present returned sucess, if isn't returned error, but I want to write more cleanable,which using ternary operation
I tried many times but I couldn't do it.
@Override
public ResponseEntity<? extends UserDetailResponse> getByPhoneNumberUserDetail(String phoneNumber) {
UserDetail userDetail = userDetailRepository.findByPhoneNumber(phoneNumber);
if (!userDetail.getPhoneNumber().equals(phoneNumber)) {
return new ResponseEntity<>(new UserDetailResponse(MessageCase.COULDNT_FOUNDED_USER_DETAILS_SUCH_PHONE_NUMBER.getMessage(), 400), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(new UserDetailResponse(MessageCase.USER_DETAL_SUCESSFULLY_FOUNDED.getMessage(), 200), HttpStatus.OK);
}
CodePudding user response:
UserDetailResponse userDetailResponse = userDetail!=null && !userDetail.getPhoneNumber().equals(phoneNumber) ? new UserDetailResponse(MessageCase.COULDNT_FOUNDED_USER_DETAILS_SUCH_PHONE_NUMBER.getMessage(), 400) : new UserDetailResponse(MessageCase.USER_DETAL_SUCESSFULLY_FOUNDED.getMessage(), 200);
responseEntity = new ResponseEntity<>(UserDetailResponse,UserDetailResponse.getStatus());
return responseEntity;
Implement getStatus in UserDetailResponse to fetch status code.