From 491af092f792ce22ba68802395a2f8b3b24b054e Mon Sep 17 00:00:00 2001 From: chaeyoungeee Date: Tue, 30 Jul 2024 22:45:50 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B4=80=EC=8B=AC=20=EB=AC=B8=ED=99=94?= =?UTF-8?q?=20=ED=86=A0=EA=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 관심 문화가 토글되는 기능을 추가했음 이미 있을 시 -> 삭제, 없을 시 -> 추가하는 로직으로 개발 --- .../api/culture/CultureController.java | 19 ++++- .../culture/CultureInterestRepository.java | 11 ++- .../service/culture/CultureService.java | 57 +++++++++++++- .../service/culture/CultureServiceTest.java | 78 ------------------- 4 files changed, 79 insertions(+), 86 deletions(-) delete mode 100644 src/test/java/likelion/MZConnent/service/culture/CultureServiceTest.java diff --git a/src/main/java/likelion/MZConnent/api/culture/CultureController.java b/src/main/java/likelion/MZConnent/api/culture/CultureController.java index 50e0bc3..63f6dba 100644 --- a/src/main/java/likelion/MZConnent/api/culture/CultureController.java +++ b/src/main/java/likelion/MZConnent/api/culture/CultureController.java @@ -12,10 +12,9 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; @RestController @Slf4j @@ -58,4 +57,16 @@ public ResponseEntity getCultureDetailInfo(@PathVariable( return ResponseEntity.ok(response); } + + // 문화 관심 토글 + @PostMapping("/api/cultures/{cultureId}/interests") + public ResponseEntity> addCultureInterest(@PathVariable("cultureId") Long cultureId, @AuthenticationPrincipal UserPrinciple userPrinciple) { + boolean status = cultureService.toggleCultureInterest(userPrinciple.getEmail(), cultureId); + + if (status) { + return ResponseEntity.ok(Map.of("message", "관심 문화 추가 성공")); + } else { + return ResponseEntity.ok(Map.of("message", "관심 문화 삭제 성공")); + } + } } diff --git a/src/main/java/likelion/MZConnent/repository/culture/CultureInterestRepository.java b/src/main/java/likelion/MZConnent/repository/culture/CultureInterestRepository.java index 3c83187..a873bf5 100644 --- a/src/main/java/likelion/MZConnent/repository/culture/CultureInterestRepository.java +++ b/src/main/java/likelion/MZConnent/repository/culture/CultureInterestRepository.java @@ -1,9 +1,18 @@ package likelion.MZConnent.repository.culture; +import likelion.MZConnent.domain.culture.Culture; import likelion.MZConnent.domain.culture.CultureInterest; +import likelion.MZConnent.domain.member.Member; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface CultureInterestRepository extends JpaRepository { -} + + boolean existsByMemberAndCulture(Member member, Culture culture); + + Optional findByMemberAndCulture(Member member, Culture culture); +} \ No newline at end of file diff --git a/src/main/java/likelion/MZConnent/service/culture/CultureService.java b/src/main/java/likelion/MZConnent/service/culture/CultureService.java index 8a62dd7..ba48f47 100644 --- a/src/main/java/likelion/MZConnent/service/culture/CultureService.java +++ b/src/main/java/likelion/MZConnent/service/culture/CultureService.java @@ -1,12 +1,16 @@ package likelion.MZConnent.service.culture; 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.dto.culture.response.CultureDetailResponse; import likelion.MZConnent.dto.culture.response.CulturesSimpleResponse; import likelion.MZConnent.dto.paging.response.PageContentResponse; import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse; +import likelion.MZConnent.repository.culture.CultureInterestRepository; import likelion.MZConnent.repository.culture.CultureRepository; +import likelion.MZConnent.repository.member.MemberRepository; import likelion.MZConnent.repository.review.ReviewRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -26,6 +30,9 @@ @RequiredArgsConstructor public class CultureService { private final CultureRepository cultureRepository; + private final CultureInterestRepository cultureInterestRepository; + private final MemberRepository memberRepository; + private final int PAGE_SIZE = 6; public PageContentResponse getCulturesSimpleList(Long cultureCategoryId, int page) { @@ -65,11 +72,55 @@ public PageContentResponse getMyIntersetCulturesSimpleLi } public CultureDetailResponse getCultureDetailInfo(Long cultureId) { + Culture culture = getCultureById(cultureId); + return CultureDetailResponse.builder() + .culture(culture).build(); + } + + + // 문화 관심 토글 + public boolean toggleCultureInterest(String email, Long cultureId) { + + Member member = memberRepository.findByEmail(email).orElseThrow(() -> { + log.info("해당 회원이 존재하지 않음."); + return new IllegalArgumentException("해당 회원이 존재하지 않습니다."); + }); + Culture culture = getCultureById(cultureId); + + boolean exists = cultureInterestRepository.existsByMemberAndCulture(member, culture); + + + if (exists) { // 이미 문화를 관심 등록 해놓은 경우 (삭제) + CultureInterest cultureInterest = cultureInterestRepository.findByMemberAndCulture(member, culture) + .get(); + cultureInterestRepository.delete(cultureInterest); + + member.getCultureInterests().remove(cultureInterest); + culture.getCultureInterests().remove(cultureInterest); + culture.setInterestCount(culture.getInterestCount() - 1); + return false; + } + else { // 문화를 관심 등록 해놓지 않은 경우 (추가) + CultureInterest cultureInterest = cultureInterestRepository.save(CultureInterest.builder() + .member(member) + .culture(culture) + .build()); + + member.getCultureInterests().add(cultureInterest); + culture.getCultureInterests().add(cultureInterest); + culture.setInterestCount(culture.getInterestCount()+1); + + return true; + } + + + } + + private Culture getCultureById(Long cultureId) { Culture culture = cultureRepository.findById(cultureId).orElseThrow(() -> { - log.info("해당 문화가 존재하지 않습니다."); + log.info("해당 문화가 존재하지 않음."); return new IllegalArgumentException("해당 문화가 존재하지 않습니다."); }); - return CultureDetailResponse.builder() - .culture(culture).build(); + return culture; } } diff --git a/src/test/java/likelion/MZConnent/service/culture/CultureServiceTest.java b/src/test/java/likelion/MZConnent/service/culture/CultureServiceTest.java deleted file mode 100644 index 976aedf..0000000 --- a/src/test/java/likelion/MZConnent/service/culture/CultureServiceTest.java +++ /dev/null @@ -1,78 +0,0 @@ -package likelion.MZConnent.service.culture; - -import likelion.MZConnent.domain.culture.Culture; -import likelion.MZConnent.domain.member.Member; -import likelion.MZConnent.dto.culture.response.CultureCategoryResponse; -import likelion.MZConnent.dto.culture.response.CultureDetailResponse; -import likelion.MZConnent.repository.culture.CultureRepository; -import likelion.MZConnent.repository.member.MemberRepository; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.Rollback; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Optional; - -@Slf4j -@SpringBootTest -@Transactional -class CultureServiceTest { - @Autowired - CultureCategoryService cultureCategoryService; - - @Autowired - MemberRepository memberRepository; - - @Autowired - CultureService cultureService; - - @Autowired - CultureRepository cultureRepository; - - @Test - public void getCultureCategory() throws Exception { - //given - - //when - CultureCategoryResponse all = cultureCategoryService.getAllCultureCategories(); - - //then -// Assertions.assertEquals(all.getCultureCategories().size(), 5); - } - - @Test - public void getCultureDetailInfo() throws Exception { - //given - Long cultureId = 1L; - - //when - CultureDetailResponse cultureDetailInfo = cultureService.getCultureDetailInfo(cultureId); - - //then - log.info("문화: {}", cultureDetailInfo); - Assertions.assertEquals(cultureDetailInfo.getCultureId(), 1L); - } - - @Test -// @Rollback(value = false) - public void addCultureInterest() throws Exception { - //given - Culture culture = cultureRepository.findById(4L).get(); - Member member = memberRepository.findById(2L).get(); - - //when - int interestCount = cultureService.addCultureInterest(member.getEmail(), culture.getCultureId()); - - //then - log.info("관심수: {}", interestCount); - - Assertions.assertEquals(member.getCultureInterests().size(), 2); - Assertions.assertEquals(culture.getCultureInterests().size(), 2); - Assertions.assertEquals(culture.getInterestCount(), interestCount); - - - } -} \ No newline at end of file