Skip to content

Commit

Permalink
fix: 입덕포인트 서비스 로직 수정 및 코드 리팩토링 #145
Browse files Browse the repository at this point in the history
memberId, attractionPointId, UpdateAttractionPoint -> memberId, AttractionPointReq 변경,
애니별 총 입덕포인트 조회 메소드 변경, 평균 계산식에 *100추가
  • Loading branch information
hanyMK committed Dec 11, 2023
1 parent d2c4dc7 commit 72af637
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface AttractionPointService {
//입덕포인트 조회(true/false)
IsAttractionPoint isAttractionPoint(Long memberId, Long animeId);

boolean update(Long memberId, Long attractionPointId, UpdateAttractionPoint req);
boolean update(Long memberId, AttractionPointReq req);

AttractionPointStats getAttractionPointStats(Long anime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@
import io.oduck.api.domain.attractionPoint.entity.AttractionElement;
import io.oduck.api.domain.attractionPoint.entity.AttractionPoint;
import io.oduck.api.domain.attractionPoint.repository.AttractionPointRepository;

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

import io.oduck.api.domain.member.entity.Member;
import io.oduck.api.domain.member.repository.MemberRepository;
import io.oduck.api.domain.review.entity.ShortReview;
import io.oduck.api.global.exception.BadRequestException;
import io.oduck.api.global.exception.ConflictException;
import io.oduck.api.global.exception.NotFoundException;
Expand All @@ -23,6 +17,10 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -34,13 +32,10 @@ public class AttractionPointServiceImpl implements AttractionPointService {

@Override
public IsAttractionPoint isAttractionPoint(Long memberId, Long animeId) {
boolean drawing = false;
boolean story = false;
boolean music = false;
boolean character = false;
boolean voiceActor = false;

List<AttractionPoint> points = attractionPointRepository.findAllByAnimeIdAndMemberId(memberId, animeId);

boolean drawing = false, story = false, music = false, character = false, voiceActor = false;

for (AttractionPoint point : points) {
switch (point.getAttractionElement()) {
case DRAWING -> drawing = true;
Expand All @@ -50,8 +45,8 @@ public IsAttractionPoint isAttractionPoint(Long memberId, Long animeId) {
default -> voiceActor = true;
}
}
return IsAttractionPoint
.builder()

return IsAttractionPoint.builder()
.drawing(drawing)
.story(story)
.music(music)
Expand All @@ -65,75 +60,59 @@ public IsAttractionPoint isAttractionPoint(Long memberId, Long animeId) {
public void save(Long memberId, AttractionPointReq req) {
List<AttractionPoint> findPoint = attractionPointRepository.findAllByAnimeIdAndMemberId(memberId, req.getAnimeId());

List<AttractionElement> elementList = findPoint.stream()
.map(AttractionPoint::getAttractionElement)
.toList();

//중복되지 않은 포인트
List<AttractionElement> matchPoint = req.getAttractionElements().stream()
.filter(attractionElements ->
elementList.stream()
.noneMatch(Predicate.isEqual(attractionElements)))
.toList();

List<AttractionElement> matchPoint = findNonOverlappingValues(findPoint, req);
if (matchPoint.isEmpty()) {
throw new ConflictException("AttractionPoint");
}

Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException("Member"));

Anime anime = animeRepository.findById(req.getAnimeId())
.orElseThrow(() -> new NotFoundException("Anime"));

List<AttractionPoint> points = matchPoint
.stream()
.map(attractionElement -> AttractionPoint
.builder()
.member(member)
.anime(anime)
.attractionElement(attractionElement)
.build())
.toList();
attractionPointRepository.saveAll(points);
attractionPointRepository.saveAll(getAttractionPoint(matchPoint, member, anime));
}

@Override
public CheckAttractionPoint checkAttractionPoint(Long memberId, Long animeId) {
List<AttractionPoint> findPoint = attractionPointRepository.findAllByAnimeIdAndMemberId(memberId, animeId);
return CheckAttractionPoint
.builder()
return CheckAttractionPoint.builder()
.isAttractionPoint(!findPoint.isEmpty())
.build();
}

@Override
public boolean update(Long memberId, Long attractionPointId, UpdateAttractionPoint req) {
AttractionPoint findAttractionPoint = getAttractionPoint(attractionPointId);
public boolean update(Long memberId, AttractionPointReq req) {
List<AttractionPoint> findPoint = attractionPointRepository.findAllByAnimeIdAndMemberId(memberId, req.getAnimeId());

if (findAttractionPoint.getAttractionElement().equals(req.getAttractionElement())) {
if (findPoint.isEmpty()) {
return false;
}
Long findMemberId = findAttractionPoint.getMember().getId();
//입덕 포인트 작성자 인지 확인
Optional
.ofNullable(findMemberId)
.ifPresent(
id -> {
if (!findMemberId.equals(memberId)) {
throw new BadRequestException("Not the author of the attractionPoint.");
}
findAttractionPoint.updateElement(req.getAttractionElement());
}
);
attractionPointRepository.save(findAttractionPoint);

Long findMemberId = findPoint.get(0).getMember().getId();

if (!findMemberId.equals(memberId)) {
throw new BadRequestException("Not the author of the attractionPoint.");
}

List<AttractionElement> findAttractionElement = findNonOverlappingValues(findPoint, req);

if (findAttractionElement.isEmpty()) {
throw new ConflictException("AttractionPoint");
} else {
attractionPointRepository.deleteAllInBatch(findPoint);

Member member = findPoint.get(0).getMember();
Anime anime = findPoint.get(0).getAnime();

attractionPointRepository.saveAll(getAttractionPoint(findAttractionElement, member, anime));
}
return true;
}

@Override
public AttractionPointStats getAttractionPointStats(Long animeId) {
//입덕포인트 / 전체 입덕포인트 개수
Long totalCount = attractionPointRepository.count();
Long totalCount = attractionPointRepository.countByAnimeId(animeId);
double drawing = calculateElementRatio(AttractionElement.DRAWING, animeId, totalCount);
double story = calculateElementRatio(AttractionElement.STORY, animeId, totalCount);
double voiceActor = calculateElementRatio(AttractionElement.VOICE_ACTOR, animeId, totalCount);
Expand All @@ -149,19 +128,27 @@ public AttractionPointStats getAttractionPointStats(Long animeId) {
.build();
}

private AttractionPoint getAttractionPoint(Long attractionPointId) {
return attractionPointRepository.findById(attractionPointId)
.orElseThrow(
() -> new NotFoundException("AttractionPoint")
);
private List<AttractionPoint> getAttractionPoint(List<AttractionElement> elements, Member member, Anime anime){
return elements.stream()
.map(attractionElement -> AttractionPoint.builder()
.member(member)
.anime(anime)
.attractionElement(attractionElement)
.build())
.collect(Collectors.toList());
}

private double calculateElementRatio(AttractionElement element, Long animeId, Long totalCount) {
Long countElementByAnimeId = attractionPointRepository.countElementByAnimeId(element, animeId);
if (countElementByAnimeId <= 0) {
return 0;
}
return (double) countElementByAnimeId / totalCount;
return countElementByAnimeId > 0 ? (double) countElementByAnimeId / totalCount * 100 : 0;
}

}
private List<AttractionElement> findNonOverlappingValues(List<AttractionPoint> findPoint, AttractionPointReq req) {
List<AttractionElement> elementList = findPoint.stream()
.map(AttractionPoint::getAttractionElement)
.toList();
//중복되지 않은 값
return req.getAttractionElements().stream()
.filter(attractionElement -> !elementList.contains(attractionElement))
.collect(Collectors.toList());
}
}

0 comments on commit 72af637

Please sign in to comment.