Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSIV false #461

Merged
merged 12 commits into from
Sep 26, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,8 +27,8 @@ public CafeController(final CafeService cafeService) {
}

@GetMapping
public ResponseEntity<List<CafeResponse>> getCafesForLoggedInMember(final Member member) {
List<CafeResponse> cafeResponses = cafeService.getCafesForLoginMember(member, PAGE_SIZE);
public ResponseEntity<List<CafeResponse>> getCafesForLoggedInMember(@LoginUser final String memberId) {
List<CafeResponse> cafeResponses = cafeService.getCafesForLoginMember(memberId, PAGE_SIZE);
return ResponseEntity.ok(cafeResponses);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,10 +40,10 @@ public ResponseEntity<List<LikedCafeResponse>> getLikedCafes(@PathVariable("memb
}

@PostMapping("/cafes/{cafeId}/likes")
public ResponseEntity<Void> updateLikes(final Member member,
public ResponseEntity<Void> 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();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
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;
import org.springframework.web.context.request.NativeWebRequest;
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 {
Expand All @@ -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) {
Expand All @@ -52,5 +45,4 @@ private String parseTokenFrom(final NativeWebRequest webRequest) {

return value.replace(BEARER, "");
}

}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embeddable;

import java.util.ArrayList;
import java.util.List;

@Embeddable
public class Images {

Expand All @@ -23,7 +26,7 @@ public Images(List<String> urls) {
}

public List<String> getUrls() {
return urls;
return new ArrayList<>(urls);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나왔던 여러가지 방법 중 이게 가장 심플하고 좋네요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

더 좋은 방법이 있으면 좋을 것 같기는 해요.

점점 한 문제를 해결하기위해서 일부분만 수정되는 모습이 현재까지 저희가 구현해온 코드를 해치는게 아닐까 라는 의문과 걱정이 들어서요 !

}

public String getRepresentativeImage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -50,7 +53,8 @@ public List<CafeRankResponse> getCafesOrderByLikeCount(final Pageable pageable)
}

@Transactional
public List<CafeResponse> getCafesForLoginMember(final Member member, final int size) {
public List<CafeResponse> getCafesForLoginMember(final String memberId, final int size) {
final Member member = memberService.findMemberByIdOrElseThrow(memberId);
final List<UnViewedCafe> cafes = member.getNextUnViewedCafes(size);
unViewedCafeService.refillWhenUnViewedCafesSizeUnderTwenty(member);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -54,9 +55,11 @@ public List<LikedCafeResponse> 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);
}
Expand Down
2 changes: 2 additions & 0 deletions server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굿 맨


Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ private ResponseFieldsSnippet getCafeResponseFields() {
);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 두 줄 개행 삭제 패치인가요?? 좋아요


private ResponseFieldsSnippet getCafeRankResponseFields() {
return responseFields(
fieldWithPath("[].rank").description("카페 순위"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cascade를 사용해서 잘 개편했군요


//when
final List<CafeResponse> result = cafeService.getCafesForLoginMember(member, 5);
final List<CafeResponse> result = cafeService.getCafesForLoginMember(member.getId(), 5);

//then
assertAll(
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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("카페가 존재하지 않습니다.");
}
}