-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feat/85] 매너 평가 등록 API 구현 #101
Conversation
@PostMapping("/good") | ||
@Operation(summary = "매너 평가 등록 API", description = "매너 평가하기 API 입니다.") | ||
public ApiResponse<MannerResponse.mannerInsertResponseDTO> mannerInsert( | ||
@RequestBody MannerRequest.mannerInsertDTO request |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[추가완료]
@PostMapping("/bad") | ||
@Operation(summary = "비매너 평가 등록 API", description = "비매너 평가하기 API 입니다.") | ||
public ApiResponse<MannerResponse.mannerInsertResponseDTO> badMannerInsert( | ||
@RequestBody MannerRequest.mannerInsertDTO request |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앞선 리뷰와 동일하게 @Valid 넣으면 좋을 것 같습니다!
List<MannerRating> mannerRatings = mannerRatingRepository.findByToMemberId(targetMember.getId()); | ||
if (!mannerRatings.isEmpty()){ | ||
for (MannerRating mannerRating : mannerRatings) { | ||
if (mannerRating.getFromMember().getId().equals(member.getId())){ | ||
List<MannerRatingKeyword> mannerKeywords = mannerRatingKeywordRepository.findByMannerRating(mannerRating); | ||
if (!mannerKeywords.isEmpty()){ | ||
MannerRatingKeyword mannerKeyword = mannerKeywords.get(0); // 첫번째 키워드만 조회. | ||
if (mannerKeyword.getMannerKeyword().getIsPositive()){ | ||
throw new MannerHandler(ErrorStatus.MANNER_CONFLICT); | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이부분을 그냥 MannerRating 엔티티에 isPositive 칼럼을 추가하고,
List<MannerRating> mannerRatings = mannerRatingRepository.findByToMemberId(targetMember.getId()); | |
if (!mannerRatings.isEmpty()){ | |
for (MannerRating mannerRating : mannerRatings) { | |
if (mannerRating.getFromMember().getId().equals(member.getId())){ | |
List<MannerRatingKeyword> mannerKeywords = mannerRatingKeywordRepository.findByMannerRating(mannerRating); | |
if (!mannerKeywords.isEmpty()){ | |
MannerRatingKeyword mannerKeyword = mannerKeywords.get(0); // 첫번째 키워드만 조회. | |
if (mannerKeyword.getMannerKeyword().getIsPositive()){ | |
throw new MannerHandler(ErrorStatus.MANNER_CONFLICT); | |
} | |
} | |
} | |
} | |
} | |
List<MannerRating> mannerRatings = mannerRatingRepository.findByFromMemberIdAndToMemberId(member.getId(), targetMember.getId()); | |
List<MannerRating> positiveMannerRatings = mannerRatings.stream() | |
.filter(MannerRating::isPositive) | |
.collect(Collectors.toList()); | |
if(!positiveMannerRatings.isEmpty()){ | |
throw new MannerHandler(ErrorStatus.MANNER_CONFLICT); | |
} |
이런식으로 변경하는 건 어떨까요..?! 이 코드가 정상 작동할지 테스트는 못해봤는데 대충 이런 방식도 가능할 것 같아서 남깁니다..!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재 ERD - 매너 키워드에 isPositive 컬럼이 존재하는데, MannerRating에도 추가를 했으면 좋겠다는 말씀이실까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[코드 리팩토링 완료]
List<MannerKeyword> mannerRatingKeywordList = new ArrayList<>(); | ||
request.getMannerRatingKeywordList() | ||
.forEach(mannerKeywordId -> { | ||
MannerKeyword mannerKeyword = mannerKeywordRepository.findById(mannerKeywordId).orElseThrow(() -> new TempHandler(ErrorStatus._BAD_REQUEST)); | ||
mannerRatingKeywordList.add(mannerKeyword); | ||
}); | ||
|
||
// mannerKeyword 유형 검증. | ||
for (Long keyword : request.getMannerRatingKeywordList()){ | ||
if (keyword < 1 || keyword > 6) { | ||
throw new MannerHandler(ErrorStatus.MANNER_KEYWORD_TYPE_INVALID); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분도 그냥 forEach 루프 한번에 할 수 있을 것 같아요!
List<MannerKeyword> mannerRatingKeywordList = new ArrayList<>(); | |
request.getMannerRatingKeywordList() | |
.forEach(mannerKeywordId -> { | |
MannerKeyword mannerKeyword = mannerKeywordRepository.findById(mannerKeywordId).orElseThrow(() -> new TempHandler(ErrorStatus._BAD_REQUEST)); | |
mannerRatingKeywordList.add(mannerKeyword); | |
}); | |
// mannerKeyword 유형 검증. | |
for (Long keyword : request.getMannerRatingKeywordList()){ | |
if (keyword < 1 || keyword > 6) { | |
throw new MannerHandler(ErrorStatus.MANNER_KEYWORD_TYPE_INVALID); | |
} | |
} | |
List<MannerKeyword> mannerRatingKeywordList = new ArrayList<>(); | |
request.getMannerRatingKeywordList() | |
.forEach(mannerKeywordId -> { | |
MannerKeyword mannerKeyword = mannerKeywordRepository.findById(mannerKeywordId) | |
.orElseThrow(() -> new TempHandler(ErrorStatus.MANNER_KEYWORD_NOT_EXIST)); | |
if(!mannerKeyword.getIsPositive()){ | |
throw new MannerHandler(ErrorStatus.MANNER_KEYWORD_TYPE_INVALID) | |
} | |
mannerRatingKeywordList.add(mannerKeyword); | |
}); |
그리고 mannerKeyword를 찾을 수 없을 경우에도 BAD_REQUEST 말고 커스텀 에러 하나 생성해서 에러 발생시켜주는게 좋을 것 같습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[코드 리팩토링 완료]
@@ -31,5 +31,12 @@ public class MannerRating extends BaseDateTimeEntity { | |||
@JoinColumn(name = "to_member_id", nullable = false) | |||
private Member toMember; | |||
|
|||
|
|||
// 연관관계 메소드 | |||
public void setFromMember(Member member){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
member 엔티티의 mannerRatingList는 toMember와 매핑되어 있는데, 여기서는 연관관계 메소드가 FromMember를 세팅하도록 되어 있습니다..!
@OneToMany(mappedBy = "toMember", cascade = CascadeType.ALL) private List<MannerRating> mannerRatingList = new ArrayList<>();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[수정 완료]
MannerRating mannerRating = MannerRating.builder() | ||
.toMember(targetMember) | ||
.mannerRatingKeywordList(new ArrayList<>()) | ||
.build(); | ||
|
||
mannerRating.setFromMember(member); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fromMember가 단방향 연관관계이고, toMember가 양방향 연관관계입니다! 매핑 부분 수정 필요합니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[수정 완료]
private final MannerService mannerService; | ||
|
||
@PostMapping("/good") | ||
@Operation(summary = "매너 평가 등록 API", description = "매너 평가하기 API 입니다.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mannerRatingKeywordList에 올 수 있는 값의 범위 설명도 적어주면 좋을 것 같습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[추가 완료]
public class MannerResponse { | ||
@Getter | ||
@Builder | ||
@Setter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기에서 @Setter를 따로 추가하신 이유가 있으신가요?!
🚀 개요
매너 평가 등록 API 구현
비매너 평가 등록 API 구현
🔍 변경사항
⏳ 작업 내용
📝 논의사항