3rd seminar : 기본과제 + 심화과제#3
Conversation
jiyeoon00
left a comment
There was a problem hiding this comment.
수화 수고했어ㅎㅎ 뭔가 리뷰하면서 공부를 엄청 하게 만들어 주는 코드라 아주 좋았어👍
|
|
||
| public static ApiResponseDto success(SuccessStatus successStatus) { //왜 스태틱? : 이유 response에 대한 객체가 계속 생성될 필요가 없음 | ||
| return new ApiResponseDto<>(successStatus.getHttpStatus().value(), successStatus.getMessage()); | ||
| } |
There was a problem hiding this comment.
P4) response에 대한 객체가 계속 생성될 필요가 없음 => 이 말이 좀 이해가 안가는게..! 나는 결국 리턴 값으로 new 해서 response 객체를 계속 생성한다고 생각했어!
static메서드로 정의한 이유는 사용부 코드에서 바로 new ApiResponseDto(successStatus)이렇게 생성하는 게 아니라 ApiResponseDto.success(successStatus) 이런식으로 메서드를 호출해서 그 안에서 객체를 생성해서 리턴해주는 패턴을 사용하기 때문에 static 메서드를 사용한거라 생각했어!!
그리고 저 패턴을 처음 봐서 사용 이유를 생각해봤는데, success, error 처럼 메서드 이름으로 생성자의 의미를 명확이 보여줘서 좋은 거 같다고 생각했는데, 수화 생각은 어때?!
| @Autowired | ||
| public PostService(UserRepository userRepository, PostRepository postRepository){ | ||
| this.userRepository = userRepository; | ||
| this.postRepository=postRepository; | ||
| } |
There was a problem hiding this comment.
@requiredargsconstructor 의미가 final붙은 필드 생성자 주입해주는 거라 여기 없어도 되지 않을까?
There was a problem hiding this comment.
P3) 나도 헷갈려서 찾아봤는데, @requiredargsconstructor가 @Autowired가 하는 의존성 주입도 다 해준대! 생성자 빼도 될듯!!
| protected ApiResponseDto SQLIntegrityConstraintViolationException(final SQLIntegrityConstraintViolationException e){ | ||
|
|
||
| if (e.getMessage().contains("'user_id' cannot be null")){ | ||
| return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_UNVALID_USERID); | ||
| } | ||
| else if(e.getMessage().contains("Duplicate entry")){ | ||
| if (e.getMessage().contains("user.UK_ob8kqyqqgmefl0aco34akdtpe")) { | ||
| return ApiResponseDto.error(ErrorStatus.CONFLICT_EMAIL_EXCEPTION); | ||
| } else if (e.getMessage().contains("user.UK_n4swgcf30j6bmtb4l4cjryuym")) { | ||
| return ApiResponseDto.error(ErrorStatus.CONFLICT_NICKNAME_EXCEPTION); | ||
| } | ||
| } |
There was a problem hiding this comment.
P4) SQLIntegrityConstraintViolationException로 다 잡아서 그 안에서 메세지?를 꺼내서 어떤 에러인지 확인해주는 것 같은데, user.UK_ob8kqyqqgmefl0aco34akdtpe➡️이런문자열이 무슨 의미인지도 잘 모르겠고 만약 다른 DB로 바꾸더라도 저 값이 똑같을까...? 라는 생각이 들었어,,
찾아보니까 스프링은 SQLException을 한번 포장해서 DataAccessException로 던져준다고 하는데 이 키워드로 검색해봐도 좋을 것 같아! 나도 여기 좀 공부해보고 피드백 추가할 부분 있으면 추가할게!
There was a problem hiding this comment.
P4) 우와.. 나도 처음에는 unique 제약조건이 위배됐을 때 SQLIntegrityConstraintViolationException이 뜨길래 이걸 활용해서 제어해볼까 했는데, 저 message가 너무 복잡한 것 같아서 포기했었거든.. 근데 나름 규칙성이 있었구나?? 대단대단!
근데 내 생각에도 이렇게 하면 DB에 의존적인 코드가 되기도 하고, 더욱 다양한 예외를 처리하기 어려울 것 같아! DataAccessException도 찾아봤는데, 아직 예외가 명확하게 추상화되어있지 않다고 하네...
| protected ApiResponseDto SQLIntegrityConstraintViolationException(final SQLIntegrityConstraintViolationException e){ | ||
|
|
||
| if (e.getMessage().contains("'user_id' cannot be null")){ | ||
| return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_UNVALID_USERID); | ||
| } | ||
| else if(e.getMessage().contains("Duplicate entry")){ | ||
| if (e.getMessage().contains("user.UK_ob8kqyqqgmefl0aco34akdtpe")) { | ||
| return ApiResponseDto.error(ErrorStatus.CONFLICT_EMAIL_EXCEPTION); | ||
| } else if (e.getMessage().contains("user.UK_n4swgcf30j6bmtb4l4cjryuym")) { | ||
| return ApiResponseDto.error(ErrorStatus.CONFLICT_NICKNAME_EXCEPTION); | ||
| } | ||
| } |
There was a problem hiding this comment.
P4) 우와.. 나도 처음에는 unique 제약조건이 위배됐을 때 SQLIntegrityConstraintViolationException이 뜨길래 이걸 활용해서 제어해볼까 했는데, 저 message가 너무 복잡한 것 같아서 포기했었거든.. 근데 나름 규칙성이 있었구나?? 대단대단!
근데 내 생각에도 이렇게 하면 DB에 의존적인 코드가 되기도 하고, 더욱 다양한 예외를 처리하기 어려울 것 같아! DataAccessException도 찾아봤는데, 아직 예외가 명확하게 추상화되어있지 않다고 하네...
| @Autowired | ||
| public PostService(UserRepository userRepository, PostRepository postRepository){ | ||
| this.userRepository = userRepository; | ||
| this.postRepository=postRepository; | ||
| } |
There was a problem hiding this comment.
P3) 나도 헷갈려서 찾아봤는데, @requiredargsconstructor가 @Autowired가 하는 의존성 주입도 다 해준대! 생성자 빼도 될듯!!
| */ | ||
| CONFLICT_EMAIL_EXCEPTION(HttpStatus.CONFLICT, "이미 등록된 이메일입니다."), | ||
| CONFLICT_NICKNAME_EXCEPTION(HttpStatus.CONFLICT, "이미 등록된 닉네임입니다."), | ||
|
|
There was a problem hiding this comment.
P3) 유저나 게시물 조회할 때에 대한 예외처리도 하면 좋을 것 같아!!
| } else if (e.getMessage().contains("user.UK_n4swgcf30j6bmtb4l4cjryuym")) { | ||
| return ApiResponseDto.error(ErrorStatus.CONFLICT_NICKNAME_EXCEPTION); | ||
| } | ||
| } |
| @Column(nullable = false) | ||
| @Column(nullable = false, unique = true) | ||
| private String email; | ||
|
|
기본과제
세미나 때 코드 그대로 입니당.
ApiResponseDto와 ControllerAdvice를 새로 배웠고 편하게 에러처리를 할 수 있어서 좋았습니다.
심화과제
기본과제에
를 했습니다.