From 97a78e0fdd82a1a7d5cabbfd9c275aa34f325cfb Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 00:22:40 +0900 Subject: [PATCH 01/10] =?UTF-8?q?feat:=20#67=20=EC=9D=BC=EB=A0=89=20?= =?UTF-8?q?=EA=B8=B0=ED=83=80=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?API=20=ED=95=84=ED=84=B0=20=EC=A1=B0=EA=B1=B4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/InstrumentController.java | 3 +- .../ElectricGuitarFilterConditions.java | 35 +++++++ .../request/InstrumentFilterConditions.java | 13 ++- .../InstrumentRepositoryCustom.java | 3 +- .../InstrumentRepositoryCustomImpl.java | 91 ++++++++++++++++--- .../service/InstrumentQueryService.java | 3 +- .../repository/InstrumentRepositoryTest.java | 24 +++-- .../controller/InstrumentControllerTest.java | 18 ++-- .../service/InstrumentQueryServiceTest.java | 19 +++- 9 files changed, 176 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/ajou/hertz/domain/instrument/dto/request/ElectricGuitarFilterConditions.java diff --git a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java index 3552da7..c94ca54 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java @@ -30,6 +30,7 @@ import com.ajou.hertz.domain.instrument.dto.request.CreateNewBassGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewEffectorRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewElectricGuitarRequest; +import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.dto.response.AcousticAndClassicGuitarResponse; import com.ajou.hertz.domain.instrument.dto.response.AmplifierResponse; @@ -97,7 +98,7 @@ public Page findElectricGuitarsV1( @Parameter( description = "정렬 기준" ) @RequestParam InstrumentSortOption sort, - @ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions + @ParameterObject @Valid @ModelAttribute ElectricGuitarFilterConditions filterConditions ) { return instrumentQueryService .findElectricGuitars(page, size, sort, filterConditions) diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/ElectricGuitarFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/ElectricGuitarFilterConditions.java new file mode 100644 index 0000000..0c66b5f --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/ElectricGuitarFilterConditions.java @@ -0,0 +1,35 @@ +package com.ajou.hertz.domain.instrument.dto.request; + +import com.ajou.hertz.domain.instrument.constant.ElectricGuitarBrand; +import com.ajou.hertz.domain.instrument.constant.ElectricGuitarModel; +import com.ajou.hertz.domain.instrument.constant.GuitarColor; +import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Setter // for @ModelAttribute +@Getter +public class ElectricGuitarFilterConditions extends InstrumentFilterConditions { + + private ElectricGuitarBrand brand; + private ElectricGuitarModel model; + private GuitarColor color; + + public ElectricGuitarFilterConditions( + InstrumentProgressStatus progress, + String sido, + String sgg, + ElectricGuitarBrand brand, + ElectricGuitarModel model, + GuitarColor color + ) { + super(progress, sido, sgg); + this.brand = brand; + this.model = model; + this.color = color; + } +} diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/InstrumentFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/InstrumentFilterConditions.java index e2ad046..88fadaf 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/InstrumentFilterConditions.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/InstrumentFilterConditions.java @@ -2,17 +2,24 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -@AllArgsConstructor(access = AccessLevel.PRIVATE) -@NoArgsConstructor(access = AccessLevel.PRIVATE) +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor(access = AccessLevel.PROTECTED) @Setter // for @ModelAttribute @Getter -public class InstrumentFilterConditions { +public class InstrumentFilterConditions { // TODO: abstract class로 전환 private InstrumentProgressStatus progress; + + @Schema(description = "거래 지역 - 시/도", example = "서울특별시") + private String sido; + + @Schema(description = "거래 지역 - 시/군/구", example = "종로구") + private String sgg; } diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java index 4f984d0..7d45618 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java @@ -3,6 +3,7 @@ import org.springframework.data.domain.Page; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; +import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; import com.ajou.hertz.domain.instrument.entity.Amplifier; @@ -14,7 +15,7 @@ public interface InstrumentRepositoryCustom { Page findElectricGuitars( - int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions + int page, int pageSize, InstrumentSortOption sort, ElectricGuitarFilterConditions filterConditions ); Page findBassGuitars( diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java index 2237f64..bd99a36 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java @@ -1,5 +1,6 @@ package com.ajou.hertz.domain.instrument.repository; +import static com.ajou.hertz.domain.instrument.entity.QElectricGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QInstrument.*; import static com.ajou.hertz.domain.user.entity.QUser.*; @@ -12,8 +13,12 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import com.ajou.hertz.domain.instrument.constant.ElectricGuitarBrand; +import com.ajou.hertz.domain.instrument.constant.ElectricGuitarModel; +import com.ajou.hertz.domain.instrument.constant.GuitarColor; import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; +import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; import com.ajou.hertz.domain.instrument.entity.Amplifier; @@ -26,7 +31,11 @@ import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Predicate; import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.core.types.dsl.ComparableExpression; +import com.querydsl.core.types.dsl.EntityPathBase; +import com.querydsl.core.types.dsl.EnumPath; import com.querydsl.core.types.dsl.PathBuilder; +import com.querydsl.core.types.dsl.StringPath; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; @@ -42,9 +51,31 @@ public Page findElectricGuitars( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + ElectricGuitarFilterConditions filterConditions ) { - return findInstrumentsByClassType(ElectricGuitar.class, page, pageSize, sort, filterConditions); + PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); + + List conditions = + new ArrayList<>(convertElectricGuitarFilterConditionsToPredicates(filterConditions)); + + List content = queryFactory + .selectFrom(electricGuitar) + .join(electricGuitar.seller, user).fetchJoin() + .where(conditions.toArray(Predicate[]::new)) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(electricGuitar))) + .fetch(); + + long totalCount = Optional.ofNullable( + queryFactory.select(electricGuitar.count()) + .from(electricGuitar) + .join(electricGuitar.seller, user) + .where(conditions.toArray(Predicate[]::new)) + .fetchOne() + ).orElse(0L); + + return new PageImpl<>(content, pageable, totalCount); } @Override @@ -108,7 +139,6 @@ private Page findInstrumentsByClassType( List conditions = new ArrayList<>(); conditions.add(instrument.instanceOf(classType)); - conditions.addAll(convertFilterConditions(filterConditions)); List content = queryFactory .selectFrom(instrument) @@ -116,7 +146,7 @@ private Page findInstrumentsByClassType( .where(conditions.toArray(Predicate[]::new)) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) - .orderBy(convertSortToOrderSpecifiers(pageable.getSort())) + .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(instrument))) .fetch() .stream() .map(classType::cast) @@ -133,21 +163,60 @@ private Page findInstrumentsByClassType( return new PageImpl<>(content, pageable, totalCount); } - private OrderSpecifier[] convertSortToOrderSpecifiers(Sort sort) { + private > PathBuilder createPathBuilder(Q qClass) { + return new PathBuilder<>(qClass.getType(), qClass.getMetadata()); + } + + private OrderSpecifier[] convertSortToOrderSpecifiers( + Sort sort, PathBuilder pathBuilder + ) { return sort.stream() .map(order -> new OrderSpecifier<>( order.getDirection().isAscending() ? Order.ASC : Order.DESC, - INSTRUMENT_PATH.getString(order.getProperty()) + pathBuilder.getString(order.getProperty()) )).toArray(OrderSpecifier[]::new); } - private List convertFilterConditions(InstrumentFilterConditions filterConditions) { + private List convertElectricGuitarFilterConditionsToPredicates( + ElectricGuitarFilterConditions filterConditions) { List res = new ArrayList<>(); - res.add(progressStatusFilterCondition(filterConditions.getProgress())); + res.add(applyProgressStatusCondition(filterConditions.getProgress(), electricGuitar.progressStatus)); + res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), electricGuitar.tradeAddress.sido)); + res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), electricGuitar.tradeAddress.sgg)); + res.add(applyElectricGuitarBrandCondition(filterConditions.getBrand())); + res.add(applyElectricGuitarModelCondition(filterConditions.getModel())); + res.add(applyGuitarColorCondition(filterConditions.getColor())); return res; } - private BooleanExpression progressStatusFilterCondition(InstrumentProgressStatus progressStatus) { - return progressStatus != null ? instrument.progressStatus.eq(progressStatus) : null; + private BooleanExpression applyProgressStatusCondition( + InstrumentProgressStatus progressStatus, + EnumPath progressStatusExpression + ) { + return createCondition(progressStatus, progressStatusExpression); + } + + private BooleanExpression applyTradeAddressSidoCondition(String sido, StringPath tradeAddressSidoExpression) { + return createCondition(sido, tradeAddressSidoExpression); + } + + private BooleanExpression applyTradeAddressSggCondition(String sgg, StringPath tradeAddressSggExpression) { + return createCondition(sgg, tradeAddressSggExpression); + } + + private BooleanExpression applyElectricGuitarBrandCondition(ElectricGuitarBrand brand) { + return createCondition(brand, electricGuitar.brand); + } + + private BooleanExpression applyElectricGuitarModelCondition(ElectricGuitarModel model) { + return createCondition(model, electricGuitar.model); + } + + private BooleanExpression applyGuitarColorCondition(GuitarColor color) { + return createCondition(color, electricGuitar.color); + } + + private > BooleanExpression createCondition(T value, ComparableExpression path) { + return value != null ? path.eq(value) : null; } -} +} \ No newline at end of file diff --git a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java index 621256b..0d8ad10 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java @@ -13,6 +13,7 @@ import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; +import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.repository.InstrumentRepository; @@ -35,7 +36,7 @@ public Page findElectricGuitars( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + ElectricGuitarFilterConditions filterConditions ) { return instrumentRepository .findElectricGuitars(page, pageSize, sort, filterConditions) diff --git a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java index 65e6cbf..066245e 100644 --- a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java +++ b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java @@ -36,6 +36,7 @@ import com.ajou.hertz.domain.instrument.constant.GuitarColor; import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; +import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; import com.ajou.hertz.domain.instrument.entity.Amplifier; @@ -72,8 +73,7 @@ public InstrumentRepositoryTest( void 일렉_기타_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; - InstrumentFilterConditions filterConditions = - createInstrumentFilterConditions(InstrumentProgressStatus.SELLING); + ElectricGuitarFilterConditions filterConditions = createElectricGuitarFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createBassGuitar(user), @@ -356,11 +356,19 @@ private InstrumentFilterConditions createInstrumentFilterConditions() throws Exc return instrumentFilterConditionsConstructor.newInstance(); } - private InstrumentFilterConditions createInstrumentFilterConditions(InstrumentProgressStatus progressStatus) throws - Exception { - Constructor instrumentFilterConditionsConstructor = - InstrumentFilterConditions.class.getDeclaredConstructor(InstrumentProgressStatus.class); - instrumentFilterConditionsConstructor.setAccessible(true); - return instrumentFilterConditionsConstructor.newInstance(progressStatus); + private ElectricGuitarFilterConditions createElectricGuitarFilterConditions() throws Exception { + Constructor constructor = ElectricGuitarFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class, + ElectricGuitarBrand.class, ElectricGuitarModel.class, GuitarColor.class + ); + constructor.setAccessible(true); + return constructor.newInstance( + InstrumentProgressStatus.SELLING, + "서울특별시", + null, + ElectricGuitarBrand.FENDER_USA, + ElectricGuitarModel.TELECASTER, + GuitarColor.RED + ); } } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java index 1e784ca..05d7581 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java @@ -63,6 +63,7 @@ import com.ajou.hertz.domain.instrument.dto.request.CreateNewBassGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewEffectorRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewElectricGuitarRequest; +import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.service.InstrumentCommandService; import com.ajou.hertz.domain.instrument.service.InstrumentQueryService; @@ -134,7 +135,7 @@ public InstrumentControllerTest(MockMvc mvc) { createElectricGuitarDto(4L, userId) )); given(instrumentQueryService.findElectricGuitars( - eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class) + eq(page), eq(pageSize), eq(sortOption), any(ElectricGuitarFilterConditions.class) )).willReturn(expectedResult); // when & then @@ -152,7 +153,7 @@ public InstrumentControllerTest(MockMvc mvc) { .andExpect(jsonPath("$.content", hasSize(expectedResult.getNumberOfElements()))); then(instrumentQueryService) .should() - .findElectricGuitars(eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class)); + .findElectricGuitars(eq(page), eq(pageSize), eq(sortOption), any(ElectricGuitarFilterConditions.class)); verifyEveryMocksShouldHaveNoMoreInteractions(); } @@ -1000,11 +1001,14 @@ private CreateNewAudioEquipmentRequest createAudioEquipmentRequest() throws Exce } private InstrumentFilterConditions createInstrumentFilterConditions() throws Exception { - Constructor instrumentFilterConditionsConstructor = - InstrumentFilterConditions.class.getDeclaredConstructor(InstrumentProgressStatus.class); - instrumentFilterConditionsConstructor.setAccessible(true); - return instrumentFilterConditionsConstructor.newInstance( - InstrumentProgressStatus.SELLING + Constructor constructor = InstrumentFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class + ); + constructor.setAccessible(true); + return constructor.newInstance( + InstrumentProgressStatus.SELLING, + "서울특별시", + "마포구" ); } } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java index 477150b..fed8057 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java @@ -45,6 +45,7 @@ import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; +import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; import com.ajou.hertz.domain.instrument.entity.Amplifier; @@ -101,7 +102,7 @@ class InstrumentQueryServiceTest { // given int page = 0; int pageSize = 10; - InstrumentFilterConditions filterConditions = createInstrumentFilterConditions(); + ElectricGuitarFilterConditions filterConditions = createElectricGuitarFilterConditions(); InstrumentSortOption sort = InstrumentSortOption.CREATED_BY_DESC; User user = createUser(); Page expectedResult = new PageImpl<>(List.of( @@ -470,4 +471,20 @@ private InstrumentFilterConditions createInstrumentFilterConditions() throws Exc instrumentFilterConditionsConstructor.setAccessible(true); return instrumentFilterConditionsConstructor.newInstance(); } + + private ElectricGuitarFilterConditions createElectricGuitarFilterConditions() throws Exception { + Constructor constructor = ElectricGuitarFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class, + ElectricGuitarBrand.class, ElectricGuitarModel.class, GuitarColor.class + ); + constructor.setAccessible(true); + return constructor.newInstance( + InstrumentProgressStatus.SELLING, + "서울특별시", + "종로구", + ElectricGuitarBrand.FENDER_USA, + ElectricGuitarModel.TELECASTER, + GuitarColor.RED + ); + } } From bffdf63dc0fd707d7aacc7d2e27f1544df09dfd8 Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 01:03:32 +0900 Subject: [PATCH 02/10] =?UTF-8?q?feat:=20#67=20=EB=B2=A0=EC=9D=B4=EC=8A=A4?= =?UTF-8?q?=20=EA=B8=B0=ED=83=80=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20API=20=ED=95=84=ED=84=B0=20=EC=A1=B0=EA=B1=B4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/InstrumentController.java | 3 +- .../request/BassGuitarFilterConditions.java | 39 +++++++++++ .../InstrumentRepositoryCustom.java | 3 +- .../InstrumentRepositoryCustomImpl.java | 69 ++++++++++++++++--- .../service/InstrumentQueryService.java | 3 +- .../repository/InstrumentRepositoryTest.java | 19 +++-- .../controller/InstrumentControllerTest.java | 5 +- .../service/InstrumentQueryServiceTest.java | 20 +++++- 8 files changed, 141 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/ajou/hertz/domain/instrument/dto/request/BassGuitarFilterConditions.java diff --git a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java index c94ca54..4450d95 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java @@ -24,6 +24,7 @@ import com.ajou.hertz.domain.instrument.dto.BassGuitarDto; import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; +import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAudioEquipmentRequest; @@ -122,7 +123,7 @@ public Page findBassGuitarsV1( @Parameter( description = "정렬 기준" ) @RequestParam InstrumentSortOption sort, - @ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions + @ParameterObject @Valid @ModelAttribute BassGuitarFilterConditions filterConditions ) { return instrumentQueryService .findBassGuitars(page, size, sort, filterConditions) diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/BassGuitarFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/BassGuitarFilterConditions.java new file mode 100644 index 0000000..d8e21ee --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/BassGuitarFilterConditions.java @@ -0,0 +1,39 @@ +package com.ajou.hertz.domain.instrument.dto.request; + +import com.ajou.hertz.domain.instrument.constant.BassGuitarBrand; +import com.ajou.hertz.domain.instrument.constant.BassGuitarPickUp; +import com.ajou.hertz.domain.instrument.constant.BassGuitarPreAmplifier; +import com.ajou.hertz.domain.instrument.constant.GuitarColor; +import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Setter // for @ModelAttribute +@Getter +public class BassGuitarFilterConditions extends InstrumentFilterConditions { + + private BassGuitarBrand brand; + private BassGuitarPickUp pickUp; + private BassGuitarPreAmplifier preAmplifier; + private GuitarColor color; + + public BassGuitarFilterConditions( + InstrumentProgressStatus progress, + String sido, + String sgg, + BassGuitarBrand brand, + BassGuitarPickUp pickUp, + BassGuitarPreAmplifier preAmplifier, + GuitarColor color + ) { + super(progress, sido, sgg); + this.brand = brand; + this.pickUp = pickUp; + this.preAmplifier = preAmplifier; + this.color = color; + } +} diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java index 7d45618..04c78c2 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java @@ -3,6 +3,7 @@ import org.springframework.data.domain.Page; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; +import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; @@ -19,7 +20,7 @@ Page findElectricGuitars( ); Page findBassGuitars( - int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions + int page, int pageSize, InstrumentSortOption sort, BassGuitarFilterConditions filterConditions ); Page findAcousticAndClassicGuitars( diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java index bd99a36..cc94ab5 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java @@ -1,5 +1,6 @@ package com.ajou.hertz.domain.instrument.repository; +import static com.ajou.hertz.domain.instrument.entity.QBassGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QElectricGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QInstrument.*; import static com.ajou.hertz.domain.user.entity.QUser.*; @@ -13,11 +14,15 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import com.ajou.hertz.domain.instrument.constant.BassGuitarBrand; +import com.ajou.hertz.domain.instrument.constant.BassGuitarPickUp; +import com.ajou.hertz.domain.instrument.constant.BassGuitarPreAmplifier; import com.ajou.hertz.domain.instrument.constant.ElectricGuitarBrand; import com.ajou.hertz.domain.instrument.constant.ElectricGuitarModel; import com.ajou.hertz.domain.instrument.constant.GuitarColor; import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; +import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; @@ -43,7 +48,6 @@ @RequiredArgsConstructor public class InstrumentRepositoryCustomImpl implements InstrumentRepositoryCustom { - private static final PathBuilder INSTRUMENT_PATH = new PathBuilder<>(Instrument.class, "instrument"); private final JPAQueryFactory queryFactory; @Override @@ -83,9 +87,31 @@ public Page findBassGuitars( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + BassGuitarFilterConditions filterConditions ) { - return findInstrumentsByClassType(BassGuitar.class, page, pageSize, sort, filterConditions); + PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); + + List conditions = + new ArrayList<>(convertBassGuitarFilterConditionsToPredicates(filterConditions)); + + List content = queryFactory + .selectFrom(bassGuitar) + .join(bassGuitar.seller, user).fetchJoin() + .where(conditions.toArray(Predicate[]::new)) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(bassGuitar))) + .fetch(); + + long totalCount = Optional.ofNullable( + queryFactory.select(bassGuitar.count()) + .from(bassGuitar) + .join(bassGuitar.seller, user) + .where(conditions.toArray(Predicate[]::new)) + .fetchOne() + ).orElse(0L); + + return new PageImpl<>(content, pageable, totalCount); } @Override @@ -178,14 +204,29 @@ private OrderSpecifier[] convertSortToOrderSpecifiers( } private List convertElectricGuitarFilterConditionsToPredicates( - ElectricGuitarFilterConditions filterConditions) { + ElectricGuitarFilterConditions filterConditions + ) { List res = new ArrayList<>(); res.add(applyProgressStatusCondition(filterConditions.getProgress(), electricGuitar.progressStatus)); res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), electricGuitar.tradeAddress.sido)); res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), electricGuitar.tradeAddress.sgg)); res.add(applyElectricGuitarBrandCondition(filterConditions.getBrand())); res.add(applyElectricGuitarModelCondition(filterConditions.getModel())); - res.add(applyGuitarColorCondition(filterConditions.getColor())); + res.add(applyGuitarColorCondition(filterConditions.getColor(), electricGuitar.color)); + return res; + } + + private List convertBassGuitarFilterConditionsToPredicates( + BassGuitarFilterConditions filterConditions + ) { + List res = new ArrayList<>(); + res.add(applyProgressStatusCondition(filterConditions.getProgress(), bassGuitar.progressStatus)); + res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), bassGuitar.tradeAddress.sido)); + res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), bassGuitar.tradeAddress.sgg)); + res.add(applyBassGuitarBrandCondition(filterConditions.getBrand())); + res.add(applyBassGuitarPickUpCondition(filterConditions.getPickUp())); + res.add(applyBassGuitarPreAmplifier(filterConditions.getPreAmplifier())); + res.add(applyGuitarColorCondition(filterConditions.getColor(), bassGuitar.color)); return res; } @@ -212,11 +253,23 @@ private BooleanExpression applyElectricGuitarModelCondition(ElectricGuitarModel return createCondition(model, electricGuitar.model); } - private BooleanExpression applyGuitarColorCondition(GuitarColor color) { - return createCondition(color, electricGuitar.color); + private BooleanExpression applyBassGuitarBrandCondition(BassGuitarBrand brand) { + return createCondition(brand, bassGuitar.brand); + } + + private BooleanExpression applyBassGuitarPickUpCondition(BassGuitarPickUp pickUp) { + return createCondition(pickUp, bassGuitar.pickUp); + } + + private BooleanExpression applyBassGuitarPreAmplifier(BassGuitarPreAmplifier preAmplifier) { + return createCondition(preAmplifier, bassGuitar.preAmplifier); + } + + private BooleanExpression applyGuitarColorCondition(GuitarColor color, EnumPath colorExpression) { + return createCondition(color, colorExpression); } private > BooleanExpression createCondition(T value, ComparableExpression path) { return value != null ? path.eq(value) : null; } -} \ No newline at end of file +} diff --git a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java index 0d8ad10..520faeb 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java @@ -13,6 +13,7 @@ import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; +import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.repository.InstrumentRepository; @@ -47,7 +48,7 @@ public Page findBassGuitars( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + BassGuitarFilterConditions filterConditions ) { return instrumentRepository .findBassGuitars(page, pageSize, sort, filterConditions) diff --git a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java index 066245e..f6172d1 100644 --- a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java +++ b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java @@ -36,6 +36,7 @@ import com.ajou.hertz.domain.instrument.constant.GuitarColor; import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; +import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; @@ -92,7 +93,7 @@ public InstrumentRepositoryTest( void 베이스_기타_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; - InstrumentFilterConditions filterConditions = createInstrumentFilterConditions(); + BassGuitarFilterConditions filterConditions = createEmptyBassGuitarFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createElectricGuitar(user), @@ -111,7 +112,7 @@ public InstrumentRepositoryTest( void 어쿠스틱_클래식_기타_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; - InstrumentFilterConditions filterConditions = createInstrumentFilterConditions(); + InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createElectricGuitar(user), @@ -132,7 +133,7 @@ public InstrumentRepositoryTest( void 이펙터_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; - InstrumentFilterConditions filterConditions = createInstrumentFilterConditions(); + InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createElectricGuitar(user), @@ -151,7 +152,7 @@ public InstrumentRepositoryTest( void 앰프_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; - InstrumentFilterConditions filterConditions = createInstrumentFilterConditions(); + InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createElectricGuitar(user), @@ -170,7 +171,7 @@ public InstrumentRepositoryTest( void 음향_장비_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_ASC; - InstrumentFilterConditions filterConditions = createInstrumentFilterConditions(); + InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createElectricGuitar(user), @@ -349,7 +350,7 @@ private AudioEquipment createAudioEquipment(User seller) throws Exception { ); } - private InstrumentFilterConditions createInstrumentFilterConditions() throws Exception { + private InstrumentFilterConditions createEmptyInstrumentFilterConditions() throws Exception { Constructor instrumentFilterConditionsConstructor = InstrumentFilterConditions.class.getDeclaredConstructor(); instrumentFilterConditionsConstructor.setAccessible(true); @@ -371,4 +372,10 @@ private ElectricGuitarFilterConditions createElectricGuitarFilterConditions() th GuitarColor.RED ); } + + private BassGuitarFilterConditions createEmptyBassGuitarFilterConditions() throws Exception { + Constructor constructor = BassGuitarFilterConditions.class.getDeclaredConstructor(); + constructor.setAccessible(true); + return constructor.newInstance(); + } } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java index 05d7581..80cd4ba 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java @@ -57,6 +57,7 @@ import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; import com.ajou.hertz.domain.instrument.dto.InstrumentImageDto; +import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAudioEquipmentRequest; @@ -171,7 +172,7 @@ public InstrumentControllerTest(MockMvc mvc) { createBassGuitarDto(4L, userId) )); given(instrumentQueryService.findBassGuitars( - eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class) + eq(page), eq(pageSize), eq(sortOption), any(BassGuitarFilterConditions.class) )).willReturn(expectedResult); // when & then @@ -189,7 +190,7 @@ public InstrumentControllerTest(MockMvc mvc) { .andExpect(jsonPath("$.content", hasSize(expectedResult.getNumberOfElements()))); then(instrumentQueryService) .should() - .findBassGuitars(eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class)); + .findBassGuitars(eq(page), eq(pageSize), eq(sortOption), any(BassGuitarFilterConditions.class)); verifyEveryMocksShouldHaveNoMoreInteractions(); } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java index fed8057..5a086e3 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java @@ -45,6 +45,7 @@ import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; +import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; @@ -131,7 +132,7 @@ class InstrumentQueryServiceTest { // given int page = 0; int pageSize = 10; - InstrumentFilterConditions filterConditions = createInstrumentFilterConditions(); + BassGuitarFilterConditions filterConditions = createBassGuitarFilterConditions(); InstrumentSortOption sort = InstrumentSortOption.CREATED_BY_DESC; User user = createUser(); Page expectedResult = new PageImpl<>(List.of( @@ -487,4 +488,21 @@ private ElectricGuitarFilterConditions createElectricGuitarFilterConditions() th GuitarColor.RED ); } + + private BassGuitarFilterConditions createBassGuitarFilterConditions() throws Exception { + Constructor constructor = BassGuitarFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class, + BassGuitarBrand.class, BassGuitarPickUp.class, BassGuitarPreAmplifier.class, GuitarColor.class + ); + constructor.setAccessible(true); + return constructor.newInstance( + InstrumentProgressStatus.SELLING, + "서울특별시", + "종로구", + BassGuitarBrand.FENDER, + BassGuitarPickUp.JAZZ, + BassGuitarPreAmplifier.ACTIVE, + GuitarColor.RED + ); + } } From ca467913bc035efcece57b4511747930fe36c36e Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 20:35:44 +0900 Subject: [PATCH 03/10] =?UTF-8?q?feat:=20#67=20=EC=96=B4=EC=BF=A0=EC=8A=A4?= =?UTF-8?q?=ED=8B=B1&=ED=81=B4=EB=9E=98=EC=8B=9D=20=EA=B8=B0=ED=83=80=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20API=20=ED=95=84?= =?UTF-8?q?=ED=84=B0=20=EC=A1=B0=EA=B1=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/InstrumentController.java | 3 +- ...usticAndClassicGuitarFilterConditions.java | 39 ++++++++++++ .../request/BassGuitarFilterConditions.java | 2 +- .../ElectricGuitarFilterConditions.java | 2 +- .../InstrumentRepositoryCustom.java | 3 +- .../InstrumentRepositoryCustomImpl.java | 62 ++++++++++++++++++- .../service/InstrumentQueryService.java | 3 +- .../repository/InstrumentRepositoryTest.java | 26 ++++---- .../controller/InstrumentControllerTest.java | 5 +- .../service/InstrumentQueryServiceTest.java | 17 ++++- .../com/ajou/hertz/util/ReflectionUtils.java | 19 ++++++ 11 files changed, 157 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/ajou/hertz/domain/instrument/dto/request/AcousticAndClassicGuitarFilterConditions.java diff --git a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java index 4450d95..a2eacb7 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java @@ -24,6 +24,7 @@ import com.ajou.hertz.domain.instrument.dto.BassGuitarDto; import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; +import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; @@ -147,7 +148,7 @@ public Page findAcousticAndClassicGuitarsV1( @Parameter( description = "정렬 기준" ) @RequestParam InstrumentSortOption sort, - @ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions + @ParameterObject @Valid @ModelAttribute AcousticAndClassicGuitarFilterConditions filterConditions ) { return instrumentQueryService .findAcousticAndClassicGuitars(page, size, sort, filterConditions) diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/AcousticAndClassicGuitarFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/AcousticAndClassicGuitarFilterConditions.java new file mode 100644 index 0000000..e35cd06 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/AcousticAndClassicGuitarFilterConditions.java @@ -0,0 +1,39 @@ +package com.ajou.hertz.domain.instrument.dto.request; + +import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarBrand; +import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarModel; +import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarPickUp; +import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarWood; +import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Setter // for @ModelAttribute +@Getter +public class AcousticAndClassicGuitarFilterConditions extends InstrumentFilterConditions { + + private AcousticAndClassicGuitarBrand brand; + private AcousticAndClassicGuitarModel model; + private AcousticAndClassicGuitarWood wood; + private AcousticAndClassicGuitarPickUp pickUp; + + private AcousticAndClassicGuitarFilterConditions( + InstrumentProgressStatus progress, + String sido, + String sgg, + AcousticAndClassicGuitarBrand brand, + AcousticAndClassicGuitarModel model, + AcousticAndClassicGuitarWood wood, + AcousticAndClassicGuitarPickUp pickUp + ) { + super(progress, sido, sgg); + this.brand = brand; + this.model = model; + this.wood = wood; + this.pickUp = pickUp; + } +} diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/BassGuitarFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/BassGuitarFilterConditions.java index d8e21ee..e03f7cd 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/BassGuitarFilterConditions.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/BassGuitarFilterConditions.java @@ -21,7 +21,7 @@ public class BassGuitarFilterConditions extends InstrumentFilterConditions { private BassGuitarPreAmplifier preAmplifier; private GuitarColor color; - public BassGuitarFilterConditions( + private BassGuitarFilterConditions( InstrumentProgressStatus progress, String sido, String sgg, diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/ElectricGuitarFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/ElectricGuitarFilterConditions.java index 0c66b5f..ca78027 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/ElectricGuitarFilterConditions.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/ElectricGuitarFilterConditions.java @@ -19,7 +19,7 @@ public class ElectricGuitarFilterConditions extends InstrumentFilterConditions { private ElectricGuitarModel model; private GuitarColor color; - public ElectricGuitarFilterConditions( + private ElectricGuitarFilterConditions( InstrumentProgressStatus progress, String sido, String sgg, diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java index 04c78c2..0365424 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java @@ -3,6 +3,7 @@ import org.springframework.data.domain.Page; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; +import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; @@ -24,7 +25,7 @@ Page findBassGuitars( ); Page findAcousticAndClassicGuitars( - int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions + int page, int pageSize, InstrumentSortOption sort, AcousticAndClassicGuitarFilterConditions filterConditions ); Page findEffectors( diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java index cc94ab5..babf298 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java @@ -1,5 +1,6 @@ package com.ajou.hertz.domain.instrument.repository; +import static com.ajou.hertz.domain.instrument.entity.QAcousticAndClassicGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QBassGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QElectricGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QInstrument.*; @@ -14,6 +15,10 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarBrand; +import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarModel; +import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarPickUp; +import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarWood; import com.ajou.hertz.domain.instrument.constant.BassGuitarBrand; import com.ajou.hertz.domain.instrument.constant.BassGuitarPickUp; import com.ajou.hertz.domain.instrument.constant.BassGuitarPreAmplifier; @@ -22,6 +27,7 @@ import com.ajou.hertz.domain.instrument.constant.GuitarColor; import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; +import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; @@ -119,9 +125,31 @@ public Page findAcousticAndClassicGuitars( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + AcousticAndClassicGuitarFilterConditions filterConditions ) { - return findInstrumentsByClassType(AcousticAndClassicGuitar.class, page, pageSize, sort, filterConditions); + PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); + + List conditions = + new ArrayList<>(convertAcousticAndClassicGuitarFilterConditionsToPredicates(filterConditions)); + + List content = queryFactory + .selectFrom(acousticAndClassicGuitar) + .join(acousticAndClassicGuitar.seller, user).fetchJoin() + .where(conditions.toArray(Predicate[]::new)) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(acousticAndClassicGuitar))) + .fetch(); + + long totalCount = Optional.ofNullable( + queryFactory.select(acousticAndClassicGuitar.count()) + .from(acousticAndClassicGuitar) + .join(acousticAndClassicGuitar.seller, user) + .where(conditions.toArray(Predicate[]::new)) + .fetchOne() + ).orElse(0L); + + return new PageImpl<>(content, pageable, totalCount); } @Override @@ -230,6 +258,20 @@ private List convertBassGuitarFilterConditionsToPredicates( return res; } + private List convertAcousticAndClassicGuitarFilterConditionsToPredicates( + AcousticAndClassicGuitarFilterConditions filterConditions + ) { + List res = new ArrayList<>(); + res.add(applyProgressStatusCondition(filterConditions.getProgress(), bassGuitar.progressStatus)); + res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), bassGuitar.tradeAddress.sido)); + res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), bassGuitar.tradeAddress.sgg)); + res.add(applyAcousticAndClassicGuitarBrandCondition(filterConditions.getBrand())); + res.add(applyAcousticAndClassicGuitarModelCondition(filterConditions.getModel())); + res.add(applyAcousticAndClassicGuitarWood(filterConditions.getWood())); + res.add(applyAcousticAndClassicGuitarPickUpCondition(filterConditions.getPickUp())); + return res; + } + private BooleanExpression applyProgressStatusCondition( InstrumentProgressStatus progressStatus, EnumPath progressStatusExpression @@ -269,6 +311,22 @@ private BooleanExpression applyGuitarColorCondition(GuitarColor color, EnumPath< return createCondition(color, colorExpression); } + private Predicate applyAcousticAndClassicGuitarBrandCondition(AcousticAndClassicGuitarBrand brand) { + return createCondition(brand, acousticAndClassicGuitar.brand); + } + + private Predicate applyAcousticAndClassicGuitarModelCondition(AcousticAndClassicGuitarModel model) { + return createCondition(model, acousticAndClassicGuitar.model); + } + + private Predicate applyAcousticAndClassicGuitarWood(AcousticAndClassicGuitarWood wood) { + return createCondition(wood, acousticAndClassicGuitar.wood); + } + + private Predicate applyAcousticAndClassicGuitarPickUpCondition(AcousticAndClassicGuitarPickUp pickUp) { + return createCondition(pickUp, acousticAndClassicGuitar.pickUp); + } + private > BooleanExpression createCondition(T value, ComparableExpression path) { return value != null ? path.eq(value) : null; } diff --git a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java index 520faeb..0b27956 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java @@ -13,6 +13,7 @@ import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; +import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; @@ -59,7 +60,7 @@ public Page findAcousticAndClassicGuitars( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + AcousticAndClassicGuitarFilterConditions filterConditions ) { return instrumentRepository .findAcousticAndClassicGuitars(page, pageSize, sort, filterConditions) diff --git a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java index 38e3a15..e5da97e 100644 --- a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java +++ b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java @@ -35,6 +35,7 @@ import com.ajou.hertz.domain.instrument.constant.GuitarColor; import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; +import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; @@ -74,7 +75,7 @@ public InstrumentRepositoryTest( void 일렉_기타_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; - ElectricGuitarFilterConditions filterConditions = createElectricGuitarFilterConditions(); + ElectricGuitarFilterConditions filterConditions = createEmptyElectricGuitarFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createBassGuitar(user), @@ -112,7 +113,8 @@ public InstrumentRepositoryTest( void 어쿠스틱_클래식_기타_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; - InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); + AcousticAndClassicGuitarFilterConditions filterConditions = + createEmptyAcousticAndClassicGuitarFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createElectricGuitar(user), @@ -121,9 +123,7 @@ public InstrumentRepositoryTest( )); // when - Page result = sut.findAcousticAndClassicGuitars( - 0, 10, sortOption, filterConditions - ); + Page result = sut.findAcousticAndClassicGuitars(0, 10, sortOption, filterConditions); // then assertThat(result.getNumberOfElements()).isEqualTo(savedInstruments.size() - 1); @@ -311,18 +311,16 @@ private InstrumentFilterConditions createEmptyInstrumentFilterConditions() throw return ReflectionUtils.createInstrumentFilterConditions(null, null, null); } - private ElectricGuitarFilterConditions createElectricGuitarFilterConditions() throws Exception { - return ReflectionUtils.createElectricGuitarFilterConditions( - InstrumentProgressStatus.SELLING, - "서울특별시", - null, - ElectricGuitarBrand.FENDER_USA, - ElectricGuitarModel.TELECASTER, - GuitarColor.RED - ); + private ElectricGuitarFilterConditions createEmptyElectricGuitarFilterConditions() throws Exception { + return ReflectionUtils.createElectricGuitarFilterConditions(null, null, null, null, null, null); } private BassGuitarFilterConditions createEmptyBassGuitarFilterConditions() throws Exception { return ReflectionUtils.createBassGuitarFilterConditions(null, null, null, null, null, null, null); } + + private AcousticAndClassicGuitarFilterConditions createEmptyAcousticAndClassicGuitarFilterConditions( + ) throws Exception { + return ReflectionUtils.createAcousticAndClassicGuitarFilterConditions(null, null, null, null, null, null, null); + } } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java index 275d282..66d7eb1 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java @@ -56,6 +56,7 @@ import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; import com.ajou.hertz.domain.instrument.dto.InstrumentImageDto; +import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; @@ -208,7 +209,7 @@ public InstrumentControllerTest(MockMvc mvc) { createAcousticAndClassicGuitarDto(4L, userId) )); given(instrumentQueryService.findAcousticAndClassicGuitars( - eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class) + eq(page), eq(pageSize), eq(sortOption), any(AcousticAndClassicGuitarFilterConditions.class) )).willReturn(expectedResult); // when & then @@ -227,7 +228,7 @@ public InstrumentControllerTest(MockMvc mvc) { then(instrumentQueryService) .should() .findAcousticAndClassicGuitars( - eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class) + eq(page), eq(pageSize), eq(sortOption), any(AcousticAndClassicGuitarFilterConditions.class) ); verifyEveryMocksShouldHaveNoMoreInteractions(); } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java index 5abde5d..f7428a3 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java @@ -44,6 +44,7 @@ import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; +import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; @@ -160,7 +161,8 @@ class InstrumentQueryServiceTest { // given int page = 0; int pageSize = 10; - InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); + AcousticAndClassicGuitarFilterConditions filterConditions = + createEmptyAcousticAndClassicGuitarFilterConditions(); InstrumentSortOption sort = InstrumentSortOption.CREATED_BY_DESC; User user = createUser(); Page expectedResult = new PageImpl<>(List.of( @@ -443,4 +445,17 @@ private BassGuitarFilterConditions createBassGuitarFilterConditions() throws Exc GuitarColor.RED ); } + + private AcousticAndClassicGuitarFilterConditions createEmptyAcousticAndClassicGuitarFilterConditions( + ) throws Exception { + return ReflectionUtils.createAcousticAndClassicGuitarFilterConditions( + InstrumentProgressStatus.RESERVED, + "서울특별시", + "종로구", + AcousticAndClassicGuitarBrand.BENTIVOGLIO, + AcousticAndClassicGuitarModel.DREADNOUGHT, + AcousticAndClassicGuitarWood.PLYWOOD_AND_SOLID_WOOD, + AcousticAndClassicGuitarPickUp.MAGNETIC + ); + } } diff --git a/src/test/java/com/ajou/hertz/util/ReflectionUtils.java b/src/test/java/com/ajou/hertz/util/ReflectionUtils.java index 2271976..ad16565 100644 --- a/src/test/java/com/ajou/hertz/util/ReflectionUtils.java +++ b/src/test/java/com/ajou/hertz/util/ReflectionUtils.java @@ -42,6 +42,7 @@ import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentImageDto; +import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; @@ -763,6 +764,24 @@ public static BassGuitarFilterConditions createBassGuitarFilterConditions( return constructor.newInstance(progressStatus, sido, sgg, brand, pickUp, preAmplifier, color); } + public static AcousticAndClassicGuitarFilterConditions createAcousticAndClassicGuitarFilterConditions( + InstrumentProgressStatus progressStatus, + String sido, + String sgg, + AcousticAndClassicGuitarBrand brand, + AcousticAndClassicGuitarModel model, + AcousticAndClassicGuitarWood wood, + AcousticAndClassicGuitarPickUp pickUp + ) throws Exception { + Constructor constructor = AcousticAndClassicGuitarFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class, + AcousticAndClassicGuitarBrand.class, AcousticAndClassicGuitarModel.class, + AcousticAndClassicGuitarWood.class, AcousticAndClassicGuitarPickUp.class + ); + constructor.setAccessible(true); + return constructor.newInstance(progressStatus, sido, sgg, brand, model, wood, pickUp); + } + /** * DTO(Response) */ From 9d6c287414ec4fff388288f5232f6e222ecf6326 Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 20:44:19 +0900 Subject: [PATCH 04/10] =?UTF-8?q?feat:=20#67=20=EC=9D=B4=ED=8E=99=ED=84=B0?= =?UTF-8?q?=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20API=20=ED=95=84?= =?UTF-8?q?=ED=84=B0=20=EC=A1=B0=EA=B1=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/InstrumentController.java | 3 +- .../dto/request/EffectorFilterConditions.java | 31 ++++++++++ .../InstrumentRepositoryCustom.java | 3 +- .../InstrumentRepositoryCustomImpl.java | 57 +++++++++++++++++-- .../service/InstrumentQueryService.java | 3 +- .../repository/InstrumentRepositoryTest.java | 7 ++- .../controller/InstrumentControllerTest.java | 5 +- .../service/InstrumentQueryServiceTest.java | 13 ++++- .../com/ajou/hertz/util/ReflectionUtils.java | 16 ++++++ 9 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/ajou/hertz/domain/instrument/dto/request/EffectorFilterConditions.java diff --git a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java index a2eacb7..756299f 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java @@ -32,6 +32,7 @@ import com.ajou.hertz.domain.instrument.dto.request.CreateNewBassGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewEffectorRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewElectricGuitarRequest; +import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.dto.response.AcousticAndClassicGuitarResponse; @@ -172,7 +173,7 @@ public Page findEffectorsV1( @Parameter( description = "정렬 기준" ) @RequestParam InstrumentSortOption sort, - @ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions + @ParameterObject @Valid @ModelAttribute EffectorFilterConditions filterConditions ) { return instrumentQueryService .findEffectors(page, size, sort, filterConditions) diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/EffectorFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/EffectorFilterConditions.java new file mode 100644 index 0000000..0e80f3e --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/EffectorFilterConditions.java @@ -0,0 +1,31 @@ +package com.ajou.hertz.domain.instrument.dto.request; + +import com.ajou.hertz.domain.instrument.constant.EffectorFeature; +import com.ajou.hertz.domain.instrument.constant.EffectorType; +import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Setter // for @ModelAttribute +@Getter +public class EffectorFilterConditions extends InstrumentFilterConditions { + + private EffectorType type; + private EffectorFeature feature; + + private EffectorFilterConditions( + InstrumentProgressStatus progress, + String sido, + String sgg, + EffectorType type, + EffectorFeature feature + ) { + super(progress, sido, sgg); + this.type = type; + this.feature = feature; + } +} diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java index 0365424..31f0a3f 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java @@ -5,6 +5,7 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; @@ -29,7 +30,7 @@ Page findAcousticAndClassicGuitars( ); Page findEffectors( - int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions + int page, int pageSize, InstrumentSortOption sort, EffectorFilterConditions filterConditions ); Page findAmplifiers( diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java index babf298..f151844 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java @@ -2,6 +2,7 @@ import static com.ajou.hertz.domain.instrument.entity.QAcousticAndClassicGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QBassGuitar.*; +import static com.ajou.hertz.domain.instrument.entity.QEffector.*; import static com.ajou.hertz.domain.instrument.entity.QElectricGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QInstrument.*; import static com.ajou.hertz.domain.user.entity.QUser.*; @@ -22,6 +23,8 @@ import com.ajou.hertz.domain.instrument.constant.BassGuitarBrand; import com.ajou.hertz.domain.instrument.constant.BassGuitarPickUp; import com.ajou.hertz.domain.instrument.constant.BassGuitarPreAmplifier; +import com.ajou.hertz.domain.instrument.constant.EffectorFeature; +import com.ajou.hertz.domain.instrument.constant.EffectorType; import com.ajou.hertz.domain.instrument.constant.ElectricGuitarBrand; import com.ajou.hertz.domain.instrument.constant.ElectricGuitarModel; import com.ajou.hertz.domain.instrument.constant.GuitarColor; @@ -29,6 +32,7 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; @@ -157,9 +161,30 @@ public Page findEffectors( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + EffectorFilterConditions filterConditions ) { - return findInstrumentsByClassType(Effector.class, page, pageSize, sort, filterConditions); + PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); + + List conditions = new ArrayList<>(convertEffectorFilterConditionsToPredicates(filterConditions)); + + List content = queryFactory + .selectFrom(effector) + .join(effector.seller, user).fetchJoin() + .where(conditions.toArray(Predicate[]::new)) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(effector))) + .fetch(); + + long totalCount = Optional.ofNullable( + queryFactory.select(effector.count()) + .from(effector) + .join(effector.seller, user) + .where(conditions.toArray(Predicate[]::new)) + .fetchOne() + ).orElse(0L); + + return new PageImpl<>(content, pageable, totalCount); } @Override @@ -272,6 +297,18 @@ private List convertAcousticAndClassicGuitarFilterConditionsToPredica return res; } + private List convertEffectorFilterConditionsToPredicates( + EffectorFilterConditions filterConditions + ) { + List res = new ArrayList<>(); + res.add(applyProgressStatusCondition(filterConditions.getProgress(), bassGuitar.progressStatus)); + res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), bassGuitar.tradeAddress.sido)); + res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), bassGuitar.tradeAddress.sgg)); + res.add(applyEffectorTypeCondition(filterConditions.getType())); + res.add(applyEffectorFeatureCondition(filterConditions.getFeature())); + return res; + } + private BooleanExpression applyProgressStatusCondition( InstrumentProgressStatus progressStatus, EnumPath progressStatusExpression @@ -311,22 +348,30 @@ private BooleanExpression applyGuitarColorCondition(GuitarColor color, EnumPath< return createCondition(color, colorExpression); } - private Predicate applyAcousticAndClassicGuitarBrandCondition(AcousticAndClassicGuitarBrand brand) { + private BooleanExpression applyAcousticAndClassicGuitarBrandCondition(AcousticAndClassicGuitarBrand brand) { return createCondition(brand, acousticAndClassicGuitar.brand); } - private Predicate applyAcousticAndClassicGuitarModelCondition(AcousticAndClassicGuitarModel model) { + private BooleanExpression applyAcousticAndClassicGuitarModelCondition(AcousticAndClassicGuitarModel model) { return createCondition(model, acousticAndClassicGuitar.model); } - private Predicate applyAcousticAndClassicGuitarWood(AcousticAndClassicGuitarWood wood) { + private BooleanExpression applyAcousticAndClassicGuitarWood(AcousticAndClassicGuitarWood wood) { return createCondition(wood, acousticAndClassicGuitar.wood); } - private Predicate applyAcousticAndClassicGuitarPickUpCondition(AcousticAndClassicGuitarPickUp pickUp) { + private BooleanExpression applyAcousticAndClassicGuitarPickUpCondition(AcousticAndClassicGuitarPickUp pickUp) { return createCondition(pickUp, acousticAndClassicGuitar.pickUp); } + private BooleanExpression applyEffectorTypeCondition(EffectorType type) { + return createCondition(type, effector.type); + } + + private BooleanExpression applyEffectorFeatureCondition(EffectorFeature feature) { + return createCondition(feature, effector.feature); + } + private > BooleanExpression createCondition(T value, ComparableExpression path) { return value != null ? path.eq(value) : null; } diff --git a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java index 0b27956..99e87f9 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java @@ -15,6 +15,7 @@ import com.ajou.hertz.domain.instrument.dto.InstrumentDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.repository.InstrumentRepository; @@ -71,7 +72,7 @@ public Page findEffectors( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + EffectorFilterConditions filterConditions ) { return instrumentRepository .findEffectors(page, pageSize, sort, filterConditions) diff --git a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java index e5da97e..6fc839e 100644 --- a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java +++ b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java @@ -37,6 +37,7 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; @@ -133,7 +134,7 @@ public InstrumentRepositoryTest( void 이펙터_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; - InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); + EffectorFilterConditions filterConditions = createEmptyEffectorFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createElectricGuitar(user), @@ -323,4 +324,8 @@ private AcousticAndClassicGuitarFilterConditions createEmptyAcousticAndClassicGu ) throws Exception { return ReflectionUtils.createAcousticAndClassicGuitarFilterConditions(null, null, null, null, null, null, null); } + + private EffectorFilterConditions createEmptyEffectorFilterConditions() throws Exception { + return ReflectionUtils.createEffectorFilterConditions(null, null, null, null, null); + } } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java index 66d7eb1..5f25d4c 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java @@ -64,6 +64,7 @@ import com.ajou.hertz.domain.instrument.dto.request.CreateNewBassGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewEffectorRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewElectricGuitarRequest; +import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.service.InstrumentCommandService; @@ -247,7 +248,7 @@ public InstrumentControllerTest(MockMvc mvc) { createEffectorDto(4L, userId) )); given(instrumentQueryService.findEffectors( - eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class) + eq(page), eq(pageSize), eq(sortOption), any(EffectorFilterConditions.class) )).willReturn(expectedResult); // when & then @@ -265,7 +266,7 @@ public InstrumentControllerTest(MockMvc mvc) { .andExpect(jsonPath("$.content", hasSize(expectedResult.getNumberOfElements()))); then(instrumentQueryService) .should() - .findEffectors(eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class)); + .findEffectors(eq(page), eq(pageSize), eq(sortOption), any(EffectorFilterConditions.class)); verifyEveryMocksShouldHaveNoMoreInteractions(); } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java index f7428a3..5c279e8 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java @@ -46,6 +46,7 @@ import com.ajou.hertz.domain.instrument.dto.InstrumentDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; @@ -192,7 +193,7 @@ class InstrumentQueryServiceTest { // given int page = 0; int pageSize = 10; - InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); + EffectorFilterConditions filterConditions = createEmptyEffectorFilterConditions(); InstrumentSortOption sort = InstrumentSortOption.CREATED_BY_DESC; User user = createUser(); Page expectedResult = new PageImpl<>(List.of( @@ -458,4 +459,14 @@ private AcousticAndClassicGuitarFilterConditions createEmptyAcousticAndClassicGu AcousticAndClassicGuitarPickUp.MAGNETIC ); } + + private EffectorFilterConditions createEmptyEffectorFilterConditions() throws Exception { + return ReflectionUtils.createEffectorFilterConditions( + InstrumentProgressStatus.SOLD_OUT, + "서울특별시", + "종로구", + EffectorType.GUITAR, + EffectorFeature.ETC + ); + } } diff --git a/src/test/java/com/ajou/hertz/util/ReflectionUtils.java b/src/test/java/com/ajou/hertz/util/ReflectionUtils.java index ad16565..b870742 100644 --- a/src/test/java/com/ajou/hertz/util/ReflectionUtils.java +++ b/src/test/java/com/ajou/hertz/util/ReflectionUtils.java @@ -50,6 +50,7 @@ import com.ajou.hertz.domain.instrument.dto.request.CreateNewBassGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewEffectorRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewElectricGuitarRequest; +import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; @@ -782,6 +783,21 @@ public static AcousticAndClassicGuitarFilterConditions createAcousticAndClassicG return constructor.newInstance(progressStatus, sido, sgg, brand, model, wood, pickUp); } + public static EffectorFilterConditions createEffectorFilterConditions( + InstrumentProgressStatus progressStatus, + String sido, + String sgg, + EffectorType type, + EffectorFeature feature + ) throws Exception { + Constructor constructor = EffectorFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class, + EffectorType.class, EffectorFeature.class + ); + constructor.setAccessible(true); + return constructor.newInstance(progressStatus, sido, sgg, type, feature); + } + /** * DTO(Response) */ From 6def70a8a0fb16d2b689181595d8260521438bda Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 20:53:02 +0900 Subject: [PATCH 05/10] =?UTF-8?q?feat:=20#67=20=EC=95=B0=ED=94=84=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20API=20=ED=95=84?= =?UTF-8?q?=ED=84=B0=20=EC=A1=B0=EA=B1=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/InstrumentController.java | 3 +- .../request/AmplifierFilterConditions.java | 35 ++++++++++++ .../InstrumentRepositoryCustom.java | 3 +- .../InstrumentRepositoryCustomImpl.java | 55 ++++++++++++++++++- .../service/InstrumentQueryService.java | 3 +- .../repository/InstrumentRepositoryTest.java | 7 ++- .../controller/InstrumentControllerTest.java | 5 +- .../service/InstrumentQueryServiceTest.java | 14 ++++- .../com/ajou/hertz/util/ReflectionUtils.java | 17 ++++++ 9 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/ajou/hertz/domain/instrument/dto/request/AmplifierFilterConditions.java diff --git a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java index 756299f..55c0fb5 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java @@ -25,6 +25,7 @@ import com.ajou.hertz.domain.instrument.dto.EffectorDto; import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; @@ -197,7 +198,7 @@ public Page findAmplifiersV1( @Parameter( description = "정렬 기준" ) @RequestParam InstrumentSortOption sort, - @ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions + @ParameterObject @Valid @ModelAttribute AmplifierFilterConditions filterConditions ) { return instrumentQueryService .findAmplifiers(page, size, sort, filterConditions) diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/AmplifierFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/AmplifierFilterConditions.java new file mode 100644 index 0000000..7afd461 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/AmplifierFilterConditions.java @@ -0,0 +1,35 @@ +package com.ajou.hertz.domain.instrument.dto.request; + +import com.ajou.hertz.domain.instrument.constant.AmplifierBrand; +import com.ajou.hertz.domain.instrument.constant.AmplifierType; +import com.ajou.hertz.domain.instrument.constant.AmplifierUsage; +import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Setter // for @ModelAttribute +@Getter +public class AmplifierFilterConditions extends InstrumentFilterConditions { + + private AmplifierType type; + private AmplifierBrand brand; + private AmplifierUsage usage; + + private AmplifierFilterConditions( + InstrumentProgressStatus progress, + String sido, + String sgg, + AmplifierType type, + AmplifierBrand brand, + AmplifierUsage usage + ) { + super(progress, sido, sgg); + this.type = type; + this.brand = brand; + this.usage = usage; + } +} diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java index 31f0a3f..b284957 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java @@ -4,6 +4,7 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; @@ -34,7 +35,7 @@ Page findEffectors( ); Page findAmplifiers( - int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions + int page, int pageSize, InstrumentSortOption sort, AmplifierFilterConditions filterConditions ); Page findAudioEquipments( diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java index f151844..ce99ed5 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java @@ -1,6 +1,7 @@ package com.ajou.hertz.domain.instrument.repository; import static com.ajou.hertz.domain.instrument.entity.QAcousticAndClassicGuitar.*; +import static com.ajou.hertz.domain.instrument.entity.QAmplifier.*; import static com.ajou.hertz.domain.instrument.entity.QBassGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QEffector.*; import static com.ajou.hertz.domain.instrument.entity.QElectricGuitar.*; @@ -20,6 +21,9 @@ import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarModel; import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarPickUp; import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarWood; +import com.ajou.hertz.domain.instrument.constant.AmplifierBrand; +import com.ajou.hertz.domain.instrument.constant.AmplifierType; +import com.ajou.hertz.domain.instrument.constant.AmplifierUsage; import com.ajou.hertz.domain.instrument.constant.BassGuitarBrand; import com.ajou.hertz.domain.instrument.constant.BassGuitarPickUp; import com.ajou.hertz.domain.instrument.constant.BassGuitarPreAmplifier; @@ -31,6 +35,7 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; @@ -192,9 +197,30 @@ public Page findAmplifiers( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + AmplifierFilterConditions filterConditions ) { - return findInstrumentsByClassType(Amplifier.class, page, pageSize, sort, filterConditions); + PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); + + List conditions = new ArrayList<>(convertAmplifierFilterConditionsToPredicates(filterConditions)); + + List content = queryFactory + .selectFrom(amplifier) + .join(amplifier.seller, user).fetchJoin() + .where(conditions.toArray(Predicate[]::new)) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(amplifier))) + .fetch(); + + long totalCount = Optional.ofNullable( + queryFactory.select(amplifier.count()) + .from(amplifier) + .join(amplifier.seller, user) + .where(conditions.toArray(Predicate[]::new)) + .fetchOne() + ).orElse(0L); + + return new PageImpl<>(content, pageable, totalCount); } @Override @@ -309,6 +335,19 @@ private List convertEffectorFilterConditionsToPredicates( return res; } + private List convertAmplifierFilterConditionsToPredicates( + AmplifierFilterConditions filterConditions + ) { + List res = new ArrayList<>(); + res.add(applyProgressStatusCondition(filterConditions.getProgress(), bassGuitar.progressStatus)); + res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), bassGuitar.tradeAddress.sido)); + res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), bassGuitar.tradeAddress.sgg)); + res.add(applyAmplifierTypeCondition(filterConditions.getType())); + res.add(applyAmplifierBrandCondition(filterConditions.getBrand())); + res.add(applyAmplifierUsageCondition(filterConditions.getUsage())); + return res; + } + private BooleanExpression applyProgressStatusCondition( InstrumentProgressStatus progressStatus, EnumPath progressStatusExpression @@ -372,6 +411,18 @@ private BooleanExpression applyEffectorFeatureCondition(EffectorFeature feature) return createCondition(feature, effector.feature); } + private BooleanExpression applyAmplifierTypeCondition(AmplifierType type) { + return createCondition(type, amplifier.type); + } + + private BooleanExpression applyAmplifierBrandCondition(AmplifierBrand brand) { + return createCondition(brand, amplifier.brand); + } + + private BooleanExpression applyAmplifierUsageCondition(AmplifierUsage usage) { + return createCondition(usage, amplifier.usage); + } + private > BooleanExpression createCondition(T value, ComparableExpression path) { return value != null ? path.eq(value) : null; } diff --git a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java index 99e87f9..648c2fa 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java @@ -14,6 +14,7 @@ import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; @@ -83,7 +84,7 @@ public Page findAmplifiers( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + AmplifierFilterConditions filterConditions ) { return instrumentRepository .findAmplifiers(page, pageSize, sort, filterConditions) diff --git a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java index 6fc839e..8e1a01b 100644 --- a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java +++ b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java @@ -36,6 +36,7 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; @@ -153,7 +154,7 @@ public InstrumentRepositoryTest( void 앰프_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; - InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); + AmplifierFilterConditions filterConditions = createEmptyAmplifierFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createElectricGuitar(user), @@ -328,4 +329,8 @@ private AcousticAndClassicGuitarFilterConditions createEmptyAcousticAndClassicGu private EffectorFilterConditions createEmptyEffectorFilterConditions() throws Exception { return ReflectionUtils.createEffectorFilterConditions(null, null, null, null, null); } + + private AmplifierFilterConditions createEmptyAmplifierFilterConditions() throws Exception { + return ReflectionUtils.createAmplifierFilterConditions(null, null, null, null, null, null); + } } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java index 5f25d4c..447a066 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java @@ -57,6 +57,7 @@ import com.ajou.hertz.domain.instrument.dto.InstrumentDto; import com.ajou.hertz.domain.instrument.dto.InstrumentImageDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; @@ -284,7 +285,7 @@ public InstrumentControllerTest(MockMvc mvc) { createAmplifierDto(4L, userId) )); given(instrumentQueryService.findAmplifiers( - eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class) + eq(page), eq(pageSize), eq(sortOption), any(AmplifierFilterConditions.class) )).willReturn(expectedResult); // when & then @@ -302,7 +303,7 @@ public InstrumentControllerTest(MockMvc mvc) { .andExpect(jsonPath("$.content", hasSize(expectedResult.getNumberOfElements()))); then(instrumentQueryService) .should() - .findAmplifiers(eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class)); + .findAmplifiers(eq(page), eq(pageSize), eq(sortOption), any(AmplifierFilterConditions.class)); verifyEveryMocksShouldHaveNoMoreInteractions(); } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java index 5c279e8..527d263 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java @@ -45,6 +45,7 @@ import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; @@ -221,7 +222,7 @@ class InstrumentQueryServiceTest { // given int page = 0; int pageSize = 10; - InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); + AmplifierFilterConditions filterConditions = createAmplifierFilterConditions(); InstrumentSortOption sort = InstrumentSortOption.CREATED_BY_DESC; User user = createUser(); Page expectedResult = new PageImpl<>(List.of( @@ -469,4 +470,15 @@ private EffectorFilterConditions createEmptyEffectorFilterConditions() throws Ex EffectorFeature.ETC ); } + + private AmplifierFilterConditions createAmplifierFilterConditions() throws Exception { + return ReflectionUtils.createAmplifierFilterConditions( + InstrumentProgressStatus.SELLING, + "서울특별시", + "종로구", + AmplifierType.GUITAR, + AmplifierBrand.FENDER, + AmplifierUsage.HOME + ); + } } diff --git a/src/test/java/com/ajou/hertz/util/ReflectionUtils.java b/src/test/java/com/ajou/hertz/util/ReflectionUtils.java index b870742..08e2d7e 100644 --- a/src/test/java/com/ajou/hertz/util/ReflectionUtils.java +++ b/src/test/java/com/ajou/hertz/util/ReflectionUtils.java @@ -43,6 +43,7 @@ import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.InstrumentImageDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; @@ -798,6 +799,22 @@ public static EffectorFilterConditions createEffectorFilterConditions( return constructor.newInstance(progressStatus, sido, sgg, type, feature); } + public static AmplifierFilterConditions createAmplifierFilterConditions( + InstrumentProgressStatus progressStatus, + String sido, + String sgg, + AmplifierType type, + AmplifierBrand brand, + AmplifierUsage usage + ) throws Exception { + Constructor constructor = AmplifierFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class, + AmplifierType.class, AmplifierBrand.class, AmplifierUsage.class + ); + constructor.setAccessible(true); + return constructor.newInstance(progressStatus, sido, sgg, type, brand, usage); + } + /** * DTO(Response) */ From 753678e242af217607ed1b9e092b96fbf047db65 Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 21:01:11 +0900 Subject: [PATCH 06/10] =?UTF-8?q?feat:=20#67=20=EC=9D=8C=ED=96=A5=20?= =?UTF-8?q?=EC=9E=A5=EB=B9=84=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?API=20=ED=95=84=ED=84=B0=20=EC=A1=B0=EA=B1=B4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/InstrumentController.java | 3 +- .../AudioEquipmentFilterConditions.java | 27 +++++++ .../InstrumentRepositoryCustom.java | 4 +- .../InstrumentRepositoryCustomImpl.java | 73 ++++++++++--------- .../service/InstrumentQueryService.java | 4 +- .../repository/InstrumentRepositoryTest.java | 12 +-- .../controller/InstrumentControllerTest.java | 5 +- .../service/InstrumentQueryServiceTest.java | 12 ++- .../com/ajou/hertz/util/ReflectionUtils.java | 70 +++++++++++------- 9 files changed, 136 insertions(+), 74 deletions(-) create mode 100644 src/main/java/com/ajou/hertz/domain/instrument/dto/request/AudioEquipmentFilterConditions.java diff --git a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java index 55c0fb5..f7a60e3 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/controller/InstrumentController.java @@ -26,6 +26,7 @@ import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AudioEquipmentFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; @@ -222,7 +223,7 @@ public Page findAudioEquipmentsV1( @Parameter( description = "정렬 기준" ) @RequestParam InstrumentSortOption sort, - @ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions + @ParameterObject @Valid @ModelAttribute AudioEquipmentFilterConditions filterConditions ) { return instrumentQueryService .findAudioEquipments(page, size, sort, filterConditions) diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/AudioEquipmentFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/AudioEquipmentFilterConditions.java new file mode 100644 index 0000000..dcd4f50 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/AudioEquipmentFilterConditions.java @@ -0,0 +1,27 @@ +package com.ajou.hertz.domain.instrument.dto.request; + +import com.ajou.hertz.domain.instrument.constant.AudioEquipmentType; +import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Setter // for @ModelAttribute +@Getter +public class AudioEquipmentFilterConditions extends InstrumentFilterConditions { + + private AudioEquipmentType type; + + private AudioEquipmentFilterConditions( + InstrumentProgressStatus progress, + String sido, + String sgg, + AudioEquipmentType type + ) { + super(progress, sido, sgg); + this.type = type; + } +} diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java index b284957..58c44ce 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustom.java @@ -5,10 +5,10 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AudioEquipmentFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; -import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; import com.ajou.hertz.domain.instrument.entity.Amplifier; import com.ajou.hertz.domain.instrument.entity.AudioEquipment; @@ -39,6 +39,6 @@ Page findAmplifiers( ); Page findAudioEquipments( - int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions + int page, int pageSize, InstrumentSortOption sort, AudioEquipmentFilterConditions filterConditions ); } diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java index ce99ed5..aa97087 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java @@ -2,10 +2,10 @@ import static com.ajou.hertz.domain.instrument.entity.QAcousticAndClassicGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QAmplifier.*; +import static com.ajou.hertz.domain.instrument.entity.QAudioEquipment.*; import static com.ajou.hertz.domain.instrument.entity.QBassGuitar.*; import static com.ajou.hertz.domain.instrument.entity.QEffector.*; import static com.ajou.hertz.domain.instrument.entity.QElectricGuitar.*; -import static com.ajou.hertz.domain.instrument.entity.QInstrument.*; import static com.ajou.hertz.domain.user.entity.QUser.*; import java.util.ArrayList; @@ -24,6 +24,7 @@ import com.ajou.hertz.domain.instrument.constant.AmplifierBrand; import com.ajou.hertz.domain.instrument.constant.AmplifierType; import com.ajou.hertz.domain.instrument.constant.AmplifierUsage; +import com.ajou.hertz.domain.instrument.constant.AudioEquipmentType; import com.ajou.hertz.domain.instrument.constant.BassGuitarBrand; import com.ajou.hertz.domain.instrument.constant.BassGuitarPickUp; import com.ajou.hertz.domain.instrument.constant.BassGuitarPreAmplifier; @@ -36,10 +37,10 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AudioEquipmentFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; -import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; import com.ajou.hertz.domain.instrument.entity.Amplifier; import com.ajou.hertz.domain.instrument.entity.AudioEquipment; @@ -228,39 +229,26 @@ public Page findAudioEquipments( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions - ) { - return findInstrumentsByClassType(AudioEquipment.class, page, pageSize, sort, filterConditions); - } - - private Page findInstrumentsByClassType( - Class classType, - int page, - int pageSize, - InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + AudioEquipmentFilterConditions filterConditions ) { PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); - List conditions = new ArrayList<>(); - conditions.add(instrument.instanceOf(classType)); + List conditions = + new ArrayList<>(convertAudioEquipmentFilterConditionsToPredicates(filterConditions)); - List content = queryFactory - .selectFrom(instrument) - .join(instrument.seller, user).fetchJoin() + List content = queryFactory + .selectFrom(audioEquipment) + .join(audioEquipment.seller, user).fetchJoin() .where(conditions.toArray(Predicate[]::new)) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) - .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(instrument))) - .fetch() - .stream() - .map(classType::cast) - .toList(); + .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(audioEquipment))) + .fetch(); long totalCount = Optional.ofNullable( - queryFactory.select(instrument.count()) - .from(instrument) - .join(instrument.seller, user) + queryFactory.select(audioEquipment.count()) + .from(audioEquipment) + .join(audioEquipment.seller, user) .where(conditions.toArray(Predicate[]::new)) .fetchOne() ).orElse(0L); @@ -313,9 +301,9 @@ private List convertAcousticAndClassicGuitarFilterConditionsToPredica AcousticAndClassicGuitarFilterConditions filterConditions ) { List res = new ArrayList<>(); - res.add(applyProgressStatusCondition(filterConditions.getProgress(), bassGuitar.progressStatus)); - res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), bassGuitar.tradeAddress.sido)); - res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), bassGuitar.tradeAddress.sgg)); + res.add(applyProgressStatusCondition(filterConditions.getProgress(), acousticAndClassicGuitar.progressStatus)); + res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), acousticAndClassicGuitar.tradeAddress.sido)); + res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), acousticAndClassicGuitar.tradeAddress.sgg)); res.add(applyAcousticAndClassicGuitarBrandCondition(filterConditions.getBrand())); res.add(applyAcousticAndClassicGuitarModelCondition(filterConditions.getModel())); res.add(applyAcousticAndClassicGuitarWood(filterConditions.getWood())); @@ -327,9 +315,9 @@ private List convertEffectorFilterConditionsToPredicates( EffectorFilterConditions filterConditions ) { List res = new ArrayList<>(); - res.add(applyProgressStatusCondition(filterConditions.getProgress(), bassGuitar.progressStatus)); - res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), bassGuitar.tradeAddress.sido)); - res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), bassGuitar.tradeAddress.sgg)); + res.add(applyProgressStatusCondition(filterConditions.getProgress(), effector.progressStatus)); + res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), effector.tradeAddress.sido)); + res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), effector.tradeAddress.sgg)); res.add(applyEffectorTypeCondition(filterConditions.getType())); res.add(applyEffectorFeatureCondition(filterConditions.getFeature())); return res; @@ -339,15 +327,26 @@ private List convertAmplifierFilterConditionsToPredicates( AmplifierFilterConditions filterConditions ) { List res = new ArrayList<>(); - res.add(applyProgressStatusCondition(filterConditions.getProgress(), bassGuitar.progressStatus)); - res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), bassGuitar.tradeAddress.sido)); - res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), bassGuitar.tradeAddress.sgg)); + res.add(applyProgressStatusCondition(filterConditions.getProgress(), amplifier.progressStatus)); + res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), amplifier.tradeAddress.sido)); + res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), amplifier.tradeAddress.sgg)); res.add(applyAmplifierTypeCondition(filterConditions.getType())); res.add(applyAmplifierBrandCondition(filterConditions.getBrand())); res.add(applyAmplifierUsageCondition(filterConditions.getUsage())); return res; } + private List convertAudioEquipmentFilterConditionsToPredicates( + AudioEquipmentFilterConditions filterConditions + ) { + List res = new ArrayList<>(); + res.add(applyProgressStatusCondition(filterConditions.getProgress(), audioEquipment.progressStatus)); + res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), audioEquipment.tradeAddress.sido)); + res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), audioEquipment.tradeAddress.sgg)); + res.add(applyAudioEquipmentTypeCondition(filterConditions.getType())); + return res; + } + private BooleanExpression applyProgressStatusCondition( InstrumentProgressStatus progressStatus, EnumPath progressStatusExpression @@ -423,6 +422,10 @@ private BooleanExpression applyAmplifierUsageCondition(AmplifierUsage usage) { return createCondition(usage, amplifier.usage); } + private BooleanExpression applyAudioEquipmentTypeCondition(AudioEquipmentType type) { + return createCondition(type, audioEquipment.type); + } + private > BooleanExpression createCondition(T value, ComparableExpression path) { return value != null ? path.eq(value) : null; } diff --git a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java index 648c2fa..82968df 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/service/InstrumentQueryService.java @@ -15,10 +15,10 @@ import com.ajou.hertz.domain.instrument.dto.InstrumentDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AudioEquipmentFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; -import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.repository.InstrumentRepository; import lombok.RequiredArgsConstructor; @@ -95,7 +95,7 @@ public Page findAudioEquipments( int page, int pageSize, InstrumentSortOption sort, - InstrumentFilterConditions filterConditions + AudioEquipmentFilterConditions filterConditions ) { return instrumentRepository .findAudioEquipments(page, pageSize, sort, filterConditions) diff --git a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java index 8e1a01b..ddc536e 100644 --- a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java +++ b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java @@ -37,10 +37,10 @@ import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AudioEquipmentFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; -import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; import com.ajou.hertz.domain.instrument.entity.Amplifier; import com.ajou.hertz.domain.instrument.entity.AudioEquipment; @@ -173,7 +173,7 @@ public InstrumentRepositoryTest( void 음향_장비_목록을_조회한다() throws Exception { // given InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_ASC; - InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); + AudioEquipmentFilterConditions filterConditions = createAudioEquipmentFilterConditions(); User user = userRepository.save(createUser()); List savedInstruments = sut.saveAll(List.of( createElectricGuitar(user), @@ -309,10 +309,6 @@ private AudioEquipment createAudioEquipment(User seller) throws Exception { ); } - private InstrumentFilterConditions createEmptyInstrumentFilterConditions() throws Exception { - return ReflectionUtils.createInstrumentFilterConditions(null, null, null); - } - private ElectricGuitarFilterConditions createEmptyElectricGuitarFilterConditions() throws Exception { return ReflectionUtils.createElectricGuitarFilterConditions(null, null, null, null, null, null); } @@ -333,4 +329,8 @@ private EffectorFilterConditions createEmptyEffectorFilterConditions() throws Ex private AmplifierFilterConditions createEmptyAmplifierFilterConditions() throws Exception { return ReflectionUtils.createAmplifierFilterConditions(null, null, null, null, null, null); } + + private AudioEquipmentFilterConditions createAudioEquipmentFilterConditions() throws Exception { + return ReflectionUtils.createAudioEquipmentFilterConditions(null, null, null, null); + } } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java index 447a066..632eb03 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java @@ -58,6 +58,7 @@ import com.ajou.hertz.domain.instrument.dto.InstrumentImageDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AudioEquipmentFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; @@ -321,7 +322,7 @@ public InstrumentControllerTest(MockMvc mvc) { createAudioEquipmentDto(4L, userId) )); given(instrumentQueryService.findAudioEquipments( - eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class) + eq(page), eq(pageSize), eq(sortOption), any(AudioEquipmentFilterConditions.class) )).willReturn(expectedResult); // when & then @@ -339,7 +340,7 @@ public InstrumentControllerTest(MockMvc mvc) { .andExpect(jsonPath("$.content", hasSize(expectedResult.getNumberOfElements()))); then(instrumentQueryService) .should() - .findAudioEquipments(eq(page), eq(pageSize), eq(sortOption), any(InstrumentFilterConditions.class)); + .findAudioEquipments(eq(page), eq(pageSize), eq(sortOption), any(AudioEquipmentFilterConditions.class)); verifyEveryMocksShouldHaveNoMoreInteractions(); } diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java index 527d263..4f02794 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/service/InstrumentQueryServiceTest.java @@ -46,6 +46,7 @@ import com.ajou.hertz.domain.instrument.dto.InstrumentDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AudioEquipmentFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; @@ -250,7 +251,7 @@ class InstrumentQueryServiceTest { // given int page = 0; int pageSize = 10; - InstrumentFilterConditions filterConditions = createEmptyInstrumentFilterConditions(); + AudioEquipmentFilterConditions filterConditions = createAudioEquipmentFilterConditions(); InstrumentSortOption sort = InstrumentSortOption.CREATED_BY_DESC; User user = createUser(); Page expectedResult = new PageImpl<>(List.of( @@ -481,4 +482,13 @@ private AmplifierFilterConditions createAmplifierFilterConditions() throws Excep AmplifierUsage.HOME ); } + + private AudioEquipmentFilterConditions createAudioEquipmentFilterConditions() throws Exception { + return ReflectionUtils.createAudioEquipmentFilterConditions( + InstrumentProgressStatus.SELLING, + "서울특별시", + "종로구", + AudioEquipmentType.AUDIO_EQUIPMENT + ); + } } diff --git a/src/test/java/com/ajou/hertz/util/ReflectionUtils.java b/src/test/java/com/ajou/hertz/util/ReflectionUtils.java index 08e2d7e..b4a9268 100644 --- a/src/test/java/com/ajou/hertz/util/ReflectionUtils.java +++ b/src/test/java/com/ajou/hertz/util/ReflectionUtils.java @@ -44,6 +44,7 @@ import com.ajou.hertz.domain.instrument.dto.InstrumentImageDto; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.AmplifierFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.AudioEquipmentFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAcousticAndClassicGuitarRequest; import com.ajou.hertz.domain.instrument.dto.request.CreateNewAmplifierRequest; @@ -580,11 +581,12 @@ public static CreateNewElectricGuitarRequest createElectricGuitarRequest( Short productionYear, GuitarColor color ) throws Exception { - Constructor constructor = CreateNewElectricGuitarRequest.class.getDeclaredConstructor( - String.class, List.class, InstrumentProgressStatus.class, AddressRequest.class, - Short.class, Integer.class, Boolean.class, String.class, ElectricGuitarBrand.class, - ElectricGuitarModel.class, Short.class, GuitarColor.class, List.class - ); + Constructor constructor = + CreateNewElectricGuitarRequest.class.getDeclaredConstructor( + String.class, List.class, InstrumentProgressStatus.class, AddressRequest.class, + Short.class, Integer.class, Boolean.class, String.class, ElectricGuitarBrand.class, + ElectricGuitarModel.class, Short.class, GuitarColor.class, List.class + ); constructor.setAccessible(true); return constructor.newInstance( title, images, progressStatus, tradeAddress, qualityStatus, price, hasAnomaly, description, @@ -634,12 +636,13 @@ public static CreateNewAcousticAndClassicGuitarRequest createAcousticAndClassicG AcousticAndClassicGuitarWood wood, AcousticAndClassicGuitarPickUp pickUp ) throws Exception { - Constructor constructor = CreateNewAcousticAndClassicGuitarRequest.class.getDeclaredConstructor( - String.class, InstrumentProgressStatus.class, AddressRequest.class, Short.class, - Integer.class, Boolean.class, String.class, List.class, List.class, - AcousticAndClassicGuitarBrand.class, AcousticAndClassicGuitarModel.class, - AcousticAndClassicGuitarWood.class, AcousticAndClassicGuitarPickUp.class - ); + Constructor constructor = + CreateNewAcousticAndClassicGuitarRequest.class.getDeclaredConstructor( + String.class, InstrumentProgressStatus.class, AddressRequest.class, Short.class, + Integer.class, Boolean.class, String.class, List.class, List.class, + AcousticAndClassicGuitarBrand.class, AcousticAndClassicGuitarModel.class, + AcousticAndClassicGuitarWood.class, AcousticAndClassicGuitarPickUp.class + ); constructor.setAccessible(true); return constructor.newInstance( title, progressStatus, tradeAddress, qualityStatus, price, hasAnomaly, description, images, hashtags, @@ -710,11 +713,12 @@ public static CreateNewAudioEquipmentRequest createAudioEquipmentRequest( List hashtags, AudioEquipmentType type ) throws Exception { - Constructor constructor = CreateNewAudioEquipmentRequest.class.getDeclaredConstructor( - String.class, InstrumentProgressStatus.class, AddressRequest.class, Short.class, - Integer.class, Boolean.class, String.class, List.class, List.class, - AudioEquipmentType.class - ); + Constructor constructor = + CreateNewAudioEquipmentRequest.class.getDeclaredConstructor( + String.class, InstrumentProgressStatus.class, AddressRequest.class, Short.class, + Integer.class, Boolean.class, String.class, List.class, List.class, + AudioEquipmentType.class + ); constructor.setAccessible(true); return constructor.newInstance( title, progressStatus, tradeAddress, qualityStatus, price, hasAnomaly, description, images, hashtags, type @@ -741,10 +745,11 @@ public static ElectricGuitarFilterConditions createElectricGuitarFilterCondition ElectricGuitarModel model, GuitarColor color ) throws Exception { - Constructor constructor = ElectricGuitarFilterConditions.class.getDeclaredConstructor( - InstrumentProgressStatus.class, String.class, String.class, - ElectricGuitarBrand.class, ElectricGuitarModel.class, GuitarColor.class - ); + Constructor constructor = + ElectricGuitarFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class, + ElectricGuitarBrand.class, ElectricGuitarModel.class, GuitarColor.class + ); constructor.setAccessible(true); return constructor.newInstance(progressStatus, sido, sgg, brand, model, color); } @@ -775,11 +780,12 @@ public static AcousticAndClassicGuitarFilterConditions createAcousticAndClassicG AcousticAndClassicGuitarWood wood, AcousticAndClassicGuitarPickUp pickUp ) throws Exception { - Constructor constructor = AcousticAndClassicGuitarFilterConditions.class.getDeclaredConstructor( - InstrumentProgressStatus.class, String.class, String.class, - AcousticAndClassicGuitarBrand.class, AcousticAndClassicGuitarModel.class, - AcousticAndClassicGuitarWood.class, AcousticAndClassicGuitarPickUp.class - ); + Constructor constructor = + AcousticAndClassicGuitarFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class, + AcousticAndClassicGuitarBrand.class, AcousticAndClassicGuitarModel.class, + AcousticAndClassicGuitarWood.class, AcousticAndClassicGuitarPickUp.class + ); constructor.setAccessible(true); return constructor.newInstance(progressStatus, sido, sgg, brand, model, wood, pickUp); } @@ -815,6 +821,20 @@ public static AmplifierFilterConditions createAmplifierFilterConditions( return constructor.newInstance(progressStatus, sido, sgg, type, brand, usage); } + public static AudioEquipmentFilterConditions createAudioEquipmentFilterConditions( + InstrumentProgressStatus progressStatus, + String sido, + String sgg, + AudioEquipmentType type + ) throws Exception { + Constructor constructor = + AudioEquipmentFilterConditions.class.getDeclaredConstructor( + InstrumentProgressStatus.class, String.class, String.class, AudioEquipmentType.class + ); + constructor.setAccessible(true); + return constructor.newInstance(progressStatus, sido, sgg, type); + } + /** * DTO(Response) */ From cb27490bd6c7975d7c105f3254fbd5e9fac05453 Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 21:53:20 +0900 Subject: [PATCH 07/10] =?UTF-8?q?test:=20#67=20`InstrumentRepository`=20ta?= =?UTF-8?q?rget=20test=20coverage=20=EB=8B=AC=EC=84=B1=EC=9D=84=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BC=80?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/InstrumentRepositoryTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java index ddc536e..2d35aad 100644 --- a/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java +++ b/src/test/java/com/ajou/hertz/integration/domain/instrument/repository/InstrumentRepositoryTest.java @@ -92,6 +92,42 @@ public InstrumentRepositoryTest( assertThat(result.getNumberOfElements()).isEqualTo(savedInstruments.size() - 1); } + @Test + void 필터링_조건이_주어지고_일렉_기타_목록을_조회하면_조건에_일치하는_매물이_조회된다() throws Exception { + // given + InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; + ElectricGuitarFilterConditions filterConditions = createElectricGuitarFilterConditions( + InstrumentProgressStatus.SELLING, + ElectricGuitarBrand.FENDER_USA, + ElectricGuitarModel.TELECASTER, + GuitarColor.RED + ); + User user = userRepository.save(createUser()); + sut.saveAll(List.of( + createBassGuitar(user), + createElectricGuitar( + user, + InstrumentProgressStatus.SELLING, + ElectricGuitarBrand.FENDER_JAPAN, + ElectricGuitarModel.TELECASTER, + GuitarColor.BLACK + ), + createElectricGuitar( + user, + InstrumentProgressStatus.SELLING, + ElectricGuitarBrand.FENDER_USA, + ElectricGuitarModel.TELECASTER, + GuitarColor.RED + ) + )); + + // when + Page result = sut.findElectricGuitars(0, 10, sortOption, filterConditions); + + // then + assertThat(result.getNumberOfElements()).isEqualTo(1); + } + @Test void 베이스_기타_목록을_조회한다() throws Exception { // given @@ -207,6 +243,30 @@ private User createUser() throws Exception { ); } + private ElectricGuitar createElectricGuitar( + User seller, + InstrumentProgressStatus progressStatus, + ElectricGuitarBrand brand, + ElectricGuitarModel model, + GuitarColor color + ) throws Exception { + return ReflectionUtils.createElectricGuitar( + null, + seller, + "Test electric guitar", + progressStatus, + createAddress(), + (short)3, + 550000, + true, + "description", + brand, + model, + (short)2014, + color + ); + } + private ElectricGuitar createElectricGuitar(User seller) throws Exception { return ReflectionUtils.createElectricGuitar( null, @@ -313,6 +373,15 @@ private ElectricGuitarFilterConditions createEmptyElectricGuitarFilterConditions return ReflectionUtils.createElectricGuitarFilterConditions(null, null, null, null, null, null); } + private ElectricGuitarFilterConditions createElectricGuitarFilterConditions( + InstrumentProgressStatus progressStatus, + ElectricGuitarBrand brand, + ElectricGuitarModel model, + GuitarColor color + ) throws Exception { + return ReflectionUtils.createElectricGuitarFilterConditions(progressStatus, null, null, brand, model, color); + } + private BassGuitarFilterConditions createEmptyBassGuitarFilterConditions() throws Exception { return ReflectionUtils.createBassGuitarFilterConditions(null, null, null, null, null, null, null); } From b855ee755522b9fe87a00d08849f2cdb6646ecee Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 21:53:35 +0900 Subject: [PATCH 08/10] =?UTF-8?q?docs:=20#67=20=EC=98=88=EC=A0=95=20?= =?UTF-8?q?=EC=9E=91=EC=97=85=20=EC=B7=A8=EC=86=8C=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20TODO=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../instrument/dto/request/InstrumentFilterConditions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/InstrumentFilterConditions.java b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/InstrumentFilterConditions.java index 88fadaf..f899c7c 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/dto/request/InstrumentFilterConditions.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/dto/request/InstrumentFilterConditions.java @@ -13,7 +13,7 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) @Setter // for @ModelAttribute @Getter -public class InstrumentFilterConditions { // TODO: abstract class로 전환 +public class InstrumentFilterConditions { private InstrumentProgressStatus progress; From 31920a6df81b644371a202b6dcc23617ede0d4e8 Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 21:53:54 +0900 Subject: [PATCH 09/10] =?UTF-8?q?refactor:=20#67=20`InstrumentRepository`?= =?UTF-8?q?=20custom=20query=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../InstrumentRepositoryCustomImpl.java | 418 ++++++------------ 1 file changed, 124 insertions(+), 294 deletions(-) diff --git a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java index aa97087..ccbc672 100644 --- a/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java +++ b/src/main/java/com/ajou/hertz/domain/instrument/repository/InstrumentRepositoryCustomImpl.java @@ -11,28 +11,13 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.function.Function; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; -import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarBrand; -import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarModel; -import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarPickUp; -import com.ajou.hertz.domain.instrument.constant.AcousticAndClassicGuitarWood; -import com.ajou.hertz.domain.instrument.constant.AmplifierBrand; -import com.ajou.hertz.domain.instrument.constant.AmplifierType; -import com.ajou.hertz.domain.instrument.constant.AmplifierUsage; -import com.ajou.hertz.domain.instrument.constant.AudioEquipmentType; -import com.ajou.hertz.domain.instrument.constant.BassGuitarBrand; -import com.ajou.hertz.domain.instrument.constant.BassGuitarPickUp; -import com.ajou.hertz.domain.instrument.constant.BassGuitarPreAmplifier; -import com.ajou.hertz.domain.instrument.constant.EffectorFeature; -import com.ajou.hertz.domain.instrument.constant.EffectorType; -import com.ajou.hertz.domain.instrument.constant.ElectricGuitarBrand; -import com.ajou.hertz.domain.instrument.constant.ElectricGuitarModel; -import com.ajou.hertz.domain.instrument.constant.GuitarColor; import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus; import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption; import com.ajou.hertz.domain.instrument.dto.request.AcousticAndClassicGuitarFilterConditions; @@ -41,6 +26,7 @@ import com.ajou.hertz.domain.instrument.dto.request.BassGuitarFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.EffectorFilterConditions; import com.ajou.hertz.domain.instrument.dto.request.ElectricGuitarFilterConditions; +import com.ajou.hertz.domain.instrument.dto.request.InstrumentFilterConditions; import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar; import com.ajou.hertz.domain.instrument.entity.Amplifier; import com.ajou.hertz.domain.instrument.entity.AudioEquipment; @@ -48,6 +34,7 @@ import com.ajou.hertz.domain.instrument.entity.Effector; import com.ajou.hertz.domain.instrument.entity.ElectricGuitar; import com.ajou.hertz.domain.instrument.entity.Instrument; +import com.ajou.hertz.domain.user.entity.User; import com.querydsl.core.types.Order; import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Predicate; @@ -68,188 +55,96 @@ public class InstrumentRepositoryCustomImpl implements InstrumentRepositoryCusto @Override public Page findElectricGuitars( - int page, - int pageSize, - InstrumentSortOption sort, + int page, int pageSize, InstrumentSortOption sort, ElectricGuitarFilterConditions filterConditions ) { - PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); - - List conditions = - new ArrayList<>(convertElectricGuitarFilterConditionsToPredicates(filterConditions)); - - List content = queryFactory - .selectFrom(electricGuitar) - .join(electricGuitar.seller, user).fetchJoin() - .where(conditions.toArray(Predicate[]::new)) - .offset(pageable.getOffset()) - .limit(pageable.getPageSize()) - .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(electricGuitar))) - .fetch(); - - long totalCount = Optional.ofNullable( - queryFactory.select(electricGuitar.count()) - .from(electricGuitar) - .join(electricGuitar.seller, user) - .where(conditions.toArray(Predicate[]::new)) - .fetchOne() - ).orElse(0L); - - return new PageImpl<>(content, pageable, totalCount); + return findInstruments( + page, pageSize, sort, filterConditions, electricGuitar, electricGuitar.seller, + this::mapInstrumentFilterConditionsToPredicates + ); } @Override public Page findBassGuitars( - int page, - int pageSize, - InstrumentSortOption sort, + int page, int pageSize, InstrumentSortOption sort, BassGuitarFilterConditions filterConditions ) { - PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); - - List conditions = - new ArrayList<>(convertBassGuitarFilterConditionsToPredicates(filterConditions)); - - List content = queryFactory - .selectFrom(bassGuitar) - .join(bassGuitar.seller, user).fetchJoin() - .where(conditions.toArray(Predicate[]::new)) - .offset(pageable.getOffset()) - .limit(pageable.getPageSize()) - .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(bassGuitar))) - .fetch(); - - long totalCount = Optional.ofNullable( - queryFactory.select(bassGuitar.count()) - .from(bassGuitar) - .join(bassGuitar.seller, user) - .where(conditions.toArray(Predicate[]::new)) - .fetchOne() - ).orElse(0L); - - return new PageImpl<>(content, pageable, totalCount); + return findInstruments( + page, pageSize, sort, filterConditions, bassGuitar, bassGuitar.seller, + this::mapInstrumentFilterConditionsToPredicates + ); } @Override public Page findAcousticAndClassicGuitars( - int page, - int pageSize, - InstrumentSortOption sort, + int page, int pageSize, InstrumentSortOption sort, AcousticAndClassicGuitarFilterConditions filterConditions ) { - PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); - - List conditions = - new ArrayList<>(convertAcousticAndClassicGuitarFilterConditionsToPredicates(filterConditions)); - - List content = queryFactory - .selectFrom(acousticAndClassicGuitar) - .join(acousticAndClassicGuitar.seller, user).fetchJoin() - .where(conditions.toArray(Predicate[]::new)) - .offset(pageable.getOffset()) - .limit(pageable.getPageSize()) - .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(acousticAndClassicGuitar))) - .fetch(); - - long totalCount = Optional.ofNullable( - queryFactory.select(acousticAndClassicGuitar.count()) - .from(acousticAndClassicGuitar) - .join(acousticAndClassicGuitar.seller, user) - .where(conditions.toArray(Predicate[]::new)) - .fetchOne() - ).orElse(0L); - - return new PageImpl<>(content, pageable, totalCount); + return findInstruments( + page, pageSize, sort, filterConditions, acousticAndClassicGuitar, acousticAndClassicGuitar.seller, + this::mapInstrumentFilterConditionsToPredicates + ); } @Override public Page findEffectors( - int page, - int pageSize, - InstrumentSortOption sort, + int page, int pageSize, InstrumentSortOption sort, EffectorFilterConditions filterConditions ) { - PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); - - List conditions = new ArrayList<>(convertEffectorFilterConditionsToPredicates(filterConditions)); - - List content = queryFactory - .selectFrom(effector) - .join(effector.seller, user).fetchJoin() - .where(conditions.toArray(Predicate[]::new)) - .offset(pageable.getOffset()) - .limit(pageable.getPageSize()) - .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(effector))) - .fetch(); - - long totalCount = Optional.ofNullable( - queryFactory.select(effector.count()) - .from(effector) - .join(effector.seller, user) - .where(conditions.toArray(Predicate[]::new)) - .fetchOne() - ).orElse(0L); - - return new PageImpl<>(content, pageable, totalCount); + return findInstruments( + page, pageSize, sort, filterConditions, effector, effector.seller, + this::mapInstrumentFilterConditionsToPredicates + ); } @Override public Page findAmplifiers( - int page, - int pageSize, - InstrumentSortOption sort, + int page, int pageSize, InstrumentSortOption sort, AmplifierFilterConditions filterConditions ) { - PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); - - List conditions = new ArrayList<>(convertAmplifierFilterConditionsToPredicates(filterConditions)); - - List content = queryFactory - .selectFrom(amplifier) - .join(amplifier.seller, user).fetchJoin() - .where(conditions.toArray(Predicate[]::new)) - .offset(pageable.getOffset()) - .limit(pageable.getPageSize()) - .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(amplifier))) - .fetch(); - - long totalCount = Optional.ofNullable( - queryFactory.select(amplifier.count()) - .from(amplifier) - .join(amplifier.seller, user) - .where(conditions.toArray(Predicate[]::new)) - .fetchOne() - ).orElse(0L); - - return new PageImpl<>(content, pageable, totalCount); + return findInstruments( + page, pageSize, sort, filterConditions, amplifier, amplifier.seller, + this::mapInstrumentFilterConditionsToPredicates + ); } @Override public Page findAudioEquipments( - int page, - int pageSize, - InstrumentSortOption sort, + int page, int pageSize, InstrumentSortOption sort, AudioEquipmentFilterConditions filterConditions + ) { + return findInstruments( + page, pageSize, sort, filterConditions, audioEquipment, audioEquipment.seller, + this::mapInstrumentFilterConditionsToPredicates + ); + } + + private Page findInstruments( + int page, int pageSize, InstrumentSortOption sort, + F filterConditions, + EntityPathBase qInstrument, + EntityPathBase qSeller, + Function> filterConverter ) { PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort()); - List conditions = - new ArrayList<>(convertAudioEquipmentFilterConditionsToPredicates(filterConditions)); + List conditions = new ArrayList<>(filterConverter.apply(filterConditions)); - List content = queryFactory - .selectFrom(audioEquipment) - .join(audioEquipment.seller, user).fetchJoin() - .where(conditions.toArray(Predicate[]::new)) + List content = queryFactory + .selectFrom(qInstrument) + .join(qSeller, user).fetchJoin() + .where(conditions.toArray(new Predicate[0])) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) - .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(audioEquipment))) + .orderBy(convertSortToOrderSpecifiers(pageable.getSort(), createPathBuilder(qInstrument))) .fetch(); long totalCount = Optional.ofNullable( - queryFactory.select(audioEquipment.count()) - .from(audioEquipment) - .join(audioEquipment.seller, user) - .where(conditions.toArray(Predicate[]::new)) + queryFactory + .select(qInstrument.count()) + .from(qInstrument) + .join(qSeller, user) + .where(conditions.toArray(new Predicate[0])) .fetchOne() ).orElse(0L); @@ -261,7 +156,8 @@ private > PathBuilder creat } private OrderSpecifier[] convertSortToOrderSpecifiers( - Sort sort, PathBuilder pathBuilder + Sort sort, + PathBuilder pathBuilder ) { return sort.stream() .map(order -> new OrderSpecifier<>( @@ -270,163 +166,97 @@ private OrderSpecifier[] convertSortToOrderSpecifiers( )).toArray(OrderSpecifier[]::new); } - private List convertElectricGuitarFilterConditionsToPredicates( - ElectricGuitarFilterConditions filterConditions + private List mapInstrumentFilterConditionsToPredicates( + InstrumentFilterConditions filterConditions, + EnumPath progressStatusEntityPath, + StringPath tradeAddressSidoExpression, + StringPath tradeAddressSggExpression ) { List res = new ArrayList<>(); - res.add(applyProgressStatusCondition(filterConditions.getProgress(), electricGuitar.progressStatus)); - res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), electricGuitar.tradeAddress.sido)); - res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), electricGuitar.tradeAddress.sgg)); - res.add(applyElectricGuitarBrandCondition(filterConditions.getBrand())); - res.add(applyElectricGuitarModelCondition(filterConditions.getModel())); - res.add(applyGuitarColorCondition(filterConditions.getColor(), electricGuitar.color)); + res.add(createEqualCondition(filterConditions.getProgress(), progressStatusEntityPath)); + res.add(createEqualCondition(filterConditions.getSido(), tradeAddressSidoExpression)); + res.add(createEqualCondition(filterConditions.getSgg(), tradeAddressSggExpression)); return res; } - private List convertBassGuitarFilterConditionsToPredicates( - BassGuitarFilterConditions filterConditions - ) { - List res = new ArrayList<>(); - res.add(applyProgressStatusCondition(filterConditions.getProgress(), bassGuitar.progressStatus)); - res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), bassGuitar.tradeAddress.sido)); - res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), bassGuitar.tradeAddress.sgg)); - res.add(applyBassGuitarBrandCondition(filterConditions.getBrand())); - res.add(applyBassGuitarPickUpCondition(filterConditions.getPickUp())); - res.add(applyBassGuitarPreAmplifier(filterConditions.getPreAmplifier())); - res.add(applyGuitarColorCondition(filterConditions.getColor(), bassGuitar.color)); + private List mapInstrumentFilterConditionsToPredicates(ElectricGuitarFilterConditions filterConditions) { + List res = mapInstrumentFilterConditionsToPredicates( + filterConditions, + electricGuitar.progressStatus, + electricGuitar.tradeAddress.sido, + electricGuitar.tradeAddress.sgg + ); + res.add(createEqualCondition(filterConditions.getBrand(), electricGuitar.brand)); + res.add(createEqualCondition(filterConditions.getModel(), electricGuitar.model)); + res.add(createEqualCondition(filterConditions.getColor(), electricGuitar.color)); return res; } - private List convertAcousticAndClassicGuitarFilterConditionsToPredicates( - AcousticAndClassicGuitarFilterConditions filterConditions - ) { - List res = new ArrayList<>(); - res.add(applyProgressStatusCondition(filterConditions.getProgress(), acousticAndClassicGuitar.progressStatus)); - res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), acousticAndClassicGuitar.tradeAddress.sido)); - res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), acousticAndClassicGuitar.tradeAddress.sgg)); - res.add(applyAcousticAndClassicGuitarBrandCondition(filterConditions.getBrand())); - res.add(applyAcousticAndClassicGuitarModelCondition(filterConditions.getModel())); - res.add(applyAcousticAndClassicGuitarWood(filterConditions.getWood())); - res.add(applyAcousticAndClassicGuitarPickUpCondition(filterConditions.getPickUp())); + private List mapInstrumentFilterConditionsToPredicates(BassGuitarFilterConditions filterConditions) { + List res = mapInstrumentFilterConditionsToPredicates( + filterConditions, + bassGuitar.progressStatus, + bassGuitar.tradeAddress.sido, + bassGuitar.tradeAddress.sgg + ); + res.add(createEqualCondition(filterConditions.getBrand(), bassGuitar.brand)); + res.add(createEqualCondition(filterConditions.getPickUp(), bassGuitar.pickUp)); + res.add(createEqualCondition(filterConditions.getPreAmplifier(), bassGuitar.preAmplifier)); + res.add(createEqualCondition(filterConditions.getColor(), bassGuitar.color)); return res; } - private List convertEffectorFilterConditionsToPredicates( - EffectorFilterConditions filterConditions - ) { - List res = new ArrayList<>(); - res.add(applyProgressStatusCondition(filterConditions.getProgress(), effector.progressStatus)); - res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), effector.tradeAddress.sido)); - res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), effector.tradeAddress.sgg)); - res.add(applyEffectorTypeCondition(filterConditions.getType())); - res.add(applyEffectorFeatureCondition(filterConditions.getFeature())); + private List mapInstrumentFilterConditionsToPredicates(AcousticAndClassicGuitarFilterConditions filterConditions) { + List res = mapInstrumentFilterConditionsToPredicates( + filterConditions, + acousticAndClassicGuitar.progressStatus, + acousticAndClassicGuitar.tradeAddress.sido, + acousticAndClassicGuitar.tradeAddress.sgg + ); + res.add(createEqualCondition(filterConditions.getBrand(), acousticAndClassicGuitar.brand)); + res.add(createEqualCondition(filterConditions.getModel(), acousticAndClassicGuitar.model)); + res.add(createEqualCondition(filterConditions.getWood(), acousticAndClassicGuitar.wood)); + res.add(createEqualCondition(filterConditions.getPickUp(), acousticAndClassicGuitar.pickUp)); return res; } - private List convertAmplifierFilterConditionsToPredicates( - AmplifierFilterConditions filterConditions - ) { - List res = new ArrayList<>(); - res.add(applyProgressStatusCondition(filterConditions.getProgress(), amplifier.progressStatus)); - res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), amplifier.tradeAddress.sido)); - res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), amplifier.tradeAddress.sgg)); - res.add(applyAmplifierTypeCondition(filterConditions.getType())); - res.add(applyAmplifierBrandCondition(filterConditions.getBrand())); - res.add(applyAmplifierUsageCondition(filterConditions.getUsage())); + private List mapInstrumentFilterConditionsToPredicates(EffectorFilterConditions filterConditions) { + List res = mapInstrumentFilterConditionsToPredicates( + filterConditions, + effector.progressStatus, + effector.tradeAddress.sido, + effector.tradeAddress.sgg + ); + res.add(createEqualCondition(filterConditions.getType(), effector.type)); + res.add(createEqualCondition(filterConditions.getFeature(), effector.feature)); return res; } - private List convertAudioEquipmentFilterConditionsToPredicates( - AudioEquipmentFilterConditions filterConditions - ) { - List res = new ArrayList<>(); - res.add(applyProgressStatusCondition(filterConditions.getProgress(), audioEquipment.progressStatus)); - res.add(applyTradeAddressSidoCondition(filterConditions.getSido(), audioEquipment.tradeAddress.sido)); - res.add(applyTradeAddressSggCondition(filterConditions.getSgg(), audioEquipment.tradeAddress.sgg)); - res.add(applyAudioEquipmentTypeCondition(filterConditions.getType())); + private List mapInstrumentFilterConditionsToPredicates(AmplifierFilterConditions filterConditions) { + List res = mapInstrumentFilterConditionsToPredicates( + filterConditions, + amplifier.progressStatus, + amplifier.tradeAddress.sido, + amplifier.tradeAddress.sgg + ); + res.add(createEqualCondition(filterConditions.getType(), amplifier.type)); + res.add(createEqualCondition(filterConditions.getBrand(), amplifier.brand)); + res.add(createEqualCondition(filterConditions.getUsage(), amplifier.usage)); return res; } - private BooleanExpression applyProgressStatusCondition( - InstrumentProgressStatus progressStatus, - EnumPath progressStatusExpression - ) { - return createCondition(progressStatus, progressStatusExpression); - } - - private BooleanExpression applyTradeAddressSidoCondition(String sido, StringPath tradeAddressSidoExpression) { - return createCondition(sido, tradeAddressSidoExpression); - } - - private BooleanExpression applyTradeAddressSggCondition(String sgg, StringPath tradeAddressSggExpression) { - return createCondition(sgg, tradeAddressSggExpression); - } - - private BooleanExpression applyElectricGuitarBrandCondition(ElectricGuitarBrand brand) { - return createCondition(brand, electricGuitar.brand); - } - - private BooleanExpression applyElectricGuitarModelCondition(ElectricGuitarModel model) { - return createCondition(model, electricGuitar.model); - } - - private BooleanExpression applyBassGuitarBrandCondition(BassGuitarBrand brand) { - return createCondition(brand, bassGuitar.brand); - } - - private BooleanExpression applyBassGuitarPickUpCondition(BassGuitarPickUp pickUp) { - return createCondition(pickUp, bassGuitar.pickUp); - } - - private BooleanExpression applyBassGuitarPreAmplifier(BassGuitarPreAmplifier preAmplifier) { - return createCondition(preAmplifier, bassGuitar.preAmplifier); - } - - private BooleanExpression applyGuitarColorCondition(GuitarColor color, EnumPath colorExpression) { - return createCondition(color, colorExpression); - } - - private BooleanExpression applyAcousticAndClassicGuitarBrandCondition(AcousticAndClassicGuitarBrand brand) { - return createCondition(brand, acousticAndClassicGuitar.brand); - } - - private BooleanExpression applyAcousticAndClassicGuitarModelCondition(AcousticAndClassicGuitarModel model) { - return createCondition(model, acousticAndClassicGuitar.model); - } - - private BooleanExpression applyAcousticAndClassicGuitarWood(AcousticAndClassicGuitarWood wood) { - return createCondition(wood, acousticAndClassicGuitar.wood); - } - - private BooleanExpression applyAcousticAndClassicGuitarPickUpCondition(AcousticAndClassicGuitarPickUp pickUp) { - return createCondition(pickUp, acousticAndClassicGuitar.pickUp); - } - - private BooleanExpression applyEffectorTypeCondition(EffectorType type) { - return createCondition(type, effector.type); - } - - private BooleanExpression applyEffectorFeatureCondition(EffectorFeature feature) { - return createCondition(feature, effector.feature); - } - - private BooleanExpression applyAmplifierTypeCondition(AmplifierType type) { - return createCondition(type, amplifier.type); - } - - private BooleanExpression applyAmplifierBrandCondition(AmplifierBrand brand) { - return createCondition(brand, amplifier.brand); - } - - private BooleanExpression applyAmplifierUsageCondition(AmplifierUsage usage) { - return createCondition(usage, amplifier.usage); - } - - private BooleanExpression applyAudioEquipmentTypeCondition(AudioEquipmentType type) { - return createCondition(type, audioEquipment.type); + private List mapInstrumentFilterConditionsToPredicates(AudioEquipmentFilterConditions filterConditions) { + List res = mapInstrumentFilterConditionsToPredicates( + filterConditions, + audioEquipment.progressStatus, + audioEquipment.tradeAddress.sido, + audioEquipment.tradeAddress.sgg + ); + res.add(createEqualCondition(filterConditions.getType(), audioEquipment.type)); + return res; } - private > BooleanExpression createCondition(T value, ComparableExpression path) { + private > BooleanExpression createEqualCondition(T value, ComparableExpression path) { return value != null ? path.eq(value) : null; } } From 2d80d88a1485cdca7b8575e230641fe37cf2422e Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 12 Mar 2024 23:41:16 +0900 Subject: [PATCH 10/10] =?UTF-8?q?feat:=20#67=20=EC=95=85=EA=B8=B0=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20API=20request=20param?= =?UTF-8?q?eter=EC=97=90=20=EB=8C=80=ED=95=9C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=A1=B0=EA=B1=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/InstrumentControllerTest.java | 60 ++++++++++++++----- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java index 632eb03..ce56e6e 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/instrument/controller/InstrumentControllerTest.java @@ -95,7 +95,7 @@ public InstrumentControllerTest(MockMvc mvc) { } @Test - void 전체_악기_매물_목록을_조회한다() throws Exception { + void 종류_상관_없이_전체_악기_매물_목록을_조회한다() throws Exception { // given long userId = 1L; int page = 0; @@ -116,7 +116,9 @@ public InstrumentControllerTest(MockMvc mvc) { .param("page", String.valueOf(page)) .param("size", String.valueOf(pageSize)) .param("sort", sortOption.name()) - .param("progress", filterConditions.getProgress().name()) + .param("progress", InstrumentProgressStatus.SELLING.name()) + .param("sido", "서울특별시") + .param("sgg", "종로구") ) .andExpect(status().isOk()) .andExpect(jsonPath("$.numberOfElements").value(expectedResult.getNumberOfElements())) @@ -127,12 +129,11 @@ public InstrumentControllerTest(MockMvc mvc) { } @Test - void 일렉_기타_매물_목록을_조회한다() throws Exception { + void 필터링_조건이_주어지고_주어진_조건에_해당하는_일렉_기타_매물_목록을_조회한다() throws Exception { // given long userId = 1L; int page = 0; int pageSize = 10; - InstrumentFilterConditions filterConditions = createInstrumentFilterConditions(); InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC; Page expectedResult = new PageImpl<>(List.of( createElectricGuitarDto(2L, userId), @@ -150,7 +151,12 @@ public InstrumentControllerTest(MockMvc mvc) { .param("page", String.valueOf(page)) .param("size", String.valueOf(pageSize)) .param("sort", sortOption.name()) - .param("progress", filterConditions.getProgress().name()) + .param("progress", InstrumentProgressStatus.SELLING.name()) + .param("sido", "서울특별시") + .param("sgg", "종로구") + .param("brand", ElectricGuitarBrand.FENDER_USA.name()) + .param("model", ElectricGuitarModel.TELECASTER.name()) + .param("color", GuitarColor.RED.name()) ) .andExpect(status().isOk()) .andExpect(jsonPath("$.numberOfElements").value(expectedResult.getNumberOfElements())) @@ -163,7 +169,7 @@ public InstrumentControllerTest(MockMvc mvc) { } @Test - void 베이스_기타_매물_목록을_조회한다() throws Exception { + void 필터링_조건이_주어지고_주어진_조건에_해당하는_베이스_기타_매물_목록을_조회한다() throws Exception { // given long userId = 1L; int page = 0; @@ -186,7 +192,13 @@ public InstrumentControllerTest(MockMvc mvc) { .param("page", String.valueOf(page)) .param("size", String.valueOf(pageSize)) .param("sort", sortOption.name()) - .param("progress", filterConditions.getProgress().name()) + .param("progress", InstrumentProgressStatus.SELLING.name()) + .param("sido", "서울특별시") + .param("sgg", "종로구") + .param("brand", BassGuitarBrand.FENDER.name()) + .param("pickUp", BassGuitarPickUp.ETC.name()) + .param("preAmplifier", BassGuitarPreAmplifier.ACTIVE.name()) + .param("color", GuitarColor.BLUE.name()) ) .andExpect(status().isOk()) .andExpect(jsonPath("$.numberOfElements").value(expectedResult.getNumberOfElements())) @@ -199,7 +211,7 @@ public InstrumentControllerTest(MockMvc mvc) { } @Test - void 어쿠스틱_클래식_기타_매물_목록을_조회한다() throws Exception { + void 필터링_조건이_주어지고_주어진_조건에_해당하는_어쿠스틱_클래식_기타_매물_목록을_조회한다() throws Exception { // given long userId = 1L; int page = 0; @@ -222,7 +234,13 @@ public InstrumentControllerTest(MockMvc mvc) { .param("page", String.valueOf(page)) .param("size", String.valueOf(pageSize)) .param("sort", sortOption.name()) - .param("progress", filterConditions.getProgress().name()) + .param("progress", InstrumentProgressStatus.SELLING.name()) + .param("sido", "서울특별시") + .param("sgg", "종로구") + .param("brand", AcousticAndClassicGuitarBrand.CORT.name()) + .param("model", AcousticAndClassicGuitarModel.JUMBO_BODY.name()) + .param("wood", AcousticAndClassicGuitarWood.SOLID_WOOD.name()) + .param("pickUp", AcousticAndClassicGuitarPickUp.MICROPHONE.name()) ) .andExpect(status().isOk()) .andExpect(jsonPath("$.numberOfElements").value(expectedResult.getNumberOfElements())) @@ -237,7 +255,7 @@ public InstrumentControllerTest(MockMvc mvc) { } @Test - void 이펙터_매물_목록을_조회한다() throws Exception { + void 필터링_조건이_주어지고_주어진_조건에_해당하는_이펙터_매물_목록을_조회한다() throws Exception { // given long userId = 1L; int page = 0; @@ -260,7 +278,11 @@ public InstrumentControllerTest(MockMvc mvc) { .param("page", String.valueOf(page)) .param("size", String.valueOf(pageSize)) .param("sort", sortOption.name()) - .param("progress", filterConditions.getProgress().name()) + .param("progress", InstrumentProgressStatus.SELLING.name()) + .param("sido", "서울특별시") + .param("sgg", "종로구") + .param("type", EffectorType.GUITAR.name()) + .param("feature", EffectorFeature.BASS_COMPRESSOR.name()) ) .andExpect(status().isOk()) .andExpect(jsonPath("$.numberOfElements").value(expectedResult.getNumberOfElements())) @@ -273,7 +295,7 @@ public InstrumentControllerTest(MockMvc mvc) { } @Test - void 앰프_매물_목록을_조회한다() throws Exception { + void 필터링_조건이_주어지고_주어진_조건에_해당하는_앰프_매물_목록을_조회한다() throws Exception { // given long userId = 1L; int page = 0; @@ -296,7 +318,12 @@ public InstrumentControllerTest(MockMvc mvc) { .param("page", String.valueOf(page)) .param("size", String.valueOf(pageSize)) .param("sort", sortOption.name()) - .param("progress", filterConditions.getProgress().name()) + .param("progress", InstrumentProgressStatus.SELLING.name()) + .param("sido", "서울특별시") + .param("sgg", "종로구") + .param("type", AmplifierType.GUITAR.name()) + .param("brand", AmplifierBrand.FENDER.name()) + .param("usage", AmplifierUsage.HOME.name()) ) .andExpect(status().isOk()) .andExpect(jsonPath("$.numberOfElements").value(expectedResult.getNumberOfElements())) @@ -309,7 +336,7 @@ public InstrumentControllerTest(MockMvc mvc) { } @Test - void 음향_장비_매물_목록을_조회한다() throws Exception { + void 필터링_조건이_주어지고_주어진_조건에_해당하는_음향_장비_매물_목록을_조회한다() throws Exception { // given long userId = 1L; int page = 0; @@ -332,7 +359,10 @@ public InstrumentControllerTest(MockMvc mvc) { .param("page", String.valueOf(page)) .param("size", String.valueOf(pageSize)) .param("sort", sortOption.name()) - .param("progress", filterConditions.getProgress().name()) + .param("progress", InstrumentProgressStatus.SELLING.name()) + .param("sido", "서울특별시") + .param("sgg", "종로구") + .param("type", AudioEquipmentType.AUDIO_EQUIPMENT.name()) ) .andExpect(status().isOk()) .andExpect(jsonPath("$.numberOfElements").value(expectedResult.getNumberOfElements()))