Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 목표 추천 #141

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RequiredArgsConstructor
Expand Down Expand Up @@ -105,4 +106,11 @@ public SuccessResponse<String> deleteTarget(

return SuccessResponse.ok("목표가 성공적으로 삭제되었습니다.");
}

// 목표 추천하기
@GetMapping("/recommend")
public SuccessResponse<?> recommendTarget(@AuthenticationPrincipal MemberPrincipal memberPrincipal){
HashMap<String, String> response = targetService.recommendTarget(memberPrincipal.getMemberId());
return SuccessResponse.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
@Repository
public interface TargetRepository extends JpaRepository<Target, Long> {

// 특정 memberId를 가진 Bankbook의 모든 Target을 조회하는 JPQL 쿼리
// 특정 memberId를 가진 Target의 모든 Target을 조회하는 JPQL 쿼리
@Query("SELECT t FROM Target t JOIN t.account a WHERE a.member.memberId = :memberId")
List<Target> findAllByMemberId(@Param("memberId") Long memberId);

@Query("SELECT t FROM Target t "
+ "JOIN t.account a "
+ "JOIN a.member m "
+ "JOIN Pet p ON p.member.memberId = m.memberId "
+ "WHERE p.petType = :petType "
+ "AND m.memberId != :memberId")
List<Target> findAllTargetsExceptMine(@Param("memberId") Long memberId, @Param("petType") String petType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,59 @@

import com.fav.daengnyang.domain.account.entity.Account;
import com.fav.daengnyang.domain.account.repository.AccountRepository;
import com.fav.daengnyang.domain.pet.entity.Pet;
import com.fav.daengnyang.domain.pet.repository.PetRepository;
import com.fav.daengnyang.domain.target.entity.Target;
import com.fav.daengnyang.domain.target.repository.TargetRepository;
import com.fav.daengnyang.domain.target.service.dto.request.CreateTargetRequest;
import com.fav.daengnyang.domain.target.service.dto.response.TargetDetailResponse;
import com.fav.daengnyang.domain.target.service.dto.response.TargetResponse;
import com.fav.daengnyang.domain.targetDetail.entity.TargetDetail;
import com.fav.daengnyang.domain.targetDetail.repository.TargetDetailRepository;
import com.fav.daengnyang.global.exception.CustomException;
import com.fav.daengnyang.global.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class TargetService {

private final TargetRepository targetRepository;
private final TargetDetailRepository targetDetailRepository;
private final AccountRepository accountRepository;
private final PetRepository petRepository;
private final Random random = new Random();

// 타겟 추천 메소드
public HashMap<String, String> recommendTarget(Long memberId){
// 1. 반려동물 정보 찾기
Pet pet = petRepository.findByMemberMemberId(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.PET_NOT_FOUND));

// 2. 다른 목표 반려동물 찾기
List<Target> targetList = targetRepository.findAllTargetsExceptMine(memberId, pet.getPetType());
log.info("targetList" + targetList);

// 3. 목표 추천
int index = random.nextInt(targetList.size());
Target target = targetList.get(index);

// 4. response
HashMap<String, String> response = new HashMap<>();
response.put("recommend", target.getTargetTitle());
return response;
}


// 목표 생성 메소드
@Transactional
Expand Down
Loading