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/149] 내가 받은 매너 평가 조회 API 구현 #154

Merged
merged 4 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/gamegoo/controller/mannner/MannerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,15 @@ public ApiResponse<MannerResponse.badMannerKeywordResponseDTO> getBadMannerKeywo

return ApiResponse.onSuccess(result);
}

@GetMapping()
@Operation(summary = "내가 받은 매너 평가 조회 API", description = "회원의 매너레벨과 회원이 받은 매너 평가를 조회하는 API 입니다.")
public ApiResponse<MannerResponse.myMannerResponseDTO> getMyManner(){

Long memberId = JWTUtil.getCurrentUserId();

MannerResponse.myMannerResponseDTO result = mannerService.getMyManner(memberId);

return ApiResponse.onSuccess(result);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/gamegoo/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,9 @@ public void updatePassword(String password) {
public void updateRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}

public void setMannerLevel(int mannerLevel) {
this.mannerLevel = mannerLevel;
}
}

19 changes: 19 additions & 0 deletions src/main/java/com/gamegoo/dto/manner/MannerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,23 @@ public static class mannerKeywordResponseDTO{
public static class badMannerKeywordResponseDTO{
List<Long> mannerRatingKeywordList;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class myMannerResponseDTO{
Integer mannerLevel;
List<mannerKeywordDTO> mannerKeywords;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class mannerKeywordDTO{
Boolean isPositive;
Integer mannerKeywordId;
Integer count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import com.gamegoo.domain.manner.MannerRating;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface MannerRatingRepository extends JpaRepository<MannerRating, Long> {
List<MannerRating> findByToMemberId(Long toMember);
List<MannerRating> findByFromMemberIdAndToMemberId(Long fromMember, Long toMember);

}
189 changes: 185 additions & 4 deletions src/main/java/com/gamegoo/service/manner/MannerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand All @@ -32,6 +29,7 @@ public class MannerService {
private final MannerRatingKeywordRepository mannerRatingKeywordRepository;
private final MannerKeywordRepository mannerKeywordRepository;

// 매너평가 등록
public MannerRating insertManner(MannerRequest.mannerInsertDTO request, Long memberId) {

Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
Expand Down Expand Up @@ -84,9 +82,23 @@ public MannerRating insertManner(MannerRequest.mannerInsertDTO request, Long mem
mannerRatingKeyword.setMannerRating(saveManner);
mannerRatingKeywordRepository.save(mannerRatingKeyword);
});

// 매너점수 산정.
int mannerScore = updateMannerScore(targetMember);

// 매너레벨 결정.
int mannerLevel = mannerLevel(mannerScore);

// 매너레벨 반영.
targetMember.setMannerLevel(mannerLevel);

// db 저장.
memberRepository.save(targetMember);

return saveManner;
}

// 비매너평가 등록
public MannerRating insertBadManner(MannerRequest.mannerInsertDTO request, Long memberId) {

Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
Expand Down Expand Up @@ -139,6 +151,19 @@ public MannerRating insertBadManner(MannerRequest.mannerInsertDTO request, Long
mannerRatingKeyword.setMannerRating(saveManner);
mannerRatingKeywordRepository.save(mannerRatingKeyword);
});

// 매너점수 산정.
int mannerScore = updateMannerScore(targetMember);

// 매너레벨 결정.
int mannerLevel = mannerLevel(mannerScore);

// 매너레벨 반영.
targetMember.setMannerLevel(mannerLevel);

// db 저장.
memberRepository.save(targetMember);

return saveManner;
}

Expand All @@ -148,6 +173,8 @@ public MannerRating update(MannerRequest.mannerUpdateDTO request, Long memberId,

MannerRating mannerRating = mannerRatingRepository.findById(mannerId).orElseThrow(() -> new MannerHandler(ErrorStatus.MANNER_NOT_FOUND));

Member targetMember = memberRepository.findById(mannerRating.getToMember().getId()).orElseThrow(() -> new MemberHandler(ErrorStatus.MANNER_TARGET_MEMBER_NOT_FOUND));

// 매너평가 작성자가 맞는지 검증.
if (!mannerRating.getFromMember().getId().equals(memberId)) {
throw new MannerHandler(ErrorStatus.MANNER_UNAUTHORIZED);
Expand Down Expand Up @@ -201,6 +228,19 @@ public MannerRating update(MannerRequest.mannerUpdateDTO request, Long memberId,
mannerRatingKeyword.setMannerKeyword(mannerKeyword);
}
}

// 매너점수 산정.
int mannerScore = updateMannerScore(targetMember);

// 매너레벨 결정.
int mannerLevel = mannerLevel(mannerScore);

// 매너레벨 반영.
targetMember.setMannerLevel(mannerLevel);

// db 저장.
memberRepository.save(targetMember);

return mannerRatingRepository.save(mannerRating);
}

Expand Down Expand Up @@ -252,9 +292,78 @@ public MannerRating update(MannerRequest.mannerUpdateDTO request, Long memberId,
mannerRatingKeyword.setMannerKeyword(mannerKeyword);
}
}

// 매너점수 산정.
int mannerScore = updateMannerScore(targetMember);

// 매너레벨 결정.
int mannerLevel = mannerLevel(mannerScore);

// 매너레벨 반영.
targetMember.setMannerLevel(mannerLevel);

// db 저장.
memberRepository.save(targetMember);

return mannerRatingRepository.save(mannerRating);
}
}

// 매너점수를 산정하고 업데이트.
public int updateMannerScore(Member targetMember){
Copy link
Member

Choose a reason for hiding this comment

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

이건 MannerService 내에서만 사용되는 메소드인 것 같은데, 그럼 private으로 선언하는게 좋을 것 같습니다!


// 매너평가 ID 조회
List<MannerRating> mannerRatings = mannerRatingRepository.findByToMemberId(targetMember.getId());
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를 양방향 매핑으로 가지고 있어서, repository로 조회 안하고 그냥 member.getMannerRatingList로 매너평가 리스트를 뽑아 올 수 있을 것 같아요!


int totalCount;

// 매너 평가 + 비매너 평가를 처음 받은 회원
if (mannerRatings.size()==1){
if (mannerRatings.get(0).getIsPositive()) {
totalCount = mannerRatings.get(0).getMannerRatingKeywordList().stream()
.map(mannerRatingKeyword -> mannerRatingKeyword.getMannerKeyword().getId())
.collect(Collectors.toList()).size();
Copy link
Member

Choose a reason for hiding this comment

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

여기에서 어차피 mannerRatingKeyword 개수만 필요한 거라면, 굳이 map으로 id 변환시키지 않고 바로 list의 size만 뽑아도 될 것 같아요!

Suggested change
totalCount = mannerRatings.get(0).getMannerRatingKeywordList().stream()
.map(mannerRatingKeyword -> mannerRatingKeyword.getMannerKeyword().getId())
.collect(Collectors.toList()).size();
totalCount = mannerRatings.get(0).getMannerRatingKeywordList().size();

} else {
totalCount = (mannerRatings.get(0).getMannerRatingKeywordList().stream()
.map(mannerRatingKeyword -> mannerRatingKeyword.getMannerKeyword().getId())
.collect(Collectors.toList()).size())*-2;
}
} else {
List<Long> positiveMannerKeywordIds = mannerRatings.stream()
.filter(MannerRating::getIsPositive)
.flatMap(mannerRating -> mannerRating.getMannerRatingKeywordList().stream())
.map(mannerRatingKeyword -> mannerRatingKeyword.getMannerKeyword().getId())
.collect(Collectors.toList());
Copy link
Member

Choose a reason for hiding this comment

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

여기도 mannerKeyword의 id가 특별히 필요하지 않다면,

Suggested change
List<Long> positiveMannerKeywordIds = mannerRatings.stream()
.filter(MannerRating::getIsPositive)
.flatMap(mannerRating -> mannerRating.getMannerRatingKeywordList().stream())
.map(mannerRatingKeyword -> mannerRatingKeyword.getMannerKeyword().getId())
.collect(Collectors.toList());
int positiveCount = mannerRatings.stream()
.filter(MannerRating::getIsPositive)
.flatMap(mannerRating -> mannerRating.getMannerRatingKeywordList().stream())
.collect(Collectors.toList())
.size();

이렇게 수정해도 될 것 같습니다!


int positiveCount = positiveMannerKeywordIds.size();

List<Long> negativeMannerKeywordIds = mannerRatings.stream()
.filter(mannerRating -> !mannerRating.getIsPositive())
.flatMap(mannerRating -> mannerRating.getMannerRatingKeywordList().stream())
.map(mannerRatingKeyword -> mannerRatingKeyword.getMannerKeyword().getId())
.collect(Collectors.toList());

int negativeCount = negativeMannerKeywordIds.size();

totalCount = positiveCount + (negativeCount*-2);
}

return totalCount;
}

// 매너레벨 결정
public int mannerLevel(int mannerCount){
Copy link
Member

Choose a reason for hiding this comment

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

이것도 private으로 하는게 좋을 것 같아요!

if (mannerCount < 10) {
return 1;
} else if (mannerCount < 20) {
return 2;
} else if (mannerCount < 30) {
return 3;
} else if (mannerCount < 40) {
return 4;
} else {
return 5;
}
}

// 매너평가 조회
Expand Down Expand Up @@ -321,4 +430,76 @@ public MannerResponse.badMannerKeywordResponseDTO getBadMannerKeyword(Long membe
.mannerRatingKeywordList(badMannerKeywordIds)
.build();
}

// 내가 받은 매너 평가 조회
@Transactional(readOnly = true)
public MannerResponse.myMannerResponseDTO getMyManner(Long memberId){

Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));

// 매너평가 ID 조회
List<MannerRating> mannerRatings = mannerRatingRepository.findByToMemberId(member.getId());
Copy link
Member

Choose a reason for hiding this comment

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

여기도 member.getMannerRatingList()로 가능합니다!


// 매너키워드 조회
List<MannerRating> positiveMannerRatings = mannerRatings.stream()
.filter(MannerRating::getIsPositive)
.collect(Collectors.toList());

// 각 매너키워드(1~6) 별 count 집계
List<Long> mannerKeywordIds = new ArrayList<>();

for (MannerRating positiveRating : positiveMannerRatings) {
List<MannerRatingKeyword> mannerRatingKeywords = positiveRating.getMannerRatingKeywordList();
for (MannerRatingKeyword mannerRatingKeyword : mannerRatingKeywords) {
mannerKeywordIds.add(mannerRatingKeyword.getMannerKeyword().getId());
}
}

Map<Integer, Integer> mannerKeywordCountMap = new HashMap<>();
for (long i = 1; i <= 6; i++) {
mannerKeywordCountMap.put((int) i, 0); // 초기화
}
for (Long keywordId : mannerKeywordIds) {
mannerKeywordCountMap.put(keywordId.intValue(), mannerKeywordCountMap.getOrDefault(keywordId.intValue(), 0) + 1);
}

// 비매너키워드 조회
List<MannerRating> negativeMannerRatings = mannerRatings.stream()
.filter(mannerRating -> !mannerRating.getIsPositive())
.collect(Collectors.toList());

// 각 비매너키워드(7~12) 별 count 집계
List<Long> badMannerKeywordIds = new ArrayList<>();

for (MannerRating negativeRating : negativeMannerRatings) {
List<MannerRatingKeyword> badMannerRatingKeywords = negativeRating.getMannerRatingKeywordList();
for (MannerRatingKeyword badMannerRatingKeyword : badMannerRatingKeywords) {
badMannerKeywordIds.add(badMannerRatingKeyword.getMannerKeyword().getId());
}
}

Map<Integer, Integer> badMannerKeywordCountMap = new HashMap<>();
for (long i = 7; i <= 12; i++) {
badMannerKeywordCountMap.put((int) i, 0); // 초기화
}
for (Long keywordId : badMannerKeywordIds) {
badMannerKeywordCountMap.put(keywordId.intValue(), badMannerKeywordCountMap.getOrDefault(keywordId.intValue(), 0) + 1);
}

// 매너 키워드 DTO 생성
List<MannerResponse.mannerKeywordDTO> mannerKeywordDTOs = new ArrayList<>();
for (int i = 1; i <= 6; i++) {
int count = mannerKeywordCountMap.getOrDefault(i, 0);
mannerKeywordDTOs.add(new MannerResponse.mannerKeywordDTO(true, i, count));
}

// 비매너 키워드 DTO 생성
for (int i = 7; i <= 12; i++) {
int count = badMannerKeywordCountMap.getOrDefault(i, 0);
mannerKeywordDTOs.add(new MannerResponse.mannerKeywordDTO(false, i, count));
}

Integer mannerLevel = member.getMannerLevel();
return new MannerResponse.myMannerResponseDTO(mannerLevel, mannerKeywordDTOs);
Copy link
Member

Choose a reason for hiding this comment

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

여기서 builder로 DTO 생성하는 건 어떠신가요?!

}
}