Skip to content

Commit

Permalink
Merge pull request #74 from LIKELION-TEAM4-HACKATHON/feature/culture-…
Browse files Browse the repository at this point in the history
…detail

후기 좋아요 토글 기능 추가
  • Loading branch information
oosedus authored Jul 31, 2024
2 parents d240d4f + 6db3dd5 commit 8d16987
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public ResponseEntity<CultureDetailResponse> getCultureDetailInfo(@PathVariable(
}

// 문화 관심 토글
@PostMapping("/api/cultures/{cultureId}/interests")
public ResponseEntity<Map<String, String>> addCultureInterest(@PathVariable("cultureId") Long cultureId, @AuthenticationPrincipal UserPrinciple userPrinciple) {
@PutMapping("/api/cultures/{cultureId}/interests")
public ResponseEntity<Map<String, String>> toggleCultureInterest(@PathVariable("cultureId") Long cultureId, @AuthenticationPrincipal UserPrinciple userPrinciple) {
boolean status = cultureService.toggleCultureInterest(userPrinciple.getEmail(), cultureId);

if (status) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/likelion/MZConnent/api/review/ReviewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -58,4 +59,16 @@ public ResponseEntity<SaveReviewResponse> saveReview(
return ResponseEntity.status(500).body(null);
}
}

// 후기 좋아요 토글
@PutMapping("/api/reviews/{reviewId}/likes")
public ResponseEntity<Map<String, String>> toggleReviewLike(@PathVariable("reviewId") Long reviewId, @AuthenticationPrincipal UserPrinciple userPrinciple) {
boolean status = reviewService.toggleReviewLike(userPrinciple.getEmail(), reviewId);

if (status) {
return ResponseEntity.ok(Map.of("message", "후기 좋아요 추가 성공"));
} else {
return ResponseEntity.ok(Map.of("message", "후기 좋아요 삭제 성공"));
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/likelion/MZConnent/domain/review/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.List;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Review {
@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package likelion.MZConnent.repository.review;

import likelion.MZConnent.domain.culture.Culture;
import likelion.MZConnent.domain.culture.CultureInterest;
import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.domain.review.Review;
import likelion.MZConnent.domain.review.ReviewLike;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface ReviewLikeRepository extends JpaRepository<ReviewLike, Long> {

boolean existsByMemberAndReview(Member member, Review review);

Optional<ReviewLike> findByMemberAndReview(Member member, Review review);
}
58 changes: 56 additions & 2 deletions src/main/java/likelion/MZConnent/service/review/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import jakarta.transaction.Transactional;
import likelion.MZConnent.domain.culture.Culture;
import likelion.MZConnent.domain.culture.CultureInterest;
import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.domain.review.Review;
import likelion.MZConnent.domain.review.ReviewLike;
import likelion.MZConnent.dto.paging.response.PageContentResponse;
import likelion.MZConnent.dto.review.request.SaveReviewRequest;
import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse;
import likelion.MZConnent.dto.review.response.SaveReviewResponse;
import likelion.MZConnent.repository.culture.CultureRepository;
import likelion.MZConnent.repository.member.MemberRepository;
import likelion.MZConnent.repository.review.ReviewLikeRepository;
import likelion.MZConnent.repository.review.ReviewRepository;
import likelion.MZConnent.service.image.S3ImageService;
import lombok.RequiredArgsConstructor;
Expand All @@ -36,6 +39,7 @@ public class ReviewService {
private final S3ImageService s3ImageService;
private final MemberRepository memberRepository;
private final CultureRepository cultureRepository;
private final ReviewLikeRepository reviewLikeRepository;
private final int PAGE_SIZE = 6;

public PageContentResponse<ReviewsSimpleResponse> getReviewsSimpleList(String keyword, int page) {
Expand All @@ -61,8 +65,8 @@ public PageContentResponse<ReviewsSimpleResponse> getReviewsSimpleList(String ke
public SaveReviewResponse createReview(String email, SaveReviewRequest request, List<MultipartFile> images, Long cultureId) throws IOException {
List<String> imageUrls = uploadImages(images);

Member member = memberRepository.findByEmail(email).orElseThrow(() -> new IllegalArgumentException("회원 정보를 찾을 수 없습니다."));
Culture culture = cultureRepository.findById(cultureId).orElseThrow(() -> new IllegalArgumentException("문화 정보를 찾을 수 없습니다."));
Member member = findMemberByEmail(email);
Culture culture = findCultureById(cultureId);

Review review = buildReview(request, imageUrls, member, culture);
review = reviewRepository.save(review);
Expand Down Expand Up @@ -116,4 +120,54 @@ private SaveReviewResponse buildSaveReviewResponse(Review review, Member member,
.likeCount(review.getLikeCount())
.build();
}

public boolean toggleReviewLike(String email, Long reviewId) {

Member member = findMemberByEmail(email);
Review review = findReviewById(reviewId);

boolean exists = reviewLikeRepository.existsByMemberAndReview(member, review);


if (exists) { // 이미 후기 좋아요를 등록한 경우 (삭제)
ReviewLike reviewLike = reviewLikeRepository.findByMemberAndReview(member, review).get();
reviewLikeRepository.delete(reviewLike);

member.getReviewLikes().remove(reviewLike);
review.getReviewLikes().remove(reviewLike);
review.setLikeCount(review.getLikeCount() - 1);
return false;
}
else { // 후기 좋아요를 하지 않은 경우 (추가)
ReviewLike reviewLike = reviewLikeRepository.save(ReviewLike.builder()
.member(member)
.review(review)
.build());

member.getReviewLikes().add(reviewLike);
review.getReviewLikes().add(reviewLike);
review.setLikeCount(review.getLikeCount() + 1);
return true;
}
}

private Review findReviewById(Long reviewId) {
return reviewRepository.findById(reviewId).orElseThrow(() -> {
log.info("후기 정보를 찾을 수 없음.");
return new IllegalArgumentException("후기 정보를 찾을 수 없습니다.");
});
}
private Culture findCultureById(Long cultureId) {
return cultureRepository.findById(cultureId).orElseThrow(() -> {
log.info("문화 정보를 찾을 수 없음.");
return new IllegalArgumentException("문화 정보를 찾을 수 없습니다.");
});
}

private Member findMemberByEmail(String email) {
return memberRepository.findByEmail(email).orElseThrow(() -> {
log.info("회원 정보를 찾을 수 없음.");
return new IllegalArgumentException("회원 정보를 찾을 수 없습니다.");
});
}
}

0 comments on commit 8d16987

Please sign in to comment.