Skip to content

Commit

Permalink
fix: 유저의 활동뱃지가 중복으로 획득되는 에러 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
punchdrunkard committed Oct 7, 2024
1 parent 1ac8a97 commit e08f2b0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.odiga.fiesta.user.dto.response.UserBadgeResponse;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;

Expand Down Expand Up @@ -39,7 +40,12 @@ public List<UserBadgeResponse> findUserBadges(Long userId) {
)
.from(badge)
.leftJoin(userBadge)
.on(badge.id.eq(userBadge.badgeId)) // 단순 조인
.on(badgeIdEq())
.distinct()
.fetch();
}

private static BooleanExpression badgeIdEq() {
return badge.id.eq(userBadge.badgeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface UserBadgeRepository extends JpaRepository<UserBadge, Long> {

@Query("SELECT ub.badgeId FROM UserBadge ub WHERE ub.userId= :userId AND ub.badgeId IN :badgeIds")
List<Long> findBadgeIdByUserIdAndBadgeIdIn(@Param("userId") Long userId, @Param("badgeIds") List<Long> badgeIds);

Boolean existsByBadgeId(Long badgeId);
}
17 changes: 10 additions & 7 deletions src/main/java/com/odiga/fiesta/badge/service/BadgeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.odiga.fiesta.category.domain.CategoryConstants.*;
import static com.odiga.fiesta.common.error.ErrorCode.*;
import static java.util.Objects.*;
import static java.util.concurrent.CompletableFuture.*;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -47,25 +48,26 @@ public class BadgeService {
// 현재 유저의 뱃지 조회
public List<UserBadgeResponse> getUserBadges(User user) {
validateUser(user);
return badgeRepository.findUserBadges(user.getId());
List<UserBadgeResponse> userBadges = badgeRepository.findUserBadges(user.getId());
return userBadges;
}

@Async
@Transactional
public CompletableFuture<List<Long>> giveFestivalBadge(Long userId) {
return CompletableFuture.completedFuture(giveBadge(userId, BadgeType.FESTIVAL));
return completedFuture(giveBadge(userId, BadgeType.FESTIVAL));
}

@Async
@Transactional
public CompletableFuture<List<Long>> giveReviewBadge(Long userId) {
return CompletableFuture.completedFuture(giveBadge(userId, BadgeType.REVIEW));
return completedFuture(giveBadge(userId, BadgeType.REVIEW));
}

@Async
@Transactional
public CompletableFuture<List<Long>> giveUserBadge(Long userId) {
return CompletableFuture.completedFuture(giveBadge(userId, BadgeType.USER));
return completedFuture(giveBadge(userId, BadgeType.USER));
}

private List<Long> giveBadge(Long userId, BadgeType badgeType) {
Expand All @@ -86,7 +88,8 @@ private List<Long> giveBadge(Long userId, BadgeType badgeType) {

// 조건을 확인하고 뱃지를 수여
for (Long badgeId : badgeIds) {
if (isUserNotOwnedBadge(badgeId, userBadgeMap) && isBadgeCondition(userId, badgeId)) {
if (isUserNotOwnedBadge(badgeId) && isBadgeCondition(userId, badgeId)) {
log.info("뱃지 획득 완료");
userBadgeRepository.save(UserBadge.builder().badgeId(badgeId).userId(userId).build());
givenBadgeIds.add(badgeId);
}
Expand Down Expand Up @@ -169,8 +172,8 @@ private boolean isBadgeCondition(long userId, long badgeId) {
return false;
}

private static boolean isUserNotOwnedBadge(Long badgeId, Map<Long, Boolean> userBadgeMap) {
return !userBadgeMap.get(badgeId);
private boolean isUserNotOwnedBadge(Long badgeId) {
return !userBadgeRepository.existsByBadgeId(badgeId);
}

private void validateUser(User user) {
Expand Down

0 comments on commit e08f2b0

Please sign in to comment.