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

[Feature] 찜 상품 삭제 기능 구현 #21

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions src/main/java/com/gdsc/hearo/HearoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.web.filter.HiddenHttpMethodFilter;

@EnableJpaAuditing
@SpringBootApplication
public class HearoApplication {


public static void main(String[] args) {

SpringApplication.run(HearoApplication.class, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public WishController(WishService wishService){
this.wishService = wishService;
}

@PostMapping
@PostMapping/*wishRequestDto가 꼭필요한가?*/
public ResponseEntity<BaseResponse<WishResponseDto>> addToWishList(@AuthenticationPrincipal CustomUserDetails userDetails,

@RequestBody WishRequestDto wishRequestDto){
Expand Down Expand Up @@ -62,4 +62,27 @@ public ResponseEntity<BaseResponse<WishListResponseDto>> getWishList(@Authentica



@DeleteMapping("/{itemId}")
public ResponseEntity<BaseResponse<WishResponseDto>> deleteFromWishList(
@AuthenticationPrincipal CustomUserDetails userDetails,
@PathVariable(name = "itemId") Long itemId){

BaseResponse<WishResponseDto> response;

try{
Long userId = userDetails.getMember().getMemberId();
WishResponseDto result = wishService.removeFromWishList(userId, itemId);
//wishService.deleteFromWishList(userDetails.getMember().getMemberId(),itemId);

response = new BaseResponse<>(BaseResponseStatus.SUCCESS, result);
return ResponseEntity.ok(response);
}catch(Exception e){
response = new BaseResponse<>(BaseResponseStatus.INTERNAL_SERVER_ERROR, null);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ public class WishRequestDto {

private Long itemId;
}
//상품 위시리스트에 추가할때도 삭제할 때도 쓰임
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
public class WishResponseDto {
private String result;
}

//상품 위시리스트에 추가할때도 삭제할 때도 쓰임
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.gdsc.hearo.domain.item.repository;

import com.gdsc.hearo.domain.item.entity.Item;
import com.gdsc.hearo.domain.item.entity.Wish;
import com.gdsc.hearo.domain.member.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface WishRepository extends JpaRepository<Wish,Long> {

List<Wish> findByMember_MemberId(Long memberId);

//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 @@ -10,9 +10,9 @@
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.domain.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -76,4 +76,28 @@ public WishListResponseDto getWishList(Long userId){


}

@Transactional
public WishResponseDto removeFromWishList(Long userId, Long itemId){
try {
Item item = itemRepository.findById(itemId)
.orElseThrow(() -> new RuntimeException("상품을 찾을 수 없습니다."));


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

wishRepository.deleteByMemberAndItem(member,item);

return WishResponseDto.builder()
.result("위시리스트에서 상품을 삭제하였습니다.")
.build();
}catch(Exception e) {
e.printStackTrace();

return WishResponseDto.builder()
.result("서버 오류")
.build();
}
}
}
Loading