Skip to content

Commit

Permalink
[Feature/BE] 회원을 검색할 때 대표장비가 아닌 다른 제품들도 나오는 오류 수정 (#330)
Browse files Browse the repository at this point in the history
* refactor: 대표장비 업데이트 로직 수정

Co-authored-by: hamcheeseburger <[email protected]>
Co-authored-by: yangdongjue5510 <[email protected]>

* [BE] 회원을 검색할 때 대표장비가 아닌 다른 제품들도 나오는 오류 수정 (#329)

refactor: 회원 검색 시 대표 장비와 함께 조회되도록 수정

* [BE] 회원 검색 시 대표장비만 조회되도록 수정 코드 리뷰 피드백 반영 (#356)

refactor: 코드 리뷰 피드백 반영

Co-authored-by: yh20studio(tiki) <[email protected]>
Co-authored-by: hamcheeseburger <[email protected]>
  • Loading branch information
3 people authored Aug 2, 2022
1 parent aaceaf6 commit 00f579f
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.woowacourse.f12.domain.member.MemberRepository;
import com.woowacourse.f12.dto.request.inventoryproduct.ProfileProductRequest;
import com.woowacourse.f12.dto.response.inventoryproduct.InventoryProductsResponse;
import com.woowacourse.f12.exception.internalserver.SqlUpdateException;
import com.woowacourse.f12.exception.badrequest.DuplicatedProfileProductCategoryException;
import com.woowacourse.f12.exception.badrequest.InvalidProfileProductCategoryException;
import com.woowacourse.f12.exception.internalserver.SqlUpdateException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.woowacourse.f12.domain.member;

import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.product.Product;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
Expand Down Expand Up @@ -82,6 +84,13 @@ public boolean isRegisterCompleted() {
return Objects.nonNull(this.careerLevel) && Objects.nonNull(this.jobType);
}

public List<Product> getProfileProducts() {
return this.inventoryProducts.stream()
.filter(InventoryProduct::isSelected)
.map(InventoryProduct::getProduct)
.collect(Collectors.toList());
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public MemberWithProfileProductResponse(final Long id, final String gitHubId, fi
}

public static MemberWithProfileProductResponse from(final Member member) {
final List<ProductResponse> profileProducts = member.getInventoryProducts()
final List<ProductResponse> profileProducts = member.getProfileProducts()
.stream()
.map(inventoryProduct -> ProductResponse.from(inventoryProduct.getProduct()))
.map(ProductResponse::from)
.collect(Collectors.toList());
return new MemberWithProfileProductResponse(member.getId(), member.getGitHubId(), member.getName(),
member.getImageUrl(), CareerLevelConstant.from(member.getCareerLevel()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,42 +112,6 @@ class InventoryProductAcceptanceTest extends AcceptanceTest {
);
}

@Test
void 대표_장비가_있는_상태에서_모두_해제하고_요청된_대표_장비만_등록한다() {
// given
Product product1 = 제품을_저장한다(KEYBOARD_1.생성());
Product product2 = 제품을_저장한다(KEYBOARD_2.생성());
LoginResponse loginResponse = 로그인을_한다(CORINNE_GITHUB.getCode());
String token = loginResponse.getToken();
Member member = 응답을_회원으로_변환한다(loginResponse.getMember());

InventoryProduct selectedInventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(member, product1);
InventoryProduct unselectedInventoryProduct = UNSELECTED_INVENTORY_PRODUCT.생성(member, product2);
인벤토리에_장비를_한다(selectedInventoryProduct);
InventoryProduct savedUnselectedInventoryProduct = 인벤토리에_장비를_한다(unselectedInventoryProduct);

// when
ExtractableResponse<Response> profileProductResponse = 로그인된_상태로_PATCH_요청을_보낸다(
"api/v1/members/inventoryProducts", token,
new ProfileProductRequest(List.of(savedUnselectedInventoryProduct.getId())));

List<InventoryProductResponse> inventoryProductResponses = 로그인된_상태로_GET_요청을_보낸다(
"/api/v1/members/inventoryProducts",
token)
.as(InventoryProductsResponse.class).getItems();

// then
assertAll(
() -> assertThat(profileProductResponse.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(inventoryProductResponses.stream()
.filter(InventoryProductResponse::isSelected)
.findFirst()
.get()).usingRecursiveComparison()
.ignoringFields("selected")
.isEqualTo(InventoryProductResponse.from(savedUnselectedInventoryProduct))
);
}

@Test
void 등록된_장비_목록을_대표_장비를_포함해서_조회한다() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import static com.woowacourse.f12.support.GitHubProfileFixtures.CORINNE_GITHUB;
import static com.woowacourse.f12.support.GitHubProfileFixtures.MINCHO_GITHUB;
import static com.woowacourse.f12.support.InventoryProductFixtures.SELECTED_INVENTORY_PRODUCT;
import static com.woowacourse.f12.support.InventoryProductFixtures.대표_장비_업데이트_한다;
import static com.woowacourse.f12.support.MemberFixtures.CORINNE;
import static com.woowacourse.f12.support.ProductFixture.KEYBOARD_1;
import static com.woowacourse.f12.support.ProductFixture.MOUSE_1;
import static com.woowacourse.f12.support.ReviewFixtures.REVIEW_RATING_4;
import static com.woowacourse.f12.support.ReviewFixtures.REVIEW_RATING_5;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
Expand All @@ -28,8 +31,10 @@
import com.woowacourse.f12.dto.response.member.MemberPageResponse;
import com.woowacourse.f12.dto.response.member.MemberResponse;
import com.woowacourse.f12.dto.response.member.MemberWithProfileProductResponse;
import com.woowacourse.f12.dto.response.product.ProductResponse;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -192,9 +197,10 @@ public class MemberAcceptanceTest extends AcceptanceTest {
}

@Test
void 회원정보를_키워드와_옵션으로_검색하여_조회한다() {
void 회원정보를_키워드와_옵션으로_검색하여_대표_장비와_함께_조회한다() {
// given
Product product = 제품을_저장한다(KEYBOARD_1.생성());
Product keyboard = 제품을_저장한다(KEYBOARD_1.생성());
Product mouse = 제품을_저장한다(MOUSE_1.생성());

MemberRequest memberRequest = new MemberRequest(SENIOR_CONSTANT, BACKEND_CONSTANT);
LoginResponse firstLoginResponse = 로그인을_한다(MINCHO_GITHUB.getCode());
Expand All @@ -205,26 +211,31 @@ public class MemberAcceptanceTest extends AcceptanceTest {
Long memberId = secondLoginResponse.getMember().getId();
로그인된_상태로_PATCH_요청을_보낸다("/api/v1/members/me", token, memberRequest);

REVIEW_RATING_5.작성_요청을_보낸다(product.getId(), token);
REVIEW_RATING_5.작성_요청을_보낸다(keyboard.getId(), token);
REVIEW_RATING_4.작성_요청을_보낸다(mouse.getId(), token);
대표_장비_업데이트_한다(List.of(keyboard), token);

// when
ExtractableResponse<Response> response = GET_요청을_보낸다(
"/api/v1/members?page=0&size=2&query=cheese&careerLevel=senior&jobType=backend");

// then
MemberPageResponse memberPageResponse = response.as(MemberPageResponse.class);
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(1L, CORINNE.생성(memberId), product);
InventoryProduct inventoryProduct = SELECTED_INVENTORY_PRODUCT.생성(1L, CORINNE.생성(memberId), keyboard);
Member member = CORINNE.대표장비를_해서_생성(memberId, inventoryProduct);
MemberWithProfileProductResponse memberWithProfileProductResponse = MemberWithProfileProductResponse.from(
member);
assertAll(
() -> assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(memberPageResponse.isHasNext()).isFalse(),
() -> assertThat(
memberPageResponse.getItems()).usingRecursiveFieldByFieldElementComparatorIgnoringFields(
"profileProducts")
() -> assertThat(memberPageResponse.getItems())
.usingRecursiveFieldByFieldElementComparatorIgnoringFields("profileProducts")
.hasSize(1)
.containsOnly(memberWithProfileProductResponse),
() -> assertThat(memberPageResponse.getItems().get(0).getProfileProducts())
.usingRecursiveFieldByFieldElementComparatorIgnoringFields("reviewCount", "rating")
.hasSize(1)
.containsOnly(memberWithProfileProductResponse)
.containsOnly(ProductResponse.from(keyboard))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.woowacourse.f12.domain.member;

import static org.assertj.core.api.Assertions.assertThat;

import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.product.Category;
import com.woowacourse.f12.domain.product.Product;
import java.util.List;
import org.junit.jupiter.api.Test;

class MemberTest {

@Test
void getProfileProducts() {
// given
Product keyboard = Product.builder()
.category(Category.KEYBOARD)
.imageUrl("keyboardImage")
.name("keyboard")
.build();
Product mouse = Product.builder()
.category(Category.MOUSE)
.imageUrl("mouseImage")
.name("mouse")
.build();
InventoryProduct selectedInventoryProduct = InventoryProduct.builder()
.product(keyboard)
.selected(true)
.build();
InventoryProduct unselectedInventoryProduct = InventoryProduct.builder()
.product(mouse)
.selected(false)
.build();
Member member = Member.builder()
.careerLevel(CareerLevel.JUNIOR)
.jobType(JobType.BACKEND)
.gitHubId("githubId")
.imageUrl("imageUrl")
.name("klay")
.inventoryProducts(List.of(selectedInventoryProduct, unselectedInventoryProduct))
.build();

// when
List<Product> profileProducts = member.getProfileProducts();

// then
assertThat(profileProducts)
.containsOnly(selectedInventoryProduct.getProduct());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.woowacourse.f12.support;

import com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil;
import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.product.Product;
import com.woowacourse.f12.dto.request.inventoryproduct.ProfileProductRequest;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import java.util.List;
import java.util.stream.Collectors;

public enum InventoryProductFixtures {

Expand All @@ -28,4 +34,13 @@ public enum InventoryProductFixtures {
.product(product)
.build();
}

public static ExtractableResponse<Response> 대표_장비_업데이트_한다(final List<Product> selectedProducts,
final String token) {
final List<Long> selectedProductIds = selectedProducts.stream()
.map(Product::getId)
.collect(Collectors.toList());
return RestAssuredRequestUtil.로그인된_상태로_PATCH_요청을_보낸다("/api/v1/members/inventoryProducts", token,
new ProfileProductRequest(selectedProductIds));
}
}

0 comments on commit 00f579f

Please sign in to comment.