Skip to content

Commit

Permalink
feat: 관심 상품 목록에서 관심 상품 삭제하기 기능 구현(#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
acceptor-gyu committed Jun 14, 2023
1 parent bb1200e commit b2c403e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface InterestRepository extends JpaRepository<Interest, Long>, InterestRepositoryCustom {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostMetaDto> findMyInterestsPosts(Pageable pageable, Long loggedInUserId);

List<CategoryInInterestDto> interestCategory();

Optional<Interest> findByUserAndPostMeta(User user, PostMeta postMeta);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,4 +67,14 @@ public List<CategoryInInterestDto> interestCategory() {
.distinct()
.fetch();
}

@Override
public Optional<Interest> 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());
}

}
17 changes: 16 additions & 1 deletion be/src/main/java/com/secondhand/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ public ResponseEntity<CustomResponse> addInterestPost(@PathVariable Long postId,
.body(new CustomResponse(
"success",
200,
"관심상품 추가 / 삭제 성공"));
"관심상품 추가 성공"));
}

@DeleteMapping("/{postId}")
public ResponseEntity<CustomResponse> 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,
"관심상품 삭제 성공"));
}
}
13 changes: 13 additions & 0 deletions be/src/main/java/com/secondhand/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b2c403e

Please sign in to comment.