Skip to content

Commit

Permalink
refac: refactor service code
Browse files Browse the repository at this point in the history
  • Loading branch information
pingoo33 committed Nov 19, 2023
1 parent 42ffaff commit e2494c9
Show file tree
Hide file tree
Showing 18 changed files with 278 additions and 356 deletions.
13 changes: 10 additions & 3 deletions auth/src/main/java/com/claon/auth/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,26 @@ public DuplicatedCheckResponseDto nicknameDuplicatedCheck(String nickname) {
public JwtDto signIn(
SignInRequestDto signInRequestDto
) {
User user = this.userRepository.findByEmail(signInRequestDto.email())
return this.userRepository.findByEmail(signInRequestDto.email())
.map(user -> this.jwtUtil.createToken(user.getId()))
.orElseThrow(() -> new UnauthorizedException(
ErrorCode.USER_DOES_NOT_EXIST,
"이용자를 찾을 수 없습니다."
));

return this.jwtUtil.createToken(user.getId());
}

@Transactional
public JwtDto signUp(
SignUpRequestDto signUpRequestDto
) {
userRepository.findByNickname(signUpRequestDto.nickname())
.ifPresent(user -> {
throw new UnauthorizedException(
ErrorCode.ROW_ALREADY_EXIST,
"이미 존재하는 닉네임입니다."
);
});

User user = User.signUp(
signUpRequestDto.email(),
signUpRequestDto.nickname(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ public CenterBookmarkResponseDto create(
RequestUserInfo userInfo,
String centerId
) {
Center center = this.centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);
Center center = findCenterById(centerId);

this.centerBookmarkRepository.findByUserIdAndCenterId(userInfo.id(), center.getId()).ifPresent(
bookmarkCenter -> {
Expand All @@ -41,12 +36,7 @@ public CenterBookmarkResponseDto create(
);

return CenterBookmarkResponseDto.from(
this.centerBookmarkRepository.save(
CenterBookmark.of(
center,
userInfo.id()
)
),
this.centerBookmarkRepository.save(CenterBookmark.of(center, userInfo.id())),
true
);
}
Expand All @@ -56,12 +46,7 @@ public CenterBookmarkResponseDto delete(
RequestUserInfo userInfo,
String centerId
) {
Center center = this.centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);
Center center = findCenterById(centerId);

CenterBookmark bookmarkCenter = this.centerBookmarkRepository.findByUserIdAndCenterId(userInfo.id(), center.getId()).orElseThrow(
() -> new BadRequestException(
Expand All @@ -73,11 +58,17 @@ public CenterBookmarkResponseDto delete(
this.centerBookmarkRepository.delete(bookmarkCenter);

return CenterBookmarkResponseDto.from(
CenterBookmark.of(
center,
userInfo.id()
),
CenterBookmark.of(center, userInfo.id()),
false
);
}

private Center findCenterById(String centerId) {
return this.centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ public ReviewResponseDto createReview(
String centerId,
ReviewRequestDto reviewRequestDto
) {
Center center = centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);
Center center = findCenterById(centerId);

this.reviewRepository.findByUserIdAndCenterId(userInfo.id(), center.getId()).ifPresent(
review -> {
Expand Down Expand Up @@ -66,12 +61,7 @@ public ReviewResponseDto updateReview(
String reviewId,
ReviewRequestDto requestDto
) {
CenterReview review = reviewRepository.findById(reviewId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"리뷰를 찾을 수 없습니다."
)
);
CenterReview review = findReviewById(reviewId);

IdEqualValidator.of(review.getWriterId(), userInfo.id()).validate();

Expand All @@ -85,12 +75,7 @@ public ReviewResponseDto deleteReview(
RequestUserInfo userInfo,
String reviewId
) {
CenterReview review = reviewRepository.findById(reviewId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"리뷰를 찾을 수 없습니다."
)
);
CenterReview review = findReviewById(reviewId);

IdEqualValidator.of(review.getWriterId(), userInfo.id()).validate();

Expand All @@ -105,12 +90,7 @@ public CenterReviewResponseDto findReview(
String centerId,
Pageable pageable
) {
Center center = centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);
Center center = findCenterById(centerId);

return CenterReviewResponseDto.from(
center.getId(),
Expand All @@ -124,4 +104,22 @@ public CenterReviewResponseDto findReview(
)
);
}

private Center findCenterById(String centerId) {
return this.centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);
}

private CenterReview findReviewById(String reviewId) {
return this.reviewRepository.findById(reviewId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"리뷰를 찾을 수 없습니다."
)
);
}
}
50 changes: 19 additions & 31 deletions center/src/main/java/com/claon/center/service/CenterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ public CenterResponseDto create(

@Transactional(readOnly = true)
public CenterDetailResponseDto findCenter(RequestUserInfo userInfo, String centerId) {
Center center = centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);
Center center = findCenterById(centerId);

Boolean isBookmarked = centerBookmarkRepository.findByUserIdAndCenterId(userInfo.id(), centerId).isPresent();
Long postCount = postClient.countPostsByCenterId(userInfo.id(), centerId);
Expand All @@ -105,15 +100,8 @@ public CenterDetailResponseDto findCenter(RequestUserInfo userInfo, String cente
}

@Transactional(readOnly = true)
public List<HoldInfoResponseDto> findHoldInfoByCenterId(
String centerId
) {
Center center = centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);
public List<HoldInfoResponseDto> findHoldInfoByCenterId(String centerId) {
Center center = findCenterById(centerId);

return holdInfoRepository.findAllByCenter(center)
.stream()
Expand Down Expand Up @@ -146,12 +134,7 @@ public CenterReportResponseDto createReport(
String centerId,
CenterReportRequestDto centerReportRequestDto
) {
Center center = centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);
Center center = findCenterById(centerId);

return CenterReportResponseDto.from(
this.centerReportRepository.save(
Expand Down Expand Up @@ -182,20 +165,25 @@ public Pagination<PostThumbnailResponse> getCenterPosts(
Optional<String> holdId,
Pageable pageable
) {
Center center = centerRepository.findById(centerId).orElseThrow(
Center center = findCenterById(centerId);

holdId.ifPresent(s -> holdInfoRepository.findByIdAndCenter(s, center)
.orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"홀드를 찾을 수 없습니다."
)
));

return this.postClient.findPostThumbnails(userInfo.id(), centerId, holdId, pageable);
}

private Center findCenterById(String centerId) {
return this.centerRepository.findById(centerId).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"암장을 찾을 수 없습니다."
)
);

holdId.ifPresent(s -> holdInfoRepository.findByIdAndCenter(s, center).orElseThrow(
() -> new NotFoundException(
ErrorCode.DATA_DOES_NOT_EXIST,
"홀드를 찾을 수 없습니다."
)
));

return this.postClient.findPostThumbnails(userInfo.id(), centerId, holdId, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
Expand All @@ -23,24 +22,19 @@ public class ClimbingHistoryService {
public List<UserPostInfoResponseDto> findClimbingHistory(String userId) {
List<String> postIds = this.postRepository.selectPostIdsByUserId(userId);

List<ClimbingHistory> climbingHistories = climbingHistoryRepository.findByPostIds(postIds);

Map<String, Map<String, Integer>> historyMap = climbingHistories.stream().collect(
Collectors.groupingBy(history -> history.getPost().getCenterId(),
return climbingHistoryRepository.findByPostIds(postIds)
.stream().collect(Collectors.groupingBy(history -> history.getPost().getCenterId(),
Collectors.toMap(
ClimbingHistory::getHoldInfoId,
ClimbingHistory::getClimbingCount,
Integer::sum
)
));

return historyMap.entrySet()
.stream()
)))
.entrySet().stream()
.map(entry -> UserPostInfoResponseDto.from(
entry.getKey(),
postIds.size(),
entry.getValue().entrySet()
.stream()
entry.getValue()
.entrySet().stream()
.map(en -> ClimbingHistoryResponseDto.from(en.getKey(), en.getValue()))
.collect(Collectors.toList())))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ public NoticeResponseDto createNotice(
)
);
}

}
Loading

0 comments on commit e2494c9

Please sign in to comment.