Skip to content

Commit

Permalink
Merge pull request #36 from GoogleEyes-ewha/feature/#35
Browse files Browse the repository at this point in the history
[FIX] 상품 상세페이지 isWish 수정
  • Loading branch information
yslim21 authored Feb 16, 2024
2 parents bf7c24a + a67ebb1 commit 805e10d
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import com.gdsc.hearo.domain.item.dto.*;
import com.gdsc.hearo.domain.item.service.ItemService;
import com.gdsc.hearo.domain.item.service.WishService;
import com.gdsc.hearo.global.common.BaseResponse;
import com.gdsc.hearo.global.common.BaseResponseStatus;
import com.gdsc.hearo.global.security.CustomUserDetails;
import com.google.api.gax.rpc.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -107,11 +105,14 @@ public ResponseEntity<BaseResponse<SearchResponseDto>> getItemByKeyword(
}

@GetMapping("/{itemId}")
public ResponseEntity<BaseResponse<ItemDetailResponseDto>> getItemDetailById(@PathVariable(name = "itemId") Long itemId) {
public ResponseEntity<BaseResponse<ItemDetailResponseDto>> getItemDetailById(
@PathVariable(name = "itemId") Long itemId,
@AuthenticationPrincipal CustomUserDetails userDetails) {
BaseResponse<ItemDetailResponseDto> response;

try {
ItemDetailResponseDto itemDetailResponse = itemService.getItemDetailById(itemId);
Long userId = userDetails != null? userDetails.getMember().getMemberId():null;
ItemDetailResponseDto itemDetailResponse = itemService.getItemDetailById(itemId,userId);

response = new BaseResponse<>(BaseResponseStatus.SUCCESS, itemDetailResponse);
return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.gdsc.hearo.domain.item.dto.WishRequestDto;
import com.gdsc.hearo.domain.item.dto.WishResponseDto;
import com.gdsc.hearo.domain.item.service.WishService;
import com.gdsc.hearo.global.common.BaseException;
import com.gdsc.hearo.global.common.BaseResponse;
import com.gdsc.hearo.global.common.BaseResponseStatus;
import com.gdsc.hearo.global.security.CustomUserDetails;
Expand Down Expand Up @@ -34,6 +35,9 @@ public ResponseEntity<BaseResponse<WishResponseDto>> addToWishList(@Authenticati
Long userId = userDetails.getMember().getMemberId();
WishResponseDto result = wishService.addToWishList(userId, wishRequestDto.getItemId());




response = new BaseResponse<>(BaseResponseStatus.SUCCESS, result);
return ResponseEntity.ok(response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
@NoArgsConstructor
public class WishResponseDto {
private String result;


}

//상품 위시리스트에 추가할때도 삭제할 때도 쓰임
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface WishRepository extends JpaRepository<Wish,Long> {

//void deleteByMember_MemberIdAndItem_ItemId(Long memberId, Long itemId);
void deleteByMemberAndItem(Member member,Item item);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.gdsc.hearo.domain.item.dto.ItemDetailResponseDto;
import com.gdsc.hearo.domain.item.dto.ItemDto;
import com.gdsc.hearo.domain.item.entity.Item;

import java.util.List;

Expand All @@ -11,9 +10,6 @@ public interface ItemService {
List<ItemDto> getItemByCategory(Long categoryId);//카테고리 검색
List<ItemDto> getItemByKeyword(String keyword);//키워드 검색

ItemDetailResponseDto getItemDetailById(Long itemId);
ItemDetailResponseDto getItemDetailById(Long itemId,Long userId);

//List<ItemDetailResponseDto> getItemDetail(Long itemId);

//Item findItemById(Long itemId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import com.gdsc.hearo.domain.item.dto.ItemDetailResponseDto;
import com.gdsc.hearo.domain.item.dto.ItemDto;
import com.gdsc.hearo.domain.item.entity.Item;
import com.gdsc.hearo.domain.item.entity.Wish;
import com.gdsc.hearo.domain.item.repository.ItemRepository;
import com.gdsc.hearo.domain.item.repository.WishRepository;
import com.gdsc.hearo.domain.member.entity.Member;
import com.gdsc.hearo.domain.member.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
Expand All @@ -15,9 +21,18 @@ public class ItemServiceImpl implements ItemService {
private final ItemRepository itemRepository;
private final DocumentAiService documentAiService;

public ItemServiceImpl(ItemRepository itemRepository, DocumentAiService documentAiService) {
private final MemberRepository memberRepository;

private final WishRepository wishRepository;

@Autowired
public ItemServiceImpl(ItemRepository itemRepository, DocumentAiService documentAiService,
MemberRepository memberRepository,WishRepository wishRepository) {
this.itemRepository = itemRepository;
this.documentAiService = documentAiService;
this.memberRepository = memberRepository;
this.wishRepository = wishRepository;

}

public List<ItemDto> getItemByCategory(Long categoryId){
Expand Down Expand Up @@ -61,7 +76,9 @@ public NotFoundException(String message) {
super(message);
}
}
public ItemDetailResponseDto getItemDetailById(Long itemId){


public ItemDetailResponseDto getItemDetailById(Long itemId,Long userId){
Item item = itemRepository.findById(itemId)
.orElseThrow(() -> new NotFoundException("상품을 찾을 수 없습니다."));

Expand All @@ -73,6 +90,8 @@ public ItemDetailResponseDto getItemDetailById(Long itemId){
e.printStackTrace();
}

boolean isWish = isItemInWishlist(itemId,userId);

ItemDetailResponseDto itemDetailResponse = ItemDetailResponseDto.builder()
.name(item.getName())
.itemInfo(item.getItemInfo())
Expand All @@ -83,9 +102,23 @@ public ItemDetailResponseDto getItemDetailById(Long itemId){
.nutritionImg(item.getNutritionImg())
.nutritionText(nutritionText) // Document AI API 호출 결과로 얻은 텍스트를 여기에 설정
.kurlyUrl(item.getItemUrl())
.isWish(false) // 필요에 따라 설정
.isWish(isWish) // 필요에 따라 설정
.build();

return itemDetailResponse;
}

private boolean isItemInWishlist(Long itemId, Long userId) {
// 사용자 정보를 조회
Optional<Member> member = memberRepository.findById(userId);
if (member.isPresent()) {
// 사용자의 위시리스트를 조회
List<Wish> wishList = wishRepository.findByMember_MemberId(userId);
// 해당 상품이 위시리스트에 있는지 여부를 반환
return wishList.stream().anyMatch(wish -> wish.getItem().getItemId().equals(itemId));
} else {
// 사용자가 없는 경우에는 상품이 위시리스트에 있을 수 없음
return false;
}
}
}
30 changes: 26 additions & 4 deletions src/main/java/com/gdsc/hearo/domain/item/service/WishService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.gdsc.hearo.domain.item.service;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.gdsc.hearo.domain.item.dto.ItemDto;
import com.gdsc.hearo.domain.item.dto.WishListResponseDto;
import com.gdsc.hearo.domain.item.dto.WishRequestDto;
import com.gdsc.hearo.domain.item.dto.WishResponseDto;
import com.gdsc.hearo.domain.item.entity.Item;
import com.gdsc.hearo.domain.item.entity.Wish;
import com.gdsc.hearo.domain.item.repository.ItemRepository;
import com.gdsc.hearo.domain.item.repository.WishRepository;
import com.gdsc.hearo.domain.member.entity.Member;
import com.gdsc.hearo.domain.member.repository.MemberRepository;
import com.gdsc.hearo.global.common.BaseException;
import com.gdsc.hearo.global.common.BaseResponseStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -31,13 +33,19 @@ public WishService(WishRepository wishRepository,ItemRepository itemRepository,
this.memberRepository = memberRepository;
}

public WishResponseDto addToWishList(Long userId, Long itemId){
@Transactional
public WishResponseDto addToWishList(Long userId, Long itemId) throws BaseException {

Item item = itemRepository.findById(itemId)
.orElseThrow(() -> new RuntimeException("상품을 찾을 수 없습니다."));
.orElseThrow(() -> new BaseException(BaseResponseStatus.NO_CONTENT));

Member member = memberRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("회원을 찾을 수 없습니다."));
.orElseThrow(() -> new BaseException(BaseResponseStatus.FAILED_TO_FIND_USER));

// 이미 위시리스트에 있는지 확인
if (isItemInWishlist(itemId, userId)) {
throw new BaseException(BaseResponseStatus.DUPICATE_ITEM);
}

Wish wish = Wish.builder()
.item(item)
Expand Down Expand Up @@ -100,4 +108,18 @@ public WishResponseDto removeFromWishList(Long userId, Long itemId){
.build();
}
}

//상품이 위시리스트에 존재하는지 확인
public boolean isItemInWishlist(Long itemId, Long userId) {
Optional<Member> member = memberRepository.findById(userId);
if (member.isPresent()) {
List<Wish> wishList = wishRepository.findByMember_MemberId(userId);
return wishList.stream().anyMatch(wish -> wish.getItem().getItemId().equals(itemId));
} else {
return false;
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum BaseResponseStatus {
DUPICATE_USER_ID(3003, false, "이미 존재하는 아이디입니다."),
ACCESS_DENIED(3004, false, "알 수 없는 이유로 요청이 거절되었습니다."),
EXPIRED_REFRESH_TOKEN(3005, false, "만료된 refresh token 입니다."),
FAILED_TO_FIND_USER(3006,false,"존재하지 않는 회원입니다."),

// 3100~ : item 관련 오류
NO_CONTENT(3100, false, "상품이 존재하지 않습니다."),
Expand All @@ -42,6 +43,9 @@ public enum BaseResponseStatus {
// 3300~ : TTS 관련 오류
NO_REVIEW_TTS_FILE(3300, false, "리뷰 요약 음성 파일이 존재하지 않습니다."),

//3400~ : wishList 관련 오류
DUPICATE_ITEM(3400,false,"이미 위시리스트에 존재하는 상품입니다."),


// =====================================

Expand Down

0 comments on commit 805e10d

Please sign in to comment.