Skip to content
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

Merged
merged 8 commits into from
Aug 7, 2024
Merged

[Feat/85] 매너 평가 등록 API 구현 #101

merged 8 commits into from
Aug 7, 2024

Conversation

hzee97
Copy link
Contributor

@hzee97 hzee97 commented Jul 31, 2024

🚀 개요

매너 평가 등록 API 구현
비매너 평가 등록 API 구현

🔍 변경사항

  • 매너평가 관련 ERD 수정.
  • 매너 평가 등록 API 구현.
  • 비매너 평가 등록 API 구현.
  • 코드 리팩토링

⏳ 작업 내용

  • 매너 평가 등록 API 구현.
  • 비매너 평가 등록 API 구현.
  • Swagger 명세.
  • Valid 추가, 예외처리수정, Swagger 명세 수정.
  • 연관관계 매핑 수정.

📝 논의사항

@hzee97 hzee97 requested review from rimi3226 and Eunjin3395 July 31, 2024 19:39
@hzee97 hzee97 self-assigned this Jul 31, 2024
@hzee97 hzee97 linked an issue Jul 31, 2024 that may be closed by this pull request
4 tasks
@PostMapping("/good")
@Operation(summary = "매너 평가 등록 API", description = "매너 평가하기 API 입니다.")
public ApiResponse<MannerResponse.mannerInsertResponseDTO> mannerInsert(
@RequestBody MannerRequest.mannerInsertDTO request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Valid 어노테이션 넣어야 @notempty, @notblank 검증 되는걸로 알고있습니당..!

Copy link
Contributor Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앞선 리뷰와 동일하게 @Valid 넣으면 좋을 것 같습니다!

Comment on lines 45 to 58
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);
}
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분을 그냥 MannerRating 엔티티에 isPositive 칼럼을 추가하고,

Suggested change
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);
}

이런식으로 변경하는 건 어떨까요..?! 이 코드가 정상 작동할지 테스트는 못해봤는데 대충 이런 방식도 가능할 것 같아서 남깁니다..!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 ERD - 매너 키워드에 isPositive 컬럼이 존재하는데, MannerRating에도 추가를 했으면 좋겠다는 말씀이실까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[코드 리팩토링 완료]

Comment on lines 61 to 73
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);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분도 그냥 forEach 루프 한번에 할 수 있을 것 같아요!

Suggested change
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 말고 커스텀 에러 하나 생성해서 에러 발생시켜주는게 좋을 것 같습니다!

Copy link
Contributor Author

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){
Copy link
Member

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<>();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[수정 완료]

Comment on lines 76 to 81
MannerRating mannerRating = MannerRating.builder()
.toMember(targetMember)
.mannerRatingKeywordList(new ArrayList<>())
.build();

mannerRating.setFromMember(member);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromMember가 단방향 연관관계이고, toMember가 양방향 연관관계입니다! 매핑 부분 수정 필요합니다

Copy link
Contributor Author

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 입니다.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mannerRatingKeywordList에 올 수 있는 값의 범위 설명도 적어주면 좋을 것 같습니다!

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기에서 @Setter를 따로 추가하신 이유가 있으신가요?!

@hzee97 hzee97 requested a review from Eunjin3395 August 5, 2024 17:04
@rimi3226 rimi3226 merged commit a612923 into develop Aug 7, 2024
@rimi3226 rimi3226 deleted the feat/85 branch August 7, 2024 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

✨ [Feat] 매너 평가 등록 API 구현
3 participants