Skip to content

Commit

Permalink
feat: 관심 문화 토글
Browse files Browse the repository at this point in the history
관심 문화가 토글되는 기능을 추가했음
이미 있을 시 -> 삭제, 없을 시 -> 추가하는 로직으로 개발
  • Loading branch information
chaeyoungeee committed Jul 30, 2024
1 parent 24f0f70 commit 491af09
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,4 +57,16 @@ public ResponseEntity<CultureDetailResponse> getCultureDetailInfo(@PathVariable(

return ResponseEntity.ok(response);
}

// 문화 관심 토글
@PostMapping("/api/cultures/{cultureId}/interests")
public ResponseEntity<Map<String, String>> 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", "관심 문화 삭제 성공"));
}
}
}
Original file line number Diff line number Diff line change
@@ -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<CultureInterest, Long> {
}

boolean existsByMemberAndCulture(Member member, Culture culture);

Optional<CultureInterest> findByMemberAndCulture(Member member, Culture culture);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CulturesSimpleResponse> getCulturesSimpleList(Long cultureCategoryId, int page) {
Expand Down Expand Up @@ -65,11 +72,55 @@ public PageContentResponse<CulturesSimpleResponse> 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;
}
}

This file was deleted.

0 comments on commit 491af09

Please sign in to comment.