Skip to content

Commit

Permalink
feat: #67 악기 종류별 목록 조회 API에 판매 진행 상태에 대한 필터 조건 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Wo-ogie committed Mar 10, 2024
1 parent e34855b commit 7889142
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.InstrumentFilterConditions;
import com.ajou.hertz.domain.instrument.dto.response.AcousticAndClassicGuitarResponse;
import com.ajou.hertz.domain.instrument.dto.response.AmplifierResponse;
import com.ajou.hertz.domain.instrument.dto.response.AudioEquipmentResponse;
Expand Down Expand Up @@ -95,10 +96,11 @@ public Page<ElectricGuitarResponse> findElectricGuitarsV1(
) @RequestParam int size,
@Parameter(
description = "정렬 기준"
) @RequestParam InstrumentSortOption sort
) @RequestParam InstrumentSortOption sort,
@ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions
) {
return instrumentQueryService
.findElectricGuitars(page, size, sort)
.findElectricGuitars(page, size, sort, filterConditions)
.map(ElectricGuitarResponse::from);
}

Expand All @@ -118,10 +120,11 @@ public Page<BassGuitarResponse> findBassGuitarsV1(
) @RequestParam int size,
@Parameter(
description = "정렬 기준"
) @RequestParam InstrumentSortOption sort
) @RequestParam InstrumentSortOption sort,
@ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions
) {
return instrumentQueryService
.findBassGuitars(page, size, sort)
.findBassGuitars(page, size, sort, filterConditions)
.map(BassGuitarResponse::from);
}

Expand All @@ -141,10 +144,11 @@ public Page<AcousticAndClassicGuitarResponse> findAcousticAndClassicGuitarsV1(
) @RequestParam int size,
@Parameter(
description = "정렬 기준"
) @RequestParam InstrumentSortOption sort
) @RequestParam InstrumentSortOption sort,
@ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions
) {
return instrumentQueryService
.findAcousticAndClassicGuitars(page, size, sort)
.findAcousticAndClassicGuitars(page, size, sort, filterConditions)
.map(AcousticAndClassicGuitarResponse::from);
}

Expand All @@ -164,19 +168,20 @@ public Page<EffectorResponse> findEffectorsV1(
) @RequestParam int size,
@Parameter(
description = "정렬 기준"
) @RequestParam InstrumentSortOption sort
) @RequestParam InstrumentSortOption sort,
@ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions
) {
return instrumentQueryService
.findEffectors(page, size, sort)
.findEffectors(page, size, sort, filterConditions)
.map(EffectorResponse::from);
}

@Operation(
summary = "앰프 매물 목록 조회",
description = "앰프 매물 목록을 조회합니다."
)
@GetMapping(value = "/audio-equipments", headers = API_VERSION_HEADER_NAME + "=" + 1)
public Page<AudioEquipmentResponse> findAudioEquipmentsV1(
@GetMapping(value = "/amplifiers", headers = API_VERSION_HEADER_NAME + "=" + 1)
public Page<AmplifierResponse> findAmplifiersV1(
@Parameter(
description = "페이지 번호. 0부터 시작합니다.",
example = "0"
Expand All @@ -187,19 +192,20 @@ public Page<AudioEquipmentResponse> findAudioEquipmentsV1(
) @RequestParam int size,
@Parameter(
description = "정렬 기준"
) @RequestParam InstrumentSortOption sort
) @RequestParam InstrumentSortOption sort,
@ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions
) {
return instrumentQueryService
.findAudioEquipments(page, size, sort)
.map(AudioEquipmentResponse::from);
.findAmplifiers(page, size, sort, filterConditions)
.map(AmplifierResponse::from);
}

@Operation(
summary = "음향 장비 매물 목록 조회",
description = "음향 장비 매물 목록을 조회합니다."
)
@GetMapping(value = "/amplifiers", headers = API_VERSION_HEADER_NAME + "=" + 1)
public Page<AmplifierResponse> findAmplifiersV1(
@GetMapping(value = "/audio-equipments", headers = API_VERSION_HEADER_NAME + "=" + 1)
public Page<AudioEquipmentResponse> findAudioEquipmentsV1(
@Parameter(
description = "페이지 번호. 0부터 시작합니다.",
example = "0"
Expand All @@ -210,11 +216,12 @@ public Page<AmplifierResponse> findAmplifiersV1(
) @RequestParam int size,
@Parameter(
description = "정렬 기준"
) @RequestParam InstrumentSortOption sort
) @RequestParam InstrumentSortOption sort,
@ParameterObject @Valid @ModelAttribute InstrumentFilterConditions filterConditions
) {
return instrumentQueryService
.findAmplifiers(page, size, sort)
.map(AmplifierResponse::from);
.findAudioEquipments(page, size, sort, filterConditions)
.map(AudioEquipmentResponse::from);
}

@Operation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ajou.hertz.domain.instrument.dto.request;

import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Setter // for @ModelAttribute
@Getter
public class InstrumentFilterConditions {

private InstrumentProgressStatus progress;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.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;
Expand All @@ -12,17 +13,27 @@

public interface InstrumentRepositoryCustom {

Page<ElectricGuitar> findElectricGuitars(int page, int pageSize, InstrumentSortOption sort);
Page<ElectricGuitar> findElectricGuitars(
int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions
);

Page<BassGuitar> findBassGuitars(int page, int pageSize, InstrumentSortOption sort);
Page<BassGuitar> findBassGuitars(
int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions
);

Page<AcousticAndClassicGuitar> findAcousticAndClassicGuitars(
int page, int pageSize, InstrumentSortOption sort
int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions
);

Page<Effector> findEffectors(int page, int pageSize, InstrumentSortOption sort);
Page<Effector> findEffectors(
int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions
);

Page<Amplifier> findAmplifiers(int page, int pageSize, InstrumentSortOption sort);
Page<Amplifier> findAmplifiers(
int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions
);

Page<AudioEquipment> findAudioEquipments(int page, int pageSize, InstrumentSortOption sort);
Page<AudioEquipment> findAudioEquipments(
int page, int pageSize, InstrumentSortOption sort, InstrumentFilterConditions filterConditions
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.ajou.hertz.domain.instrument.entity.QInstrument.*;
import static com.ajou.hertz.domain.user.entity.QUser.*;

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

Expand All @@ -11,7 +12,9 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import com.ajou.hertz.domain.instrument.constant.InstrumentProgressStatus;
import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption;
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;
Expand All @@ -21,6 +24,8 @@
import com.ajou.hertz.domain.instrument.entity.Instrument;
import com.querydsl.core.types.Order;
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.PathBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;

Expand All @@ -33,49 +38,82 @@ public class InstrumentRepositoryCustomImpl implements InstrumentRepositoryCusto
private final JPAQueryFactory queryFactory;

@Override
public Page<ElectricGuitar> findElectricGuitars(int page, int pageSize, InstrumentSortOption sort) {
return findInstrumentsByClassType(ElectricGuitar.class, page, pageSize, sort);
public Page<ElectricGuitar> findElectricGuitars(
int page,
int pageSize,
InstrumentSortOption sort,
InstrumentFilterConditions filterConditions
) {
return findInstrumentsByClassType(ElectricGuitar.class, page, pageSize, sort, filterConditions);
}

@Override
public Page<BassGuitar> findBassGuitars(int page, int pageSize, InstrumentSortOption sort) {
return findInstrumentsByClassType(BassGuitar.class, page, pageSize, sort);
public Page<BassGuitar> findBassGuitars(
int page,
int pageSize,
InstrumentSortOption sort,
InstrumentFilterConditions filterConditions
) {
return findInstrumentsByClassType(BassGuitar.class, page, pageSize, sort, filterConditions);
}

@Override
public Page<AcousticAndClassicGuitar> findAcousticAndClassicGuitars(
int page, int pageSize, InstrumentSortOption sort
int page,
int pageSize,
InstrumentSortOption sort,
InstrumentFilterConditions filterConditions
) {
return findInstrumentsByClassType(AcousticAndClassicGuitar.class, page, pageSize, sort);
return findInstrumentsByClassType(AcousticAndClassicGuitar.class, page, pageSize, sort, filterConditions);
}

@Override
public Page<Effector> findEffectors(int page, int pageSize, InstrumentSortOption sort) {
return findInstrumentsByClassType(Effector.class, page, pageSize, sort);
public Page<Effector> findEffectors(
int page,
int pageSize,
InstrumentSortOption sort,
InstrumentFilterConditions filterConditions
) {
return findInstrumentsByClassType(Effector.class, page, pageSize, sort, filterConditions);
}

@Override
public Page<Amplifier> findAmplifiers(int page, int pageSize, InstrumentSortOption sort) {
return findInstrumentsByClassType(Amplifier.class, page, pageSize, sort);
public Page<Amplifier> findAmplifiers(
int page,
int pageSize,
InstrumentSortOption sort,
InstrumentFilterConditions filterConditions
) {
return findInstrumentsByClassType(Amplifier.class, page, pageSize, sort, filterConditions);
}

@Override
public Page<AudioEquipment> findAudioEquipments(int page, int pageSize, InstrumentSortOption sort) {
return findInstrumentsByClassType(AudioEquipment.class, page, pageSize, sort);
public Page<AudioEquipment> findAudioEquipments(
int page,
int pageSize,
InstrumentSortOption sort,
InstrumentFilterConditions filterConditions
) {
return findInstrumentsByClassType(AudioEquipment.class, page, pageSize, sort, filterConditions);
}

private <T extends Instrument> Page<T> findInstrumentsByClassType(
Class<T> classType,
int page,
int pageSize,
InstrumentSortOption sort
InstrumentSortOption sort,
InstrumentFilterConditions filterConditions
) {
PageRequest pageable = PageRequest.of(page, pageSize, sort.toSort());

List<Predicate> conditions = new ArrayList<>();
conditions.add(instrument.instanceOf(classType));
conditions.addAll(convertFilterConditions(filterConditions));

List<T> content = queryFactory
.selectFrom(instrument)
.join(instrument.seller, user).fetchJoin()
.where(instrument.instanceOf(classType))
.where(conditions.toArray(Predicate[]::new))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(convertSortToOrderSpecifiers(pageable.getSort()))
Expand All @@ -88,18 +126,28 @@ private <T extends Instrument> Page<T> findInstrumentsByClassType(
queryFactory.select(instrument.count())
.from(instrument)
.join(instrument.seller, user)
.where(instrument.instanceOf(classType))
.where(conditions.toArray(Predicate[]::new))
.fetchOne()
).orElse(0L);

return new PageImpl<>(content, pageable, totalCount);
}

public OrderSpecifier<?>[] convertSortToOrderSpecifiers(Sort sort) {
private OrderSpecifier<?>[] convertSortToOrderSpecifiers(Sort sort) {
return sort.stream()
.map(order -> new OrderSpecifier<>(
order.getDirection().isAscending() ? Order.ASC : Order.DESC,
INSTRUMENT_PATH.getString(order.getProperty())
)).toArray(OrderSpecifier[]::new);
}

private List<Predicate> convertFilterConditions(InstrumentFilterConditions filterConditions) {
List<Predicate> res = new ArrayList<>();
res.add(progressStatusFilterCondition(filterConditions.getProgress()));
return res;
}

private BooleanExpression progressStatusFilterCondition(InstrumentProgressStatus progressStatus) {
return progressStatus != null ? instrument.progressStatus.eq(progressStatus) : null;
}
}
Loading

0 comments on commit 7889142

Please sign in to comment.