Skip to content

Commit

Permalink
Merge pull request #66 from AdultOfNineteen/refactor/issue-65
Browse files Browse the repository at this point in the history
μ™€μž‡ 뱃지 계산 둜직 μˆ˜μ •
  • Loading branch information
imenuuu authored Jan 11, 2024
2 parents 66fa6d3 + 5d230f5 commit 52fe028
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
@Component
public class MessageConverter {

public NotificationRequestDto toNotificationRequestDto(Badge badge, UserFcmToken fcmToken, User user) {
public NotificationRequestDto toNotificationRequestDto(String badge, UserFcmToken fcmToken, User user) {
return NotificationRequestDto.builder()
.title("μƒˆλ‘œμš΄ 뱃지λ₯Ό μ–»μ—ˆμ–΄μš”~~~")
.body(toMessageBodyWineBadge(user.getNickName(), badge.getBadgeName()))
.body(toMessageBodyWineBadge(user.getNickName(), badge))
.token(fcmToken.getFcmToken())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,19 @@

import com.example.wineyapi.wineBadge.dto.WineBadgeResponse;
import com.example.wineydomain.badge.dto.WineBadgeUserDTO;
import com.example.wineydomain.badge.entity.Badge;
import com.example.wineydomain.badge.entity.BadgeType;
import com.example.wineydomain.badge.entity.UserWineBadge;
import com.example.wineydomain.badge.entity.WineBadge;
import com.example.wineydomain.user.entity.User;
import org.springframework.stereotype.Component;

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

@Component
public class WineBadgeConvertor {

public static WineBadgeResponse.BadgeDTO toBadgeDTO(UserWineBadge userWineBadge) {
return WineBadgeResponse.BadgeDTO.builder()
.badgeId(userWineBadge.getId())
.badgeType(userWineBadge.getBadge().getType())
.name(userWineBadge.getBadge().getBadgeName())
.description(userWineBadge.getBadge().getBadgeDescription())
.acquiredAt(userWineBadge.getCreatedAt())
.isRead(userWineBadge.getIsRead())
.build();
}

public static WineBadgeResponse.BadgeDTO toBadgeDTO(WineBadgeUserDTO userWineBadge) {
return WineBadgeResponse.BadgeDTO.builder()
.badgeId(userWineBadge.getWineBadgeId())
Expand Down Expand Up @@ -54,11 +44,11 @@ public static WineBadgeResponse.WineBadgeListDTO toWineBadgeListDTO(List<WineBad
.build();
}

public UserWineBadge WineBadge(Badge badge, User user) {
public UserWineBadge convertWineBadge(WineBadge wineBadge, User user) {
return UserWineBadge
.builder()
.badge(badge)
.wineBadge(wineBadge)
.user(user)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import com.example.wineyapi.common.message.MessageConverter;
import com.example.wineyapi.common.util.TimeUtils;
import com.example.wineyapi.notification.service.NotificationService;
import com.example.wineyapi.notification.service.NotificationServiceImpl;
import com.example.wineyapi.user.converter.UserConnectionConverter;
import com.example.wineyapi.wineBadge.convertor.WineBadgeConvertor;
import com.example.wineycommon.annotation.RedissonLock;
import com.example.wineycommon.exception.NotFoundException;
import com.example.wineycommon.exception.errorcode.CommonResponseStatus;
import com.example.wineydomain.badge.dto.WineBadgeUserDTO;
import com.example.wineydomain.badge.entity.Badge;
import com.example.wineydomain.badge.entity.UserWineBadge;
import com.example.wineydomain.badge.entity.WineBadge;
import com.example.wineydomain.badge.exception.WineBadgeErrorCode;
Expand All @@ -28,14 +25,16 @@
import com.example.wineyinfrastructure.firebase.service.MessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import static com.example.wineydomain.badge.entity.Badge.*;
import static com.example.wineycommon.constants.WineyStatic.*;

@Service
@RequiredArgsConstructor
Expand All @@ -57,35 +56,43 @@ public class WineBadgeServiceImpl implements WineBadgeService {
@RedissonLock(LockName = "뱃지-계산", key = "#userId")
@Async("badge")
public void calculateBadge(User user, Long userId) {
List<WineBadge> wineBadges = getWineBadgeAll();
List<TastingNote> tastingNotes = tastingNoteRepository.findByUser(user);

List<Badge> badges = userWineBadgeRepository.findByUser(user).stream()
.map(UserWineBadge::getBadge)
.collect(Collectors.toList());

log.info("Badge Lists : " + badges);
List<WineBadge> userWineBadgeLists = userWineBadgeRepository.findByUser(user).stream()
.map(UserWineBadge::getWineBadge)
.collect(Collectors.toList());

List<UserWineBadge> userWineBadges = new ArrayList<>();

userWineBadges.addAll(checkSommelierBadge(badges, tastingNotes, user));
userWineBadges.addAll(checkSommelierBadge(tastingNotes, user, userWineBadgeLists, wineBadges));

userWineBadges.addAll(calculateWineBadgeAboutTastingNote(badges, tastingNotes, user));
userWineBadges.addAll(calculateWineBadgeAboutTastingNote(tastingNotes, user, userWineBadgeLists, wineBadges));

userWineBadgeRepository.saveAll(userWineBadges);

sendMessageGetWineBadges(userWineBadges, user);
}

@Cacheable(value = "wineBadgeAll", key = "'all'", cacheManager = "redisCacheManager")
public List<WineBadge> getWineBadgeAll() {
return wineBadgeRepository.findByOrderByIdAsc();
}

public void sendMessageGetWineBadges(List<UserWineBadge> userWineBadges, User user) {
for(UserWineBadge wineBadge : userWineBadges) {
sendMessageGetWineBadge(wineBadge, user);
}
}

private WineBadge getWineBadge(int index, List<WineBadge> wineBadges){
return wineBadges.get(index);
}

public void sendMessageGetWineBadge(UserWineBadge wineBadge, User user) {
List<NotificationRequestDto> notificationRequestDtos = new ArrayList<>();
for(UserFcmToken fcmToken : user.getUserFcmTokens()) {
notificationRequestDtos.add(messageConverter.toNotificationRequestDto(wineBadge.getBadge(), fcmToken, user));
notificationRequestDtos.add(messageConverter.toNotificationRequestDto(wineBadge.getWineBadge().getName(), fcmToken, user));
}
if(!notificationRequestDtos.isEmpty()) {
messageService.sendNotifications(notificationRequestDtos);
Expand All @@ -99,7 +106,7 @@ public void provideFirstAnalysis(User user) {
user.setTastingNoteAnalyzed(true);
userRepository.save(user);

UserWineBadge userWineBadge = userWineBadgeRepository.save(wineBadgeConvertor.WineBadge(TASTE_DISCOVERY, user));
UserWineBadge userWineBadge = userWineBadgeRepository.save(wineBadgeConvertor.convertWineBadge(wineBadgeRepository.findByBadge(TASTE_DISCOVERY), user));

sendMessageGetWineBadge(userWineBadge, user);
}
Expand Down Expand Up @@ -133,47 +140,53 @@ private void checkContinueConnection(User user, UserConnection userConnection) {
}

private void checkUserConnectionBadge(UserConnection userConnection, User user) {
List<Badge> badges = userWineBadgeRepository.findByUser(user).stream()
.map(UserWineBadge::getBadge)
.collect(Collectors.toList());
List<WineBadge> userWineBadgeLists = userWineBadgeRepository.findByUser(user).stream()
.map(UserWineBadge::getWineBadge)
.collect(Collectors.toList());

if(userConnection.getCnt() == 7){
if(!badges.contains(WINE_EXCITEMENT)) userWineBadgeRepository.save(wineBadgeConvertor.WineBadge(WINE_EXCITEMENT, user));
WineBadge wineExcitementBadge = getWineBadge(WINE_EXCITEMENT_INDEX, userWineBadgeLists);
if(userConnection.getCnt() == wineExcitementBadge.getRequiredActivity()){
if(!userWineBadgeLists.contains(wineExcitementBadge)) userWineBadgeRepository.save(wineBadgeConvertor.convertWineBadge(wineExcitementBadge, user));
}
if(userConnection.getCnt() == 30){
if(!badges.contains(WINE_ADDICT)) userWineBadgeRepository.save(wineBadgeConvertor.WineBadge(WINE_ADDICT, user));
WineBadge wineAddictBadge = getWineBadge(WINE_ADDICT_INDEX, userWineBadgeLists);
if(userConnection.getCnt() == wineExcitementBadge.getRequiredActivity()){
if(!userWineBadgeLists.contains(wineAddictBadge)) userWineBadgeRepository.save(wineBadgeConvertor.convertWineBadge(wineAddictBadge, user));
}
}



public List<UserWineBadge> checkSommelierBadge(List<Badge> badges, List<TastingNote> tastingNotes, User user) {
public List<UserWineBadge> checkSommelierBadge(List<TastingNote> tastingNotes, User user, List<WineBadge> userWineBadgeLists,
List<WineBadge> wineBadges) {
List<UserWineBadge> userWineBadges = new ArrayList<>();
int cnt = tastingNotes.size();

if (cnt >= YOUNG_SOUMELIER.getRequiredActivity()) {
if (!badges.contains(YOUNG_SOUMELIER))
userWineBadges.add(wineBadgeConvertor.WineBadge(YOUNG_SOUMELIER, user));
WineBadge youngSomelierBadge = getWineBadge(YOUNG_SOMMELIER_INDEX, wineBadges);
if (cnt >= youngSomelierBadge.getRequiredActivity()) {
if (!userWineBadgeLists.contains(youngSomelierBadge))
userWineBadges.add(wineBadgeConvertor.convertWineBadge(youngSomelierBadge, user));
}
if (cnt >= ENJOYMENT.getRequiredActivity()) {
if (!badges.contains(ENJOYMENT)) userWineBadges.add(wineBadgeConvertor.WineBadge(ENJOYMENT, user));
WineBadge enjoymentBadge = getWineBadge(ENJOYMENT_INDEX, wineBadges);
if (cnt >= enjoymentBadge.getRequiredActivity()) {
if (!userWineBadgeLists.contains(enjoymentBadge)) userWineBadges.add(wineBadgeConvertor.convertWineBadge(enjoymentBadge, user));
}
if (cnt >= INTERMEDIATE_SOUMELIER.getRequiredActivity()) {
if (!badges.contains(INTERMEDIATE_SOUMELIER))
userWineBadges.add(wineBadgeConvertor.WineBadge(INTERMEDIATE_SOUMELIER, user));
WineBadge intermediateSomelierBadge = getWineBadge(INTERMEDIATE_SOMMELIER_INDEX, wineBadges);
if (cnt >= intermediateSomelierBadge.getRequiredActivity()) {
if (!userWineBadgeLists.contains(intermediateSomelierBadge)) userWineBadges.add(wineBadgeConvertor.convertWineBadge(intermediateSomelierBadge, user));
}
if (cnt >= ADVANCED_SOUMELIER.getRequiredActivity()) {
if (!badges.contains(ADVANCED_SOUMELIER))
userWineBadges.add(wineBadgeConvertor.WineBadge(ADVANCED_SOUMELIER, user));
WineBadge advancedSomelierBadge = getWineBadge(ADVANCED_SOMMELIER_INDEX, wineBadges);
if (cnt >= advancedSomelierBadge.getRequiredActivity()) {
if (!userWineBadgeLists.contains(advancedSomelierBadge)) userWineBadges.add(wineBadgeConvertor.convertWineBadge(advancedSomelierBadge, user));
}
if (cnt >= MASTER_SOUMELIER.getRequiredActivity()) {
if (!badges.contains(MASTER_SOUMELIER))
userWineBadges.add(wineBadgeConvertor.WineBadge(MASTER_SOUMELIER, user));
WineBadge masterSomelierBadge = getWineBadge(MASTER_SOMMELIER_INDEX, wineBadges);
if (cnt >= masterSomelierBadge.getRequiredActivity()) {
if (!userWineBadgeLists.contains(masterSomelierBadge)) userWineBadges.add(wineBadgeConvertor.convertWineBadge(masterSomelierBadge, user));
}
return userWineBadges;
}

public List<UserWineBadge> calculateWineBadgeAboutTastingNote(List<Badge> badges, List<TastingNote> tastingNotes, User user) {
public List<UserWineBadge> calculateWineBadgeAboutTastingNote(List<TastingNote> tastingNotes, User user,
List<WineBadge> userWineBadgeLists, List<WineBadge> wineBadges) {
int totalSmellKeywordCnt = 0;
int roesTastingNotes = 0;
int portTastingNotes = 0;
Expand Down Expand Up @@ -214,22 +227,28 @@ public List<UserWineBadge> calculateWineBadgeAboutTastingNote(List<Badge> badges
int maxWineCount = calculateMaxWine(sameWineMap);
int maxVarietalCount = calculateMaxVarietalCount(sameVarietalMap);

return checkWineBadgeAboutTastingNote(badges, user, totalSmellKeywordCnt, roesTastingNotes, portTastingNotes, otherWineTastingNotes, maxWineCount, maxVarietalCount);
return checkWineBadgeAboutTastingNote(user, totalSmellKeywordCnt, roesTastingNotes, portTastingNotes,
otherWineTastingNotes, maxWineCount, maxVarietalCount, userWineBadgeLists, wineBadges);
}

private List<UserWineBadge> checkWineBadgeAboutTastingNote(List<Badge> badges, User user, int totalSmellKeywordCnt, int roesTastingNotes, int portTastingNotes, int otherWineTastingNotes, int maxWineCount, int maxVarietalCount) {
private List<UserWineBadge> checkWineBadgeAboutTastingNote(User user, int totalSmellKeywordCnt, int roesTastingNotes, int portTastingNotes, int otherWineTastingNotes, int maxWineCount, int maxVarietalCount,
List<WineBadge> userWineBadgeLists, List<WineBadge> wineBadges) {
List<UserWineBadge> userWineBadges = new ArrayList<>();
if (totalSmellKeywordCnt >= SMELL_ENTHUSIAST.getRequiredActivity()) {
if (!badges.contains(SMELL_ENTHUSIAST)) userWineBadges.add(wineBadgeConvertor.WineBadge(SMELL_ENTHUSIAST, user));
WineBadge smellEnthusiastBadge = getWineBadge(SMELL_ENTHUSIAST_INDEX, wineBadges);
if (totalSmellKeywordCnt >= smellEnthusiastBadge.getRequiredActivity()) {
if (!userWineBadgeLists.contains(smellEnthusiastBadge)) userWineBadges.add(wineBadgeConvertor.convertWineBadge(smellEnthusiastBadge, user));
}
if (maxWineCount >= FAVORITE_WINE.getRequiredActivity()) {
if (!badges.contains(FAVORITE_WINE)) userWineBadges.add(wineBadgeConvertor.WineBadge(FAVORITE_WINE, user));
WineBadge favoriteWineBadge = getWineBadge(FAVORITE_WINE_INDEX, wineBadges);
if (maxWineCount >= favoriteWineBadge.getRequiredActivity()) {
if (!userWineBadgeLists.contains(favoriteWineBadge)) userWineBadges.add(wineBadgeConvertor.convertWineBadge(favoriteWineBadge, user));
}
if (maxVarietalCount >= GRAPE_LOVER.getRequiredActivity()) {
if (!badges.contains(GRAPE_LOVER)) userWineBadges.add(wineBadgeConvertor.WineBadge(GRAPE_LOVER, user));
WineBadge grapeLoverBadge = getWineBadge(GRAPE_LOVER_INDEX, wineBadges);
if (maxVarietalCount >= grapeLoverBadge.getRequiredActivity()) {
if (!userWineBadgeLists.contains(grapeLoverBadge)) userWineBadges.add(wineBadgeConvertor.convertWineBadge(grapeLoverBadge, user));
}
WineBadge nonAlcoholicBadge = getWineBadge(NON_ALCOHOLIC_INDEX, wineBadges);
if (roesTastingNotes >= 1 && portTastingNotes >= 1 && otherWineTastingNotes >= 1) {
if (!badges.contains(NON_ALCOHOLIC)) userWineBadges.add(wineBadgeConvertor.WineBadge(NON_ALCOHOLIC, user));
if (!userWineBadgeLists.contains(nonAlcoholicBadge)) userWineBadges.add(wineBadgeConvertor.convertWineBadge(nonAlcoholicBadge, user));
}
return userWineBadges;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectio

Map<String, RedisCacheConfiguration> cacheConfiguration = new HashMap<>();
cacheConfiguration.put("recommendWine", redisCacheConfiguration.entryTtl(Duration.ofMinutes(5L)));

cacheConfiguration.put("wineBadgeAll", redisCacheConfiguration.entryTtl(Duration.ofMinutes(60L)));

return RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
package com.example.wineycommon.constants;

import org.springframework.data.redis.connection.RedisServer;

public class WineyStatic {
public static final String BEARER = "Bearer ";

public static final String AUTHORIZATION_HEADER = "X-AUTH-TOKEN";

public static final String REFRESH_TOKEN_HEADER = "X-REFRESH-TOKEN";

public static final Integer YOUNG_SOMMELIER_INDEX = 0;

public static final Integer INTERMEDIATE_SOMMELIER_INDEX = 1;

public static final Integer ADVANCED_SOMMELIER_INDEX = 2;

public static final Integer MASTER_SOMMELIER_INDEX = 3;

public static final Integer SMELL_ENTHUSIAST_INDEX = 4;

public static final Integer FAVORITE_WINE_INDEX = 5;

public static final Integer GRAPE_LOVER_INDEX = 6;

public static final Integer ENJOYMENT_INDEX = 7;

public static final Integer WINE_EXCITEMENT_INDEX = 8;

public static final Integer WINE_ADDICT_INDEX = 9;

public static final String TASTE_DISCOVERY = "TASTE_DISCOVERY";

public static final Integer NON_ALCOHOLIC_INDEX = 11;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class QUserWineBadge extends EntityPathBase<UserWineBadge> {

public final com.example.wineydomain.common.model.QBaseEntity _super = new com.example.wineydomain.common.model.QBaseEntity(this);

public final EnumPath<Badge> badge = createEnum("badge", Badge.class);

//inherited
public final DateTimePath<java.time.LocalDateTime> createdAt = _super.createdAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
@DynamicUpdate
@DynamicInsert
public class UserWineBadge extends BaseEntity {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -35,10 +34,6 @@ public class UserWineBadge extends BaseEntity {
@JoinColumn(name = "wineBadgeId", nullable = false)
private WineBadge wineBadge;

// TODO : 이 ν•„λ“œλ₯Ό μ§€μ›Œμ€˜μ•Όν•©λ‹ˆλ‹€.
@Enumerated(EnumType.STRING)
private Badge badge;

@Builder.Default
private Boolean isRead = Boolean.FALSE;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.example.wineydomain.badge.repository;

import java.util.List;

import com.example.wineydomain.badge.entity.WineBadge;
import org.springframework.data.jpa.repository.JpaRepository;

public interface WineBadgeRepository extends JpaRepository<WineBadge, Long>, WineBadgeCustomRepository {
List<WineBadge> findByOrderByIdAsc();

WineBadge findByBadge(String tasteDiscovery);
}

0 comments on commit 52fe028

Please sign in to comment.