diff --git a/server/src/main/java/com/project/yozmcafe/controller/CafeController.java b/server/src/main/java/com/project/yozmcafe/controller/CafeController.java index 79e064e3..a477f93d 100644 --- a/server/src/main/java/com/project/yozmcafe/controller/CafeController.java +++ b/server/src/main/java/com/project/yozmcafe/controller/CafeController.java @@ -2,7 +2,6 @@ import com.project.yozmcafe.controller.dto.cafe.CafeRankResponse; import com.project.yozmcafe.controller.dto.cafe.CafeResponse; -import com.project.yozmcafe.domain.member.Member; import com.project.yozmcafe.service.CafeService; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; @@ -28,8 +27,8 @@ public CafeController(final CafeService cafeService) { } @GetMapping - public ResponseEntity> getCafesForLoggedInMember(final Member member) { - List cafeResponses = cafeService.getCafesForLoginMember(member, PAGE_SIZE); + public ResponseEntity> getCafesForLoggedInMember(@LoginUser final String memberId) { + List cafeResponses = cafeService.getCafesForLoginMember(memberId, PAGE_SIZE); return ResponseEntity.ok(cafeResponses); } diff --git a/server/src/main/java/com/project/yozmcafe/controller/LikedCafeController.java b/server/src/main/java/com/project/yozmcafe/controller/LikedCafeController.java index f46fa59a..d6594220 100644 --- a/server/src/main/java/com/project/yozmcafe/controller/LikedCafeController.java +++ b/server/src/main/java/com/project/yozmcafe/controller/LikedCafeController.java @@ -2,7 +2,6 @@ import com.project.yozmcafe.controller.dto.cafe.LikedCafeResponse; import com.project.yozmcafe.controller.dto.cafe.LikedCafeThumbnailResponse; -import com.project.yozmcafe.domain.member.Member; import com.project.yozmcafe.service.LikedCafeService; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; @@ -41,10 +40,10 @@ public ResponseEntity> getLikedCafes(@PathVariable("memb } @PostMapping("/cafes/{cafeId}/likes") - public ResponseEntity updateLikes(final Member member, + public ResponseEntity updateLikes(@LoginUser final String memberId, @PathVariable("cafeId") final long cafeId, @RequestParam("isLiked") final boolean isLiked) { - likedCafeService.updateLike(member, cafeId, isLiked); + likedCafeService.updateLike(memberId, cafeId, isLiked); return ResponseEntity.ok().build(); } } diff --git a/server/src/main/java/com/project/yozmcafe/controller/LoginArgumentResolver.java b/server/src/main/java/com/project/yozmcafe/controller/LoginArgumentResolver.java index da9a394d..efd16425 100644 --- a/server/src/main/java/com/project/yozmcafe/controller/LoginArgumentResolver.java +++ b/server/src/main/java/com/project/yozmcafe/controller/LoginArgumentResolver.java @@ -1,8 +1,7 @@ package com.project.yozmcafe.controller; -import static com.project.yozmcafe.exception.ErrorCode.INVALID_TOKEN; -import static com.project.yozmcafe.exception.ErrorCode.TOKEN_NOT_EXIST; - +import com.project.yozmcafe.exception.TokenException; +import com.project.yozmcafe.service.auth.JwtTokenProvider; import org.springframework.core.MethodParameter; import org.springframework.stereotype.Component; import org.springframework.web.bind.support.WebDataBinderFactory; @@ -10,10 +9,7 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; -import com.project.yozmcafe.domain.member.Member; -import com.project.yozmcafe.domain.member.MemberRepository; -import com.project.yozmcafe.exception.TokenException; -import com.project.yozmcafe.service.auth.JwtTokenProvider; +import static com.project.yozmcafe.exception.ErrorCode.TOKEN_NOT_EXIST; @Component public class LoginArgumentResolver implements HandlerMethodArgumentResolver { @@ -22,26 +18,23 @@ public class LoginArgumentResolver implements HandlerMethodArgumentResolver { private static final String BEARER = "Bearer "; private final JwtTokenProvider jwtTokenProvider; - private final MemberRepository memberRepository; - public LoginArgumentResolver(final JwtTokenProvider jwtTokenProvider, final MemberRepository memberRepository) { + public LoginArgumentResolver(final JwtTokenProvider jwtTokenProvider) { this.jwtTokenProvider = jwtTokenProvider; - this.memberRepository = memberRepository; } @Override public boolean supportsParameter(final MethodParameter parameter) { - return parameter.getParameterType().equals(Member.class); + return parameter.hasParameterAnnotation(LoginUser.class); } @Override - public Object resolveArgument(final MethodParameter parameter, final ModelAndViewContainer mavContainer, + public String resolveArgument(final MethodParameter parameter, final ModelAndViewContainer mavContainer, final NativeWebRequest webRequest, final WebDataBinderFactory binderFactory) { final String token = parseTokenFrom(webRequest); jwtTokenProvider.validate(token); - return memberRepository.findById(jwtTokenProvider.getMemberId(token)) - .orElseThrow(() -> new TokenException(INVALID_TOKEN)); + return jwtTokenProvider.getMemberId(token); } private String parseTokenFrom(final NativeWebRequest webRequest) { @@ -52,5 +45,4 @@ private String parseTokenFrom(final NativeWebRequest webRequest) { return value.replace(BEARER, ""); } - } diff --git a/server/src/main/java/com/project/yozmcafe/controller/LoginUser.java b/server/src/main/java/com/project/yozmcafe/controller/LoginUser.java new file mode 100644 index 00000000..7dd26f9f --- /dev/null +++ b/server/src/main/java/com/project/yozmcafe/controller/LoginUser.java @@ -0,0 +1,11 @@ +package com.project.yozmcafe.controller; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +public @interface LoginUser { +} diff --git a/server/src/main/java/com/project/yozmcafe/domain/cafe/Images.java b/server/src/main/java/com/project/yozmcafe/domain/cafe/Images.java index 0b901af3..9efe28a0 100644 --- a/server/src/main/java/com/project/yozmcafe/domain/cafe/Images.java +++ b/server/src/main/java/com/project/yozmcafe/domain/cafe/Images.java @@ -6,6 +6,9 @@ import jakarta.persistence.ElementCollection; import jakarta.persistence.Embeddable; +import java.util.ArrayList; +import java.util.List; + @Embeddable public class Images { @@ -23,7 +26,7 @@ public Images(List urls) { } public List getUrls() { - return urls; + return new ArrayList<>(urls); } public String getRepresentativeImage() { diff --git a/server/src/main/java/com/project/yozmcafe/service/CafeService.java b/server/src/main/java/com/project/yozmcafe/service/CafeService.java index 81236b3a..61efd700 100644 --- a/server/src/main/java/com/project/yozmcafe/service/CafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/CafeService.java @@ -21,11 +21,14 @@ public class CafeService { private final CafeRepository cafeRepository; + private final MemberService memberService; private final UnViewedCafeService unViewedCafeService; private final CafeRankGenerator cafeRankGenerator; - public CafeService(final CafeRepository cafeRepository, final UnViewedCafeService unViewedCafeService, final CafeRankGenerator cafeRankGenerator) { + public CafeService(final CafeRepository cafeRepository, final MemberService memberService, + final UnViewedCafeService unViewedCafeService, final CafeRankGenerator cafeRankGenerator) { this.cafeRepository = cafeRepository; + this.memberService = memberService; this.unViewedCafeService = unViewedCafeService; this.cafeRankGenerator = cafeRankGenerator; } @@ -50,7 +53,8 @@ public List getCafesOrderByLikeCount(final Pageable pageable) } @Transactional - public List getCafesForLoginMember(final Member member, final int size) { + public List getCafesForLoginMember(final String memberId, final int size) { + final Member member = memberService.findMemberByIdOrElseThrow(memberId); final List cafes = member.getNextUnViewedCafes(size); unViewedCafeService.refillWhenUnViewedCafesSizeUnderTwenty(member); diff --git a/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java b/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java index 6b1f3764..132db1f2 100644 --- a/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java @@ -6,6 +6,8 @@ import com.project.yozmcafe.domain.cafe.CafeRepository; import com.project.yozmcafe.domain.cafe.LikedCafe; import com.project.yozmcafe.domain.member.Member; +import com.project.yozmcafe.exception.BadRequestException; +import com.project.yozmcafe.exception.ErrorCode; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -19,8 +21,7 @@ public class LikedCafeService { private final CafeRepository cafeRepository; private final MemberService memberService; - public LikedCafeService(final CafeRepository cafeRepository, - final MemberService memberService) { + public LikedCafeService(final CafeRepository cafeRepository, final MemberService memberService) { this.cafeRepository = cafeRepository; this.memberService = memberService; } @@ -54,9 +55,11 @@ public List findLikedCafesByMemberId(final String memberId) { } @Transactional - public void updateLike(final Member member, final long cafeId, final boolean isLiked) { + public void updateLike(final String memberId, final long cafeId, final boolean isLiked) { + final Member member = memberService.findMemberByIdOrElseThrow(memberId); + final Cafe cafe = cafeRepository.findById(cafeId) - .orElseThrow(() -> new IllegalArgumentException("해당하는 카페가 존재하지 않습니다.")); + .orElseThrow(() -> new BadRequestException(ErrorCode.NOT_EXISTED_CAFE)); member.updateLikedCafesBy(cafe, isLiked); } diff --git a/server/src/main/resources/application.properties b/server/src/main/resources/application.properties index e484a103..745746e3 100644 --- a/server/src/main/resources/application.properties +++ b/server/src/main/resources/application.properties @@ -28,3 +28,5 @@ spring.flyway.baseline-version=20230901153300 spring.flyway.baseline-on-migrate=true spring.flyway.out-of-order=true spring.jpa.properties.hibernate.default_batch_fetch_size=1000 +spring.jpa.open-in-view=false + diff --git a/server/src/test/java/com/project/yozmcafe/controller/CafeControllerTest.java b/server/src/test/java/com/project/yozmcafe/controller/CafeControllerTest.java index e16bb8a3..e6d8a998 100644 --- a/server/src/test/java/com/project/yozmcafe/controller/CafeControllerTest.java +++ b/server/src/test/java/com/project/yozmcafe/controller/CafeControllerTest.java @@ -367,7 +367,6 @@ private ResponseFieldsSnippet getCafeResponseFields() { ); } - private ResponseFieldsSnippet getCafeRankResponseFields() { return responseFields( fieldWithPath("[].rank").description("카페 순위"), diff --git a/server/src/test/java/com/project/yozmcafe/service/CafeServiceTest.java b/server/src/test/java/com/project/yozmcafe/service/CafeServiceTest.java index 078d202a..06336f1d 100644 --- a/server/src/test/java/com/project/yozmcafe/service/CafeServiceTest.java +++ b/server/src/test/java/com/project/yozmcafe/service/CafeServiceTest.java @@ -37,14 +37,15 @@ class CafeServiceTest extends BaseTest { @DisplayName("로그인 된 사용자의 안본 카페 목록을 조회한다.") void getCafesForLoginMember() { //given - final Member member = memberRepository.save(new Member("1", "폴로", "img")); + final Member member = new Member("1", "폴로", "img"); final Cafe cafe1 = cafeRepository.save(Fixture.getCafe("카페1", "주소1", 10)); final Cafe cafe2 = cafeRepository.save(Fixture.getCafe("카페2", "주소2", 11)); member.addUnViewedCafes(Arrays.asList(cafe1, cafe2)); member.updateLikedCafesBy(cafe1, true); + memberRepository.save(member); //when - final List result = cafeService.getCafesForLoginMember(member, 5); + final List result = cafeService.getCafesForLoginMember(member.getId(), 5); //then assertAll( @@ -163,7 +164,6 @@ void getCafesOrderByLikeCountWhenPageOutFail() { void getCafeById() { //given final Cafe savedCafe1 = cafeRepository.save(Fixture.getCafe("카페1", "주소1", 10)); - final Cafe savedCafe2 = cafeRepository.save(Fixture.getCafe("카페2", "주소2", 11)); //when final CafeResponse result = cafeService.getCafeById(savedCafe1.getId()); diff --git a/server/src/test/java/com/project/yozmcafe/service/LikedCafeServiceTest.java b/server/src/test/java/com/project/yozmcafe/service/LikedCafeServiceTest.java index 482eeafc..e275525d 100644 --- a/server/src/test/java/com/project/yozmcafe/service/LikedCafeServiceTest.java +++ b/server/src/test/java/com/project/yozmcafe/service/LikedCafeServiceTest.java @@ -127,7 +127,7 @@ void updateLikeAdd() { final Cafe cafe = cafeRepository.save(Fixture.getCafe("카페", "카페주소", 10)); //when - likedCafeService.updateLike(member, cafe.getId(), true); + likedCafeService.updateLike(member.getId(), cafe.getId(), true); final Member updatedMember = memberRepository.findById(member.getId()).get(); //then @@ -148,7 +148,7 @@ void updateLikeRemove() { member.updateLikedCafesBy(cafe, true); //when - likedCafeService.updateLike(member, cafe.getId(), false); + likedCafeService.updateLike(member.getId(), cafe.getId(), false); final Member updatedMember = memberRepository.findById(member.getId()).get(); //then @@ -169,7 +169,7 @@ void updateLikeAlreadySatisfied() { member.updateLikedCafesBy(cafe, true); //when - likedCafeService.updateLike(member, cafe.getId(), true); + likedCafeService.updateLike(member.getId(), cafe.getId(), true); final Member updatedMember = memberRepository.findById(member.getId()).get(); //then @@ -189,8 +189,8 @@ void updateLikeFail() { //when & then final long cafeId = cafe.getId(); - assertThatThrownBy(() -> likedCafeService.updateLike(member, cafeId, true)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("해당하는 카페가 존재하지 않습니다."); + assertThatThrownBy(() -> likedCafeService.updateLike(member.getId(), cafeId, true)) + .isInstanceOf(BadRequestException.class) + .hasMessage("카페가 존재하지 않습니다."); } }