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

[BE] 사용자와 인벤토리에 연관관계를 설정 #222

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Long saveReviewAndInventoryProduct(final Long productId, final Long membe
final Keyboard keyboard = keyboardRepository.findById(productId)
.orElseThrow(KeyboardNotFoundException::new);
final Long reviewId = saveReview(reviewRequest, member, keyboard);
saveInventoryProduct(memberId, keyboard);
saveInventoryProduct(member, keyboard);
return reviewId;
}

Expand All @@ -64,12 +64,12 @@ private void validateNotWritten(final Member member, final Keyboard keyboard) {
}
}

private void saveInventoryProduct(final Long memberId, final Keyboard keyboard) {
if (inventoryProductRepository.existsByMemberIdAndKeyboard(memberId, keyboard)) {
private void saveInventoryProduct(final Member member, final Keyboard keyboard) {
if (inventoryProductRepository.existsByMemberAndKeyboard(member, keyboard)) {
return;
}
final InventoryProduct inventoryProduct = InventoryProduct.builder()
.memberId(memberId)
.member(member)
.keyboard(keyboard)
.build();
inventoryProductRepository.save(inventoryProduct);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.woowacourse.f12.domain.inventoryproduct;

import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.product.Keyboard;
import java.util.Objects;
import javax.persistence.Column;
Expand All @@ -26,8 +27,9 @@ public class InventoryProduct {
@Column(name = "selected")
private boolean selected;

@Column(name = "member_id")
private Long memberId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "keyboard_id")
Expand All @@ -37,10 +39,10 @@ protected InventoryProduct() {
}

@Builder
private InventoryProduct(final Long id, final boolean selected, final Long memberId, final Keyboard keyboard) {
private InventoryProduct(final Long id, final boolean selected, final Member member, final Keyboard keyboard) {
this.id = id;
this.selected = selected;
this.memberId = memberId;
this.member = member;
this.keyboard = keyboard;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.woowacourse.f12.domain.inventoryproduct;

import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.product.Keyboard;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -8,5 +9,5 @@ public interface InventoryProductRepository extends JpaRepository<InventoryProdu

List<InventoryProduct> findByMemberId(Long memberId);

boolean existsByMemberIdAndKeyboard(Long memberId, Keyboard keyboard);
boolean existsByMemberAndKeyboard(Member member, Keyboard keyboard);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.woowacourse.f12.domain.member;

import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -9,6 +12,7 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -41,6 +45,9 @@ public class Member {
@Enumerated(EnumType.STRING)
private JobType jobType;

@OneToMany(mappedBy = "member")
private List<InventoryProduct> inventoryProducts = new ArrayList<>();

protected Member() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.inventoryproduct.InventoryProductRepository;
import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.product.Keyboard;
import com.woowacourse.f12.domain.product.KeyboardRepository;
import com.woowacourse.f12.dto.request.inventoryproduct.ProfileProductRequest;
Expand Down Expand Up @@ -58,9 +59,8 @@ class InventoryProductAcceptanceTest extends AcceptanceTest {
Keyboard keyboard = 키보드를_저장한다(KEYBOARD_1.생성());
LoginResponse loginResponse = 로그인을_한다("1");
String token = loginResponse.getToken();
Long memberId = loginResponse.getMember().getId();

InventoryProduct inventoryProduct = UNSELECTED_INVENTORY_PRODUCT.생성(memberId, keyboard);
Member member = loginResponse.getMember().toMember();
InventoryProduct inventoryProduct = UNSELECTED_INVENTORY_PRODUCT.생성(member, keyboard);
InventoryProduct savedInventoryProduct = 인벤토리에_장비를_추가한다(inventoryProduct);

// when
Expand All @@ -86,11 +86,10 @@ class InventoryProductAcceptanceTest extends AcceptanceTest {
Keyboard keyboard = 키보드를_저장한다(KEYBOARD_1.생성());
LoginResponse response = 로그인을_한다("1");
String token = response.getToken();
Long memberId = response.getMember()
.getId();
InventoryProduct selectedInventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(memberId, keyboard);
Member member = response.getMember().toMember();
InventoryProduct selectedInventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(member, keyboard);
InventoryProduct savedSelectedInventoryProduct = 인벤토리에_장비를_추가한다(selectedInventoryProduct);
InventoryProduct unselectedInventoryProduct = UNSELECTED_INVENTORY_PRODUCT.생성(memberId, keyboard);
InventoryProduct unselectedInventoryProduct = UNSELECTED_INVENTORY_PRODUCT.생성(member, keyboard);
InventoryProduct savedUnselectedInventoryProduct = 인벤토리에_장비를_추가한다(unselectedInventoryProduct);

// when
Expand All @@ -111,17 +110,16 @@ class InventoryProductAcceptanceTest extends AcceptanceTest {
void 다른_회원의_아이디로_등록된_장비를_조회한다() {
// given
LoginResponse response = 로그인을_한다("1");
Long memberId = response.getMember()
.getId();
Member member = response.getMember().toMember();
Keyboard keyboard = 키보드를_저장한다(KEYBOARD_1.생성());
InventoryProduct selectedInventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(memberId, keyboard);
InventoryProduct selectedInventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(member, keyboard);
InventoryProduct savedSelectedInventoryProduct = 인벤토리에_장비를_추가한다(selectedInventoryProduct);
InventoryProduct unselectedInventoryProduct = UNSELECTED_INVENTORY_PRODUCT.생성(memberId, keyboard);
InventoryProduct unselectedInventoryProduct = UNSELECTED_INVENTORY_PRODUCT.생성(member, keyboard);
InventoryProduct savedUnselectedInventoryProduct = 인벤토리에_장비를_추가한다(unselectedInventoryProduct);

// when
ExtractableResponse<Response> profileProductResponse = GET_요청을_보낸다(
"/api/v1/members/" + memberId + "/inventoryProducts");
"/api/v1/members/" + member.getId() + "/inventoryProducts");

// then
assertAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.inventoryproduct.InventoryProductRepository;
import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.member.MemberRepository;
import com.woowacourse.f12.dto.request.inventoryproduct.ProfileProductRequest;
import com.woowacourse.f12.dto.response.inventoryproduct.InventoryProductResponse;
import com.woowacourse.f12.dto.response.inventoryproduct.InventoryProductsResponse;
import com.woowacourse.f12.exception.badrequest.InvalidProfileProductException;
import com.woowacourse.f12.support.MemberFixtures;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
Expand All @@ -41,8 +43,9 @@ class InventoryProductServiceTest {
void 대표_장비를_등록한다() {
// given
ProfileProductRequest profileProductRequest = new ProfileProductRequest(1L, 2L);
InventoryProduct inventoryProduct1 = UNSELECTED_INVENTORY_PRODUCT.생성(1L, 1L, KEYBOARD_1.생성());
InventoryProduct inventoryProduct2 = SELECTED_INVENTORY_PRODUCT.생성(2L, 1L, KEYBOARD_2.생성());
Member member = MemberFixtures.CORINNE.생성(1L);
InventoryProduct inventoryProduct1 = UNSELECTED_INVENTORY_PRODUCT.생성(1L, member, KEYBOARD_1.생성());
InventoryProduct inventoryProduct2 = SELECTED_INVENTORY_PRODUCT.생성(2L, member, KEYBOARD_2.생성());
given(memberRepository.existsById(1L))
.willReturn(true);
given(inventoryProductRepository.findById(1L))
Expand Down Expand Up @@ -80,7 +83,8 @@ class InventoryProductServiceTest {
void 등록된_장비를_멤버_id로_조회한다() {
// given
Long memberId = 1L;
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(1L, memberId, KEYBOARD_1.생성(1L));
Member member = MemberFixtures.CORINNE.생성(memberId);
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(1L, member, KEYBOARD_1.생성(1L));
given(memberRepository.existsById(1L))
.willReturn(true);
given(inventoryProductRepository.findByMemberId(memberId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ class ReviewServiceTest {
Keyboard keyboard = KEYBOARD_1.생성(productId);
Long memberId = 1L;
Member member = CORINNE.생성(memberId);
InventoryProduct inventoryProduct = UNSELECTED_INVENTORY_PRODUCT.생성(memberId, keyboard);
InventoryProduct inventoryProduct = UNSELECTED_INVENTORY_PRODUCT.생성(member, keyboard);
given(memberRepository.findById(1L))
.willReturn(Optional.of(member));
given(keyboardRepository.findById(productId))
.willReturn(Optional.of(keyboard));
given(inventoryProductRepository.existsByMemberIdAndKeyboard(memberId, keyboard))
given(inventoryProductRepository.existsByMemberAndKeyboard(member, keyboard))
.willReturn(false);
given(inventoryProductRepository.save(inventoryProduct))
.willReturn(inventoryProduct);
Expand All @@ -94,7 +94,7 @@ class ReviewServiceTest {
() -> verify(keyboardRepository).findById(productId),
() -> verify(memberRepository).findById(memberId),
() -> verify(reviewRepository).save(any(Review.class)),
() -> verify(inventoryProductRepository).existsByMemberIdAndKeyboard(memberId, keyboard),
() -> verify(inventoryProductRepository).existsByMemberAndKeyboard(member, keyboard),
() -> verify(inventoryProductRepository).save(inventoryProduct)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import com.woowacourse.f12.application.inventoryproduct.InventoryProductService;
import com.woowacourse.f12.documentation.Documentation;
import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.dto.request.inventoryproduct.ProfileProductRequest;
import com.woowacourse.f12.dto.response.inventoryproduct.InventoryProductsResponse;
import com.woowacourse.f12.presentation.inventoryproduct.InventoryProductController;
import com.woowacourse.f12.support.MemberFixtures;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -74,7 +76,8 @@ class InventoryProductDocumentation extends Documentation {
void 멤버_id_로_인벤토리_상품_조회하는_API_문서화() throws Exception {
// given
Long memberId = 1L;
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(1L, memberId, KEYBOARD_1.생성(1L));
Member member = MemberFixtures.CORINNE.생성(memberId);
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(1L, member, KEYBOARD_1.생성(1L));
String authorizationHeader = "Bearer Token";
given(jwtProvider.validateToken(authorizationHeader))
.willReturn(true);
Expand All @@ -99,7 +102,8 @@ class InventoryProductDocumentation extends Documentation {
void 다른_멤버_id_로_인벤토리_상품_조회하는_API_문서화() throws Exception {
// given
Long memberId = 1L;
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(1L, memberId, KEYBOARD_1.생성(1L));
Member member = MemberFixtures.CORINNE.생성(memberId);
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(1L, member, KEYBOARD_1.생성(1L));
given(inventoryProductService.findByMemberId(memberId))
.willReturn(InventoryProductsResponse.from(List.of(inventoryProduct)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import static com.woowacourse.f12.support.InventoryProductFixtures.SELECTED_INVENTORY_PRODUCT;
import static com.woowacourse.f12.support.KeyboardFixtures.KEYBOARD_1;
import static com.woowacourse.f12.support.KeyboardFixtures.KEYBOARD_2;
import static com.woowacourse.f12.support.MemberFixtures.CORINNE;
import static com.woowacourse.f12.support.MemberFixtures.MINCHO;
import static org.assertj.core.api.Assertions.assertThat;

import com.woowacourse.f12.config.JpaConfig;
import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.member.MemberRepository;
import com.woowacourse.f12.domain.product.Keyboard;
import com.woowacourse.f12.domain.product.KeyboardRepository;
import java.util.List;
Expand All @@ -24,19 +28,22 @@ class InventoryProductRepositoryTest {
@Autowired
private KeyboardRepository keyboardRepository;

@Autowired
private MemberRepository memberRepository;

@Test
void 멤버_아이디로_인벤토리_상품_목록을_조회한다() {
// given
Long myMemberId = 1L;
Long otherMemberId = 2L;
Keyboard keyboard1 = 키보드를_저장한다(KEYBOARD_1.생성(1L));
Keyboard keyboard2 = 키보드를_저장한다(KEYBOARD_2.생성(2L));
InventoryProduct inventoryProduct1 = SELECTED_INVENTORY_PRODUCT.생성(myMemberId, keyboard1);
InventoryProduct inventoryProduct2 = SELECTED_INVENTORY_PRODUCT.생성(otherMemberId, keyboard2);
Member myMember = 회원을_저장한다(CORINNE.생성());
Member otherMember = 회원을_저장한다(MINCHO.생성());
Keyboard keyboard1 = 키보드를_저장한다(KEYBOARD_1.생성());
Keyboard keyboard2 = 키보드를_저장한다(KEYBOARD_2.생성());
InventoryProduct inventoryProduct1 = SELECTED_INVENTORY_PRODUCT.생성(myMember, keyboard1);
InventoryProduct inventoryProduct2 = SELECTED_INVENTORY_PRODUCT.생성(otherMember, keyboard2);
inventoryProductRepository.saveAll(List.of(inventoryProduct1, inventoryProduct2));

// when
List<InventoryProduct> inventoryProducts = inventoryProductRepository.findByMemberId(myMemberId);
List<InventoryProduct> inventoryProducts = inventoryProductRepository.findByMemberId(myMember.getId());

// then
assertThat(inventoryProducts).containsOnly(inventoryProduct1);
Expand All @@ -45,13 +52,13 @@ class InventoryProductRepositoryTest {
@Test
void 멤버_아이디와_상품으로_인벤토리_상품_목록을_조회한다() {
// given
Long memberId = 1L;
Keyboard keyboard = 키보드를_저장한다(KEYBOARD_1.생성(1L));
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(memberId, keyboard);
Member member = 회원을_저장한다(CORINNE.생성());
Keyboard keyboard = 키보드를_저장한다(KEYBOARD_1.생성());
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(member, keyboard);
inventoryProductRepository.save(inventoryProduct);

// when
boolean actual = inventoryProductRepository.existsByMemberIdAndKeyboard(memberId, keyboard);
boolean actual = inventoryProductRepository.existsByMemberAndKeyboard(member, keyboard);

// then
assertThat(actual).isTrue();
Expand All @@ -60,4 +67,8 @@ class InventoryProductRepositoryTest {
private Keyboard 키보드를_저장한다(Keyboard keyboard) {
return keyboardRepository.save(keyboard);
}

private Member 회원을_저장한다(Member member) {
return memberRepository.save(member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class KeyboardRepositoryTest {
void 키보드를_단일_조회_한다() {
// given
Keyboard keyboard = 키보드_저장(KEYBOARD_1.생성());
Member member = memberRepository.save(CORINNE.생성(1L));
Member member = memberRepository.save(CORINNE.생성());
리뷰_저장(REVIEW_RATING_4.작성(keyboard, member));
리뷰_저장(REVIEW_RATING_5.작성(keyboard, member));
entityManager.flush();
Expand Down Expand Up @@ -86,7 +86,7 @@ class KeyboardRepositoryTest {
// given
Keyboard keyboard1 = 키보드_저장(KEYBOARD_1.생성());
Keyboard keyboard2 = 키보드_저장(KEYBOARD_2.생성());
Member member = memberRepository.save(CORINNE.생성(1L));
Member member = memberRepository.save(CORINNE.생성());

리뷰_저장(REVIEW_RATING_5.작성(keyboard1, member));
리뷰_저장(REVIEW_RATING_5.작성(keyboard2, member));
Expand All @@ -109,7 +109,7 @@ class KeyboardRepositoryTest {
// given
Keyboard keyboard2 = 키보드_저장(KEYBOARD_1.생성());
Keyboard keyboard1 = 키보드_저장(KEYBOARD_2.생성());
Member member = memberRepository.save(CORINNE.생성(1L));
Member member = memberRepository.save(CORINNE.생성());

리뷰_저장(REVIEW_RATING_2.작성(keyboard1, member));
리뷰_저장(REVIEW_RATING_1.작성(keyboard1, member));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ReviewRepositoryTest {
void 특정_제품의_리뷰_목록을_최신순으로_페이징하여_조회한다() {
// given
Keyboard keyboard = keyboardRepository.save(KEYBOARD_1.생성());
Member member = memberRepository.save(CORINNE.생성(1L));
Member member = memberRepository.save(CORINNE.생성());
Pageable pageable = PageRequest.of(0, 1, Sort.by(desc("createdAt")));
리뷰_저장(REVIEW_RATING_5.작성(keyboard, member));
Review review = 리뷰_저장(REVIEW_RATING_5.작성(keyboard, member));
Expand All @@ -66,7 +66,7 @@ class ReviewRepositoryTest {
void 특정_제품의_리뷰_목록을_평점순으로_페이징하여_조회한다() {
// given
Keyboard keyboard = keyboardRepository.save(KEYBOARD_1.생성());
Member member = memberRepository.save(CORINNE.생성(1L));
Member member = memberRepository.save(CORINNE.생성());
Pageable pageable = PageRequest.of(0, 1, Sort.by(desc("rating")));
Review review = 리뷰_저장(REVIEW_RATING_5.작성(keyboard, member));
리뷰_저장(REVIEW_RATING_4.작성(keyboard, member));
Expand All @@ -88,7 +88,7 @@ class ReviewRepositoryTest {
// given
Keyboard keyboard1 = keyboardRepository.save(KEYBOARD_1.생성());
Keyboard keyboard2 = keyboardRepository.save(KEYBOARD_2.생성());
Member member = memberRepository.save(CORINNE.생성(1L));
Member member = memberRepository.save(CORINNE.생성());
Pageable pageable = PageRequest.of(0, 1, Sort.by(desc("createdAt")));
리뷰_저장(REVIEW_RATING_5.작성(keyboard1, member));
Review review = 리뷰_저장(REVIEW_RATING_5.작성(keyboard2, member));
Expand Down
Loading