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; } }