diff --git a/be/src/main/java/com/secondhand/post/repository/InterestRepository.java b/be/src/main/java/com/secondhand/post/repository/InterestRepository.java index 831ad6e68..fa5b30e23 100644 --- a/be/src/main/java/com/secondhand/post/repository/InterestRepository.java +++ b/be/src/main/java/com/secondhand/post/repository/InterestRepository.java @@ -4,4 +4,5 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface InterestRepository extends JpaRepository, InterestRepositoryCustom { + } diff --git a/be/src/main/java/com/secondhand/post/repository/InterestRepositoryCustom.java b/be/src/main/java/com/secondhand/post/repository/InterestRepositoryCustom.java index 351681efe..291cacc29 100644 --- a/be/src/main/java/com/secondhand/post/repository/InterestRepositoryCustom.java +++ b/be/src/main/java/com/secondhand/post/repository/InterestRepositoryCustom.java @@ -2,13 +2,19 @@ import com.secondhand.category.dto.CategoryInInterestDto; import com.secondhand.post.dto.PostMetaDto; +import com.secondhand.post.entity.Interest; +import com.secondhand.post.entity.PostMeta; +import com.secondhand.user.entity.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import java.util.List; +import java.util.Optional; public interface InterestRepositoryCustom { Page findMyInterestsPosts(Pageable pageable, Long loggedInUserId); List interestCategory(); + + Optional findByUserAndPostMeta(User user, PostMeta postMeta); } diff --git a/be/src/main/java/com/secondhand/post/repository/InterestRepositoryImpl.java b/be/src/main/java/com/secondhand/post/repository/InterestRepositoryImpl.java index 90f4b7adb..225e71ab3 100644 --- a/be/src/main/java/com/secondhand/post/repository/InterestRepositoryImpl.java +++ b/be/src/main/java/com/secondhand/post/repository/InterestRepositoryImpl.java @@ -2,17 +2,20 @@ import com.querydsl.core.QueryResults; import com.querydsl.jpa.impl.JPAQueryFactory; -import com.secondhand.category.dto.CategoryDto; import com.secondhand.category.dto.CategoryInInterestDto; import com.secondhand.category.dto.QCategoryInInterestDto; import com.secondhand.post.dto.PostMetaDto; import com.secondhand.post.dto.QPostMetaDto; +import com.secondhand.post.entity.Interest; +import com.secondhand.post.entity.PostMeta; +import com.secondhand.user.entity.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import javax.persistence.EntityManager; import java.util.List; +import java.util.Optional; import static com.secondhand.post.entity.QInterest.interest; import static com.secondhand.post.entity.QPostMeta.postMeta; @@ -64,4 +67,14 @@ public List interestCategory() { .distinct() .fetch(); } + + @Override + public Optional findByUserAndPostMeta(User user, PostMeta postMeta) { + + return Optional.ofNullable(queryFactory + .selectFrom(interest) + .where(interest.user.id.eq(user.getId()), interest.postMeta.id.eq(postMeta.getId())) + .fetchOne()); + } + } diff --git a/be/src/main/java/com/secondhand/user/UserController.java b/be/src/main/java/com/secondhand/user/UserController.java index f33b2c091..86506de9d 100644 --- a/be/src/main/java/com/secondhand/user/UserController.java +++ b/be/src/main/java/com/secondhand/user/UserController.java @@ -46,6 +46,21 @@ public ResponseEntity addInterestPost(@PathVariable Long postId, .body(new CustomResponse( "success", 200, - "관심상품 추가 / 삭제 성공")); + "관심상품 추가 성공")); + } + + @DeleteMapping("/{postId}") + public ResponseEntity deleteInterestPost(@PathVariable Long postId, @RequestHeader("Authorization") String token) { + + LoggedInUser loggedInUser = jwtUtil.extractedUserFromToken(token); + + userService.deleteInterestPost(postId, loggedInUser); + + return ResponseEntity + .ok() + .body(new CustomResponse( + "success", + 200, + "관심상품 삭제 성공")); } } diff --git a/be/src/main/java/com/secondhand/user/UserService.java b/be/src/main/java/com/secondhand/user/UserService.java index 36d740b9c..e22998654 100644 --- a/be/src/main/java/com/secondhand/user/UserService.java +++ b/be/src/main/java/com/secondhand/user/UserService.java @@ -9,10 +9,12 @@ import com.secondhand.user.login.dto.LoggedInUser; import com.secondhand.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.transaction.Transactional; +@Slf4j @Service @RequiredArgsConstructor public class UserService { @@ -41,4 +43,15 @@ public void addInterestPost(long postId, LoggedInUser loggedInUser) { Interest interest = new Interest(user, postMeta); interestRepository.save(interest); } + + @Transactional + public void deleteInterestPost(long postId, LoggedInUser loggedInUser) { + + PostMeta postMeta = postMetaRepository.findById(postId).orElseThrow(); + User user = userRepository.findById(loggedInUser.getId()).orElseThrow(); + + Interest interest = interestRepository.findByUserAndPostMeta(user, postMeta).orElseThrow(); + + interestRepository.delete(interest); + } }