Skip to content

Commit

Permalink
Feat: 회원가입 뱃지 부여 기능 추가 (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaewon-pro authored Oct 28, 2024
1 parent 5050c2c commit d6173c5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.dnd.runus.application.badge;

import com.dnd.runus.application.member.event.SignupEvent;
import com.dnd.runus.global.constant.BadgeType;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class BadgeEventHandler {
private final BadgeService badgeService;

@EventListener
public void handleSignupEvent(SignupEvent signupEvent) {
badgeService.achieveBadge(signupEvent.member(), BadgeType.PERSONAL_RECORD, 0);
}
}
18 changes: 15 additions & 3 deletions src/main/java/com/dnd/runus/application/badge/BadgeService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.dnd.runus.application.badge;

import com.dnd.runus.domain.badge.BadgeAchievementRepository;
import com.dnd.runus.domain.badge.BadgeRepository;
import com.dnd.runus.domain.badge.BadgeWithAchieveStatusAndAchievedAt;
import com.dnd.runus.domain.badge.*;
import com.dnd.runus.domain.member.Member;
import com.dnd.runus.global.constant.BadgeType;
import com.dnd.runus.presentation.v1.badge.dto.response.AchievedBadgesResponse;
import com.dnd.runus.presentation.v1.badge.dto.response.AllBadgesListResponse;
Expand All @@ -26,6 +25,19 @@ public class BadgeService {
private final BadgeAchievementRepository badgeAchievementRepository;
private final BadgeRepository badgeRepository;

public void achieveBadge(Member member, BadgeType badgeType, int value) {
List<Badge> badges = badgeRepository.findByTypeAndRequiredValueLessThanEqual(badgeType, value);
if (badges.isEmpty()) {
return;
}

List<BadgeAchievement> badgeAchievements = badges.stream()
.map(badge -> new BadgeAchievement(badge, member))
.toList();

badgeAchievementRepository.saveAllIgnoreDuplicated(badgeAchievements);
}

public AchievedBadgesResponse getAchievedBadges(long memberId) {
return new AchievedBadgesResponse(badgeAchievementRepository.findByMemberIdWithBadge(memberId).stream()
.map(badgeAchievement -> new AchievedBadgesResponse.AchievedBadge(
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/dnd/runus/domain/badge/BadgeRepository.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.dnd.runus.domain.badge;

import com.dnd.runus.global.constant.BadgeType;

import java.util.List;

public interface BadgeRepository {
List<Badge> findByTypeAndRequiredValueLessThanEqual(BadgeType badgeType, int requiredValue);

List<BadgeWithAchieveStatusAndAchievedAt> findAllBadgesWithAchieveStatusByMemberId(long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.dnd.runus.infrastructure.persistence.domain.badge;

import com.dnd.runus.domain.badge.Badge;
import com.dnd.runus.domain.badge.BadgeRepository;
import com.dnd.runus.domain.badge.BadgeWithAchieveStatusAndAchievedAt;
import com.dnd.runus.global.constant.BadgeType;
import com.dnd.runus.infrastructure.persistence.jooq.badge.JooqBadgeRepository;
import com.dnd.runus.infrastructure.persistence.jpa.badge.JpaBadgeRepository;
import com.dnd.runus.infrastructure.persistence.jpa.badge.entity.BadgeEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

Expand All @@ -11,8 +15,16 @@
@Repository
@RequiredArgsConstructor
public class BadgeRepositoryImpl implements BadgeRepository {
private final JpaBadgeRepository jpaBadgeRepository;
private final JooqBadgeRepository jooqBadgeRepository;

@Override
public List<Badge> findByTypeAndRequiredValueLessThanEqual(BadgeType badgeType, int requiredValue) {
return jpaBadgeRepository.findByTypeAndRequiredValueLessThanEqual(badgeType, requiredValue).stream()
.map(BadgeEntity::toDomain)
.toList();
}

@Override
public List<BadgeWithAchieveStatusAndAchievedAt> findAllBadgesWithAchieveStatusByMemberId(long memberId) {
return jooqBadgeRepository.findAllBadgesWithAchieveStatusByMemberId(memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public List<BadgeAchievement.OnlyBadge> findByMemberIdWithBadge(long memberId) {
}

public void saveAllIgnoreDuplicated(List<BadgeAchievement> badgeAchievements) {
OffsetDateTime now = OffsetDateTime.now();
dsl.batch(badgeAchievements.stream()
.map(badgeAchievement -> dsl.insertInto(BADGE_ACHIEVEMENT)
.set(
Expand All @@ -38,8 +39,12 @@ public void saveAllIgnoreDuplicated(List<BadgeAchievement> badgeAchievements) {
.set(
BADGE_ACHIEVEMENT.MEMBER_ID,
badgeAchievement.member().memberId())
.set(BADGE_ACHIEVEMENT.CREATED_AT, badgeAchievement.createdAt())
.set(BADGE_ACHIEVEMENT.UPDATED_AT, badgeAchievement.updatedAt())
.set(
BADGE_ACHIEVEMENT.CREATED_AT,
badgeAchievement.createdAt() == null ? now : badgeAchievement.createdAt())
.set(
BADGE_ACHIEVEMENT.UPDATED_AT,
badgeAchievement.updatedAt() == null ? now : badgeAchievement.updatedAt())
.onConflictDoNothing())
.toList())
.execute();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.dnd.runus.infrastructure.persistence.jpa.badge;

import com.dnd.runus.global.constant.BadgeType;
import com.dnd.runus.infrastructure.persistence.jpa.badge.entity.BadgeEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface JpaBadgeRepository extends JpaRepository<BadgeEntity, Long> {
List<BadgeEntity> findByTypeAndRequiredValueLessThanEqual(BadgeType badgeType, int requiredValue);
}

0 comments on commit d6173c5

Please sign in to comment.