From d404a3a4843d003c9a57985bda11b4dae459d1c3 Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Tue, 19 Sep 2023 22:08:15 +0900 Subject: [PATCH 1/9] =?UTF-8?q?refactor:=20argumentResolver=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=EA=B0=92=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=BB=A4?= =?UTF-8?q?=EC=8A=A4=ED=85=80=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yozmcafe/controller/CafeController.java | 7 +++--- .../controller/LikedCafeController.java | 7 +++--- .../controller/LoginArgumentResolver.java | 22 ++++++------------- .../yozmcafe/controller/LoginUser.java | 11 ++++++++++ .../project/yozmcafe/service/CafeService.java | 11 ++++++++-- .../yozmcafe/service/LikedCafeService.java | 12 ++++++++-- .../src/main/resources/application.properties | 1 + 7 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 server/src/main/java/com/project/yozmcafe/controller/LoginUser.java 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..0aa6cacc 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,12 +27,12 @@ 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); } - @GetMapping("/guest") + @GetMapping(value = "/guest") public ResponseEntity> getCafesForUnLoggedInMember(@PageableDefault(size = PAGE_SIZE) final Pageable pageable) { List cafeResponses = cafeService.getCafesForUnLoginMember(pageable); 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..062fbe3a 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; @@ -40,11 +39,11 @@ public ResponseEntity> getLikedCafes(@PathVariable("memb return ResponseEntity.ok(likedCafes); } - @PostMapping("/cafes/{cafeId}/likes") - public ResponseEntity updateLikes(final Member member, + @PostMapping(value = "/cafes/{cafeId}/likes") + 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/service/CafeService.java b/server/src/main/java/com/project/yozmcafe/service/CafeService.java index 81236b3a..65c34850 100644 --- a/server/src/main/java/com/project/yozmcafe/service/CafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/CafeService.java @@ -7,6 +7,7 @@ import com.project.yozmcafe.domain.cafe.CafeRepository; import com.project.yozmcafe.domain.cafe.UnViewedCafe; import com.project.yozmcafe.domain.member.Member; +import com.project.yozmcafe.domain.member.MemberRepository; import com.project.yozmcafe.exception.BadRequestException; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -15,17 +16,21 @@ import java.util.List; import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_CAFE; +import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_MEMBER; @Service @Transactional(readOnly = true) public class CafeService { private final CafeRepository cafeRepository; + private final MemberRepository memberRepository; 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 MemberRepository memberRepository, + final UnViewedCafeService unViewedCafeService, final CafeRankGenerator cafeRankGenerator) { this.cafeRepository = cafeRepository; + this.memberRepository = memberRepository; this.unViewedCafeService = unViewedCafeService; this.cafeRankGenerator = cafeRankGenerator; } @@ -50,7 +55,9 @@ 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 = memberRepository.findById(memberId) + .orElseThrow(() -> new BadRequestException(NOT_EXISTED_MEMBER)); 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..adb6a9ed 100644 --- a/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java @@ -6,22 +6,28 @@ 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.domain.member.MemberRepository; +import com.project.yozmcafe.exception.BadRequestException; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_MEMBER; + @Service @Transactional(readOnly = true) public class LikedCafeService { private final CafeRepository cafeRepository; + private final MemberRepository memberRepository; private final MemberService memberService; - public LikedCafeService(final CafeRepository cafeRepository, + public LikedCafeService(final CafeRepository cafeRepository, final MemberRepository memberRepository, final MemberService memberService) { this.cafeRepository = cafeRepository; + this.memberRepository = memberRepository; this.memberService = memberService; } @@ -54,7 +60,9 @@ 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 = memberRepository.findById(memberId) + .orElseThrow(() -> new BadRequestException(NOT_EXISTED_MEMBER)); final Cafe cafe = cafeRepository.findById(cafeId) .orElseThrow(() -> new IllegalArgumentException("해당하는 카페가 존재하지 않습니다.")); diff --git a/server/src/main/resources/application.properties b/server/src/main/resources/application.properties index 4f2abf99..c12dbb2a 100644 --- a/server/src/main/resources/application.properties +++ b/server/src/main/resources/application.properties @@ -29,3 +29,4 @@ 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 From 081941c1904d4e2425232e85bcf5b5dd72d08ed2 Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Tue, 19 Sep 2023 22:08:43 +0900 Subject: [PATCH 2/9] =?UTF-8?q?test:=20test=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/yozmcafe/controller/CafeControllerTest.java | 2 +- .../com/project/yozmcafe/service/CafeServiceTest.java | 6 +++--- .../project/yozmcafe/service/LikedCafeServiceTest.java | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) 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..d39a9e3a 100644 --- a/server/src/test/java/com/project/yozmcafe/controller/CafeControllerTest.java +++ b/server/src/test/java/com/project/yozmcafe/controller/CafeControllerTest.java @@ -16,6 +16,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.restdocs.payload.ResponseFieldsSnippet; import org.springframework.restdocs.request.QueryParametersSnippet; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Objects; @@ -367,7 +368,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..cdd840b9 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,7 +189,7 @@ void updateLikeFail() { //when & then final long cafeId = cafe.getId(); - assertThatThrownBy(() -> likedCafeService.updateLike(member, cafeId, true)) + assertThatThrownBy(() -> likedCafeService.updateLike(member.getId(), cafeId, true)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("해당하는 카페가 존재하지 않습니다."); } From 32e0402ff730dc951b9a89221cdeac279d279791 Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Wed, 20 Sep 2023 14:40:21 +0900 Subject: [PATCH 3/9] =?UTF-8?q?refactor:=20cafe=20urls=20=EA=B0=95?= =?UTF-8?q?=EC=A0=9C=20=EC=B4=88=EA=B8=B0=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/project/yozmcafe/service/CafeService.java | 5 ++++- .../java/com/project/yozmcafe/service/LikedCafeService.java | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) 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 65c34850..2bd2361b 100644 --- a/server/src/main/java/com/project/yozmcafe/service/CafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/CafeService.java @@ -39,7 +39,10 @@ public List getCafesForUnLoginMember(final Pageable pageable) { final List foundCafes = cafeRepository.findSliceBy(pageable).getContent(); return foundCafes.stream() - .map(CafeResponse::fromUnLoggedInUser) + .map(cafe -> { + cafe.getImages().getUrls().size(); + return CafeResponse.fromUnLoggedInUser(cafe); + }) .toList(); } 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 adb6a9ed..97a011cb 100644 --- a/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java @@ -55,7 +55,10 @@ public List findLikedCafesByMemberId(final String memberId) { return likedCafes.stream() .map(LikedCafe::getCafe) - .map(LikedCafeResponse::from) + .map(cafe -> { + cafe.getImages().getUrls().size(); + return LikedCafeResponse.from(cafe); + }) .toList(); } From 4190d07496324c265663b58ad2245356aeb8ef27 Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Wed, 20 Sep 2023 14:44:04 +0900 Subject: [PATCH 4/9] =?UTF-8?q?refactor:=20member=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/yozmcafe/service/CafeService.java | 34 ++++++------------- .../yozmcafe/service/LikedCafeService.java | 7 ++-- 2 files changed, 13 insertions(+), 28 deletions(-) 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 2bd2361b..9436abe0 100644 --- a/server/src/main/java/com/project/yozmcafe/service/CafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/CafeService.java @@ -7,7 +7,6 @@ import com.project.yozmcafe.domain.cafe.CafeRepository; import com.project.yozmcafe.domain.cafe.UnViewedCafe; import com.project.yozmcafe.domain.member.Member; -import com.project.yozmcafe.domain.member.MemberRepository; import com.project.yozmcafe.exception.BadRequestException; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -16,21 +15,19 @@ import java.util.List; import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_CAFE; -import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_MEMBER; @Service @Transactional(readOnly = true) public class CafeService { private final CafeRepository cafeRepository; - private final MemberRepository memberRepository; + private final MemberService memberService; private final UnViewedCafeService unViewedCafeService; private final CafeRankGenerator cafeRankGenerator; - public CafeService(final CafeRepository cafeRepository, final MemberRepository memberRepository, - final UnViewedCafeService unViewedCafeService, final CafeRankGenerator cafeRankGenerator) { + public CafeService(final CafeRepository cafeRepository, final MemberService memberService, final UnViewedCafeService unViewedCafeService, final CafeRankGenerator cafeRankGenerator) { this.cafeRepository = cafeRepository; - this.memberRepository = memberRepository; + this.memberService = memberService; this.unViewedCafeService = unViewedCafeService; this.cafeRankGenerator = cafeRankGenerator; } @@ -38,12 +35,10 @@ public CafeService(final CafeRepository cafeRepository, final MemberRepository m public List getCafesForUnLoginMember(final Pageable pageable) { final List foundCafes = cafeRepository.findSliceBy(pageable).getContent(); - return foundCafes.stream() - .map(cafe -> { - cafe.getImages().getUrls().size(); - return CafeResponse.fromUnLoggedInUser(cafe); - }) - .toList(); + return foundCafes.stream().map(cafe -> { + cafe.getImages().getUrls().size(); + return CafeResponse.fromUnLoggedInUser(cafe); + }).toList(); } public List getCafesOrderByLikeCount(final Pageable pageable) { @@ -52,27 +47,20 @@ public List getCafesOrderByLikeCount(final Pageable pageable) final List ids = cafeRepository.findCafeIdsOrderByLikeCount(pageable); final List foundCafes = cafeRepository.findCafesByIdsOrderByLikeCount(ids); - return foundCafes.stream() - .map(cafe -> CafeRankResponse.of(cafeRankGenerator.makeRank(foundCafes.indexOf(cafe), pageable), cafe)) - .toList(); + return foundCafes.stream().map(cafe -> CafeRankResponse.of(cafeRankGenerator.makeRank(foundCafes.indexOf(cafe), pageable), cafe)).toList(); } @Transactional public List getCafesForLoginMember(final String memberId, final int size) { - final Member member = memberRepository.findById(memberId) - .orElseThrow(() -> new BadRequestException(NOT_EXISTED_MEMBER)); + final Member member = memberService.findMemberByIdOrElseThrow(memberId); final List cafes = member.getNextUnViewedCafes(size); unViewedCafeService.refillWhenUnViewedCafesSizeUnderTwenty(member); - return cafes.stream() - .map(UnViewedCafe::getCafe) - .map(cafe -> CafeResponse.fromLoggedInUser(cafe, member.isLike(cafe))) - .toList(); + return cafes.stream().map(UnViewedCafe::getCafe).map(cafe -> CafeResponse.fromLoggedInUser(cafe, member.isLike(cafe))).toList(); } public CafeResponse getCafeById(final long cafeId) { - final Cafe foundCafe = cafeRepository.findById(cafeId) - .orElseThrow(() -> new BadRequestException(NOT_EXISTED_CAFE)); + final Cafe foundCafe = cafeRepository.findById(cafeId).orElseThrow(() -> new BadRequestException(NOT_EXISTED_CAFE)); return CafeResponse.fromUnLoggedInUser(foundCafe); } 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 97a011cb..e1a27697 100644 --- a/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java @@ -7,15 +7,12 @@ import com.project.yozmcafe.domain.cafe.LikedCafe; import com.project.yozmcafe.domain.member.Member; import com.project.yozmcafe.domain.member.MemberRepository; -import com.project.yozmcafe.exception.BadRequestException; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; -import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_MEMBER; - @Service @Transactional(readOnly = true) public class LikedCafeService { @@ -64,8 +61,8 @@ public List findLikedCafesByMemberId(final String memberId) { @Transactional public void updateLike(final String memberId, final long cafeId, final boolean isLiked) { - final Member member = memberRepository.findById(memberId) - .orElseThrow(() -> new BadRequestException(NOT_EXISTED_MEMBER)); + final Member member = memberService.findMemberByIdOrElseThrow(memberId); + final Cafe cafe = cafeRepository.findById(cafeId) .orElseThrow(() -> new IllegalArgumentException("해당하는 카페가 존재하지 않습니다.")); From 39cf6302642089422c88d4aa8ef79fe23c5163b0 Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Wed, 20 Sep 2023 14:47:55 +0900 Subject: [PATCH 5/9] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/project/yozmcafe/controller/CafeController.java | 2 +- .../project/yozmcafe/controller/LikedCafeController.java | 2 +- .../java/com/project/yozmcafe/service/CafeService.java | 8 ++++++-- .../project/yozmcafe/controller/CafeControllerTest.java | 1 - 4 files changed, 8 insertions(+), 5 deletions(-) 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 0aa6cacc..a477f93d 100644 --- a/server/src/main/java/com/project/yozmcafe/controller/CafeController.java +++ b/server/src/main/java/com/project/yozmcafe/controller/CafeController.java @@ -32,7 +32,7 @@ public ResponseEntity> getCafesForLoggedInMember(@LoginUser f return ResponseEntity.ok(cafeResponses); } - @GetMapping(value = "/guest") + @GetMapping("/guest") public ResponseEntity> getCafesForUnLoggedInMember(@PageableDefault(size = PAGE_SIZE) final Pageable pageable) { List cafeResponses = cafeService.getCafesForUnLoginMember(pageable); 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 062fbe3a..d6594220 100644 --- a/server/src/main/java/com/project/yozmcafe/controller/LikedCafeController.java +++ b/server/src/main/java/com/project/yozmcafe/controller/LikedCafeController.java @@ -39,7 +39,7 @@ public ResponseEntity> getLikedCafes(@PathVariable("memb return ResponseEntity.ok(likedCafes); } - @PostMapping(value = "/cafes/{cafeId}/likes") + @PostMapping("/cafes/{cafeId}/likes") public ResponseEntity updateLikes(@LoginUser final String memberId, @PathVariable("cafeId") final long cafeId, @RequestParam("isLiked") final boolean isLiked) { 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 9436abe0..15dfd494 100644 --- a/server/src/main/java/com/project/yozmcafe/service/CafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/CafeService.java @@ -56,11 +56,15 @@ public List getCafesForLoginMember(final String memberId, final in final List cafes = member.getNextUnViewedCafes(size); unViewedCafeService.refillWhenUnViewedCafesSizeUnderTwenty(member); - return cafes.stream().map(UnViewedCafe::getCafe).map(cafe -> CafeResponse.fromLoggedInUser(cafe, member.isLike(cafe))).toList(); + return cafes.stream() + .map(UnViewedCafe::getCafe) + .map(cafe -> CafeResponse.fromLoggedInUser(cafe, member.isLike(cafe))) + .toList(); } public CafeResponse getCafeById(final long cafeId) { - final Cafe foundCafe = cafeRepository.findById(cafeId).orElseThrow(() -> new BadRequestException(NOT_EXISTED_CAFE)); + final Cafe foundCafe = cafeRepository.findById(cafeId) + .orElseThrow(() -> new BadRequestException(NOT_EXISTED_CAFE)); return CafeResponse.fromUnLoggedInUser(foundCafe); } 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 d39a9e3a..e6d8a998 100644 --- a/server/src/test/java/com/project/yozmcafe/controller/CafeControllerTest.java +++ b/server/src/test/java/com/project/yozmcafe/controller/CafeControllerTest.java @@ -16,7 +16,6 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.restdocs.payload.ResponseFieldsSnippet; import org.springframework.restdocs.request.QueryParametersSnippet; -import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Objects; From 486f19625f8c5a2263266b1373e7f211bdbd3d66 Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Wed, 20 Sep 2023 14:40:21 +0900 Subject: [PATCH 6/9] =?UTF-8?q?refactor:=20urls=20=ED=94=84=EB=A1=9D?= =?UTF-8?q?=EC=8B=9C=20=EA=B0=9D=EC=B2=B4=20=EA=B0=95=EC=A0=9C=20=EC=B4=88?= =?UTF-8?q?=EA=B8=B0=ED=99=94=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/project/yozmcafe/domain/cafe/Images.java | 3 ++- .../com/project/yozmcafe/service/CafeService.java | 7 +++---- .../project/yozmcafe/service/LikedCafeService.java | 11 ++--------- 3 files changed, 7 insertions(+), 14 deletions(-) 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 3519b825..f9bba2a4 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 @@ -5,6 +5,7 @@ import jakarta.persistence.Embeddable; import jakarta.persistence.FetchType; +import java.util.ArrayList; import java.util.List; @Embeddable @@ -24,7 +25,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 15dfd494..beda534a 100644 --- a/server/src/main/java/com/project/yozmcafe/service/CafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/CafeService.java @@ -35,10 +35,9 @@ public CafeService(final CafeRepository cafeRepository, final MemberService memb public List getCafesForUnLoginMember(final Pageable pageable) { final List foundCafes = cafeRepository.findSliceBy(pageable).getContent(); - return foundCafes.stream().map(cafe -> { - cafe.getImages().getUrls().size(); - return CafeResponse.fromUnLoggedInUser(cafe); - }).toList(); + return foundCafes.stream() + .map(CafeResponse::fromUnLoggedInUser) + .toList(); } public List getCafesOrderByLikeCount(final Pageable pageable) { 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 e1a27697..b72c1a38 100644 --- a/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java @@ -6,7 +6,6 @@ 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.domain.member.MemberRepository; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,13 +17,10 @@ public class LikedCafeService { private final CafeRepository cafeRepository; - private final MemberRepository memberRepository; private final MemberService memberService; - public LikedCafeService(final CafeRepository cafeRepository, final MemberRepository memberRepository, - final MemberService memberService) { + public LikedCafeService(final CafeRepository cafeRepository, final MemberService memberService) { this.cafeRepository = cafeRepository; - this.memberRepository = memberRepository; this.memberService = memberService; } @@ -52,10 +48,7 @@ public List findLikedCafesByMemberId(final String memberId) { return likedCafes.stream() .map(LikedCafe::getCafe) - .map(cafe -> { - cafe.getImages().getUrls().size(); - return LikedCafeResponse.from(cafe); - }) + .map(LikedCafeResponse::from) .toList(); } From a89e4cd85f1284479cee8aaa959b15d6a58688b3 Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Thu, 21 Sep 2023 21:22:39 +0900 Subject: [PATCH 7/9] =?UTF-8?q?style:=20=EC=BD=94=EB=93=9C=20=ED=8F=AC?= =?UTF-8?q?=EB=A9=A7=ED=8C=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/project/yozmcafe/service/CafeService.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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 beda534a..61efd700 100644 --- a/server/src/main/java/com/project/yozmcafe/service/CafeService.java +++ b/server/src/main/java/com/project/yozmcafe/service/CafeService.java @@ -25,7 +25,8 @@ public class CafeService { private final UnViewedCafeService unViewedCafeService; private final CafeRankGenerator cafeRankGenerator; - public CafeService(final CafeRepository cafeRepository, final MemberService memberService, 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; @@ -46,7 +47,9 @@ public List getCafesOrderByLikeCount(final Pageable pageable) final List ids = cafeRepository.findCafeIdsOrderByLikeCount(pageable); final List foundCafes = cafeRepository.findCafesByIdsOrderByLikeCount(ids); - return foundCafes.stream().map(cafe -> CafeRankResponse.of(cafeRankGenerator.makeRank(foundCafes.indexOf(cafe), pageable), cafe)).toList(); + return foundCafes.stream() + .map(cafe -> CafeRankResponse.of(cafeRankGenerator.makeRank(foundCafes.indexOf(cafe), pageable), cafe)) + .toList(); } @Transactional From e0ea14773a1c8aa9fe8e089425ba7378ed81b74c Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Thu, 21 Sep 2023 21:22:48 +0900 Subject: [PATCH 8/9] =?UTF-8?q?refactor:=20=EC=98=88=EC=99=B8=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/project/yozmcafe/service/LikedCafeService.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 b72c1a38..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; @@ -57,7 +59,7 @@ public void updateLike(final String memberId, final long cafeId, final boolean i 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); } From dc2701dd745c24dee903f49d07138b55309680a5 Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Thu, 21 Sep 2023 21:31:07 +0900 Subject: [PATCH 9/9] =?UTF-8?q?test:=20exception=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=EC=97=90=20=EB=94=B0=EB=A5=B8=20test=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/project/yozmcafe/service/LikedCafeServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 cdd840b9..e275525d 100644 --- a/server/src/test/java/com/project/yozmcafe/service/LikedCafeServiceTest.java +++ b/server/src/test/java/com/project/yozmcafe/service/LikedCafeServiceTest.java @@ -190,7 +190,7 @@ void updateLikeFail() { //when & then final long cafeId = cafe.getId(); assertThatThrownBy(() -> likedCafeService.updateLike(member.getId(), cafeId, true)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("해당하는 카페가 존재하지 않습니다."); + .isInstanceOf(BadRequestException.class) + .hasMessage("카페가 존재하지 않습니다."); } }