-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [FEAT] 응모 정보 응답 객체 필드 추가 * [FEAT] 캐싱을 적용한 경품 확률 조회메서드 추가 * [FIX] 경품 정보 반환 메서드 수정 * [FEAT] 경품 정보 에러코드 추가 * [FIX] 경품 정보 컨트롤러 수정
- Loading branch information
1 parent
40c2709
commit 9d1188a
Showing
6 changed files
with
94 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/main/java/softeer/team_pineapple_be/domain/draw/response/DrawRewardImageResponse.java
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
src/main/java/softeer/team_pineapple_be/domain/draw/response/DrawRewardInfoResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package softeer.team_pineapple_be.domain.draw.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import softeer.team_pineapple_be.domain.draw.domain.DrawRewardInfo; | ||
import softeer.team_pineapple_be.domain.draw.service.DrawProbabilityService; | ||
|
||
/** | ||
* 경품 이미지 응답 | ||
*/ | ||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
public class DrawRewardInfoResponse { | ||
private Byte rank; | ||
private String rewardName; | ||
private Integer rewardCount; | ||
private String imageUrl; | ||
|
||
public static DrawRewardInfoResponse of(DrawRewardInfo drawRewardInfo, | ||
DrawProbabilityService drawProbabilityService) { | ||
return new DrawRewardInfoResponse(drawRewardInfo.getRanking(), drawRewardInfo.getName(), | ||
drawProbabilityService.getDrawProbabilityByRanking(drawRewardInfo.getRanking()), drawRewardInfo.getImage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 51 additions & 28 deletions
79
src/main/java/softeer/team_pineapple_be/domain/draw/service/DrawProbabilityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,74 @@ | ||
package softeer.team_pineapple_be.domain.draw.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import softeer.team_pineapple_be.domain.draw.domain.DrawProbability; | ||
import softeer.team_pineapple_be.domain.draw.repository.DrawProbabilityRepository; | ||
import softeer.team_pineapple_be.domain.draw.request.DrawProbabilityRequest; | ||
import softeer.team_pineapple_be.domain.draw.response.DrawProbabilityResponse; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import softeer.team_pineapple_be.domain.draw.domain.DrawProbability; | ||
import softeer.team_pineapple_be.domain.draw.exception.DrawErrorCode; | ||
import softeer.team_pineapple_be.domain.draw.repository.DrawProbabilityRepository; | ||
import softeer.team_pineapple_be.domain.draw.request.DrawProbabilityRequest; | ||
import softeer.team_pineapple_be.domain.draw.response.DrawProbabilityResponse; | ||
import softeer.team_pineapple_be.global.exception.RestApiException; | ||
|
||
/** | ||
* 경품 확률 서비스 | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class DrawProbabilityService { | ||
|
||
private final DrawProbabilityRepository drawProbabilityRepository; | ||
private static final ConcurrentHashMap<Byte, Integer> probabilityCache = new ConcurrentHashMap<>(); | ||
private final DrawProbabilityRepository drawProbabilityRepository; | ||
|
||
/** | ||
* 경품 확률을 조회하는 메서드 | ||
* @return 경품 확률 리스트 | ||
*/ | ||
@Transactional(readOnly = true) | ||
public DrawProbabilityResponse getDrawProbability(){ | ||
List<DrawProbability> probabilities = drawProbabilityRepository.findAll(); | ||
Map<Byte, Integer> probabilitiesMap = probabilities.stream() | ||
.collect(Collectors.toMap(DrawProbability::getRanking, DrawProbability::getProbability)); | ||
/** | ||
* 경품 확률을 조회하는 메서드 | ||
* | ||
* @return 경품 확률 리스트 | ||
*/ | ||
@Transactional(readOnly = true) | ||
public DrawProbabilityResponse getDrawProbability() { | ||
List<DrawProbability> probabilities = drawProbabilityRepository.findAll(); | ||
Map<Byte, Integer> probabilitiesMap = | ||
probabilities.stream().collect(Collectors.toMap(DrawProbability::getRanking, DrawProbability::getProbability)); | ||
|
||
return new DrawProbabilityResponse(probabilitiesMap); | ||
return new DrawProbabilityResponse(probabilitiesMap); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public Integer getDrawProbabilityByRanking(Byte ranking) { | ||
if (probabilityCache.containsKey(ranking)) { | ||
return probabilityCache.get(ranking); | ||
} | ||
|
||
/** | ||
* 경품 확률을 수정하는 메서드 | ||
* @param request 수정하고자 하는 경품 확률 | ||
*/ | ||
@Transactional | ||
public void setDrawProbability(DrawProbabilityRequest request) { | ||
Map<Byte, Integer> probabilities = request.getProbabilities(); | ||
Integer probability = drawProbabilityRepository.findById(ranking) | ||
.orElseThrow( | ||
() -> new RestApiException(DrawErrorCode.NO_PRIZE_PROBABILITY)) | ||
.getProbability(); | ||
probabilityCache.put(ranking, probability); | ||
return probability; | ||
} | ||
|
||
List<DrawProbability> drawProbabilities = probabilities.entrySet().stream() | ||
.map(entry -> new DrawProbability(entry.getKey(), entry.getValue())) | ||
.collect(Collectors.toList()); | ||
/** | ||
* 경품 확률을 수정하는 메서드 | ||
* | ||
* @param request 수정하고자 하는 경품 확률 | ||
*/ | ||
@Transactional | ||
public void setDrawProbability(DrawProbabilityRequest request) { | ||
Map<Byte, Integer> probabilities = request.getProbabilities(); | ||
|
||
drawProbabilityRepository.saveAll(drawProbabilities); | ||
} | ||
List<DrawProbability> drawProbabilities = probabilities.entrySet() | ||
.stream() | ||
.map(entry -> new DrawProbability(entry.getKey(), | ||
entry.getValue())) | ||
.collect(Collectors.toList()); | ||
|
||
drawProbabilityRepository.saveAll(drawProbabilities); | ||
} | ||
} |