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 3a7457d commit e34855b
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class SecurityConfig {
AUTH_WHITE_LIST.put("/api/instruments/acoustic-and-classic-guitars", GET);
AUTH_WHITE_LIST.put("/api/instruments/effectors", GET);
AUTH_WHITE_LIST.put("/api/instruments/amplifiers", GET);
AUTH_WHITE_LIST.put("/api/instruments/audio-equipments", GET);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,29 @@ public Page<EffectorResponse> findEffectorsV1(
summary = "앰프 매물 목록 조회",
description = "앰프 매물 목록을 조회합니다."
)
@GetMapping(value = "/audio-equipments", headers = API_VERSION_HEADER_NAME + "=" + 1)
public Page<AudioEquipmentResponse> findAudioEquipmentsV1(
@Parameter(
description = "페이지 번호. 0부터 시작합니다.",
example = "0"
) @RequestParam int page,
@Parameter(
description = "페이지 크기. 한 페이지에 포함될 데이터의 개수를 의미합니다.",
example = "10"
) @RequestParam int size,
@Parameter(
description = "정렬 기준"
) @RequestParam InstrumentSortOption sort
) {
return instrumentQueryService
.findAudioEquipments(page, size, sort)
.map(AudioEquipmentResponse::from);
}

@Operation(
summary = "음향 장비 매물 목록 조회",
description = "음향 장비 매물 목록을 조회합니다."
)
@GetMapping(value = "/amplifiers", headers = API_VERSION_HEADER_NAME + "=" + 1)
public Page<AmplifierResponse> findAmplifiersV1(
@Parameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption;
import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar;
import com.ajou.hertz.domain.instrument.entity.Amplifier;
import com.ajou.hertz.domain.instrument.entity.AudioEquipment;
import com.ajou.hertz.domain.instrument.entity.BassGuitar;
import com.ajou.hertz.domain.instrument.entity.Effector;
import com.ajou.hertz.domain.instrument.entity.ElectricGuitar;
Expand All @@ -22,4 +23,6 @@ Page<AcousticAndClassicGuitar> findAcousticAndClassicGuitars(
Page<Effector> findEffectors(int page, int pageSize, InstrumentSortOption sort);

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

Page<AudioEquipment> findAudioEquipments(int page, int pageSize, InstrumentSortOption sort);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption;
import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar;
import com.ajou.hertz.domain.instrument.entity.Amplifier;
import com.ajou.hertz.domain.instrument.entity.AudioEquipment;
import com.ajou.hertz.domain.instrument.entity.BassGuitar;
import com.ajou.hertz.domain.instrument.entity.Effector;
import com.ajou.hertz.domain.instrument.entity.ElectricGuitar;
Expand Down Expand Up @@ -58,6 +59,11 @@ public Page<Amplifier> findAmplifiers(int page, int pageSize, InstrumentSortOpti
return findInstrumentsByClassType(Amplifier.class, page, pageSize, sort);
}

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

private <T extends Instrument> Page<T> findInstrumentsByClassType(
Class<T> classType,
int page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption;
import com.ajou.hertz.domain.instrument.dto.AcousticAndClassicGuitarDto;
import com.ajou.hertz.domain.instrument.dto.AmplifierDto;
import com.ajou.hertz.domain.instrument.dto.AudioEquipmentDto;
import com.ajou.hertz.domain.instrument.dto.BassGuitarDto;
import com.ajou.hertz.domain.instrument.dto.EffectorDto;
import com.ajou.hertz.domain.instrument.dto.ElectricGuitarDto;
Expand Down Expand Up @@ -60,4 +61,10 @@ public Page<AmplifierDto> findAmplifiers(int page, int pageSize, InstrumentSortO
.findAmplifiers(page, pageSize, sort)
.map(AmplifierDto::from);
}

public Page<AudioEquipmentDto> findAudioEquipments(int page, int pageSize, InstrumentSortOption sort) {
return instrumentRepository
.findAudioEquipments(page, pageSize, sort)
.map(AudioEquipmentDto::from);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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;
Expand All @@ -37,6 +38,7 @@
import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption;
import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar;
import com.ajou.hertz.domain.instrument.entity.Amplifier;
import com.ajou.hertz.domain.instrument.entity.AudioEquipment;
import com.ajou.hertz.domain.instrument.entity.BassGuitar;
import com.ajou.hertz.domain.instrument.entity.Effector;
import com.ajou.hertz.domain.instrument.entity.ElectricGuitar;
Expand Down Expand Up @@ -152,6 +154,23 @@ public InstrumentRepositoryCustomImplTest(
assertThat(result.getNumberOfElements()).isEqualTo(savedInstruments.size() - 1);
}

@Test
void 음향_장비_목록을_조회한다() throws Exception {
// given
User user = userRepository.save(createUser());
List<Instrument> savedInstruments = sut.saveAll(List.of(
createElectricGuitar(user),
createAudioEquipment(user),
createAudioEquipment(user)
));

// when
Page<AudioEquipment> result = sut.findAudioEquipments(0, 10, InstrumentSortOption.CREATED_BY_ASC);

// then
assertThat(result.getNumberOfElements()).isEqualTo(savedInstruments.size() - 1);
}

private Address createAddress() {
return new Address("서울특별시", "강남구", "청담동");
}
Expand Down Expand Up @@ -294,4 +313,25 @@ private Amplifier createAmplifier(User seller) throws Exception {
AmplifierUsage.HOME
);
}

private AudioEquipment createAudioEquipment(User seller) throws Exception {
Constructor<AudioEquipment> audioEquipmentConstructor = AudioEquipment.class.getDeclaredConstructor(
Long.class, User.class, String.class, InstrumentProgressStatus.class, Address.class,
Short.class, Integer.class, Boolean.class, String.class,
AudioEquipmentType.class
);
audioEquipmentConstructor.setAccessible(true);
return audioEquipmentConstructor.newInstance(
null,
seller,
"Title",
InstrumentProgressStatus.SELLING,
createAddress(),
(short)3,
550000,
true,
"description",
AudioEquipmentType.AUDIO_EQUIPMENT
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,36 @@ public InstrumentControllerTest(MockMvc mvc) {
verifyEveryMocksShouldHaveNoMoreInteractions();
}

@Test
void 음향_장비_매물_목록을_조회한다() throws Exception {
// given
long userId = 1L;
int page = 0;
int pageSize = 10;
InstrumentSortOption sortOption = InstrumentSortOption.CREATED_BY_DESC;
Page<AudioEquipmentDto> expectedResult = new PageImpl<>(List.of(
createAudioEquipmentDto(2L, userId),
createAudioEquipmentDto(3L, userId),
createAudioEquipmentDto(4L, userId)
));
given(instrumentQueryService.findAudioEquipments(page, pageSize, sortOption)).willReturn(expectedResult);

// when & then
mvc.perform(
get("/api/instruments/audio-equipments")
.header(API_VERSION_HEADER_NAME, 1)
.param("page", String.valueOf(page))
.param("size", String.valueOf(pageSize))
.param("sort", sortOption.name())
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.numberOfElements").value(expectedResult.getNumberOfElements()))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content", hasSize(expectedResult.getNumberOfElements())));
then(instrumentQueryService).should().findAudioEquipments(page, pageSize, sortOption);
verifyEveryMocksShouldHaveNoMoreInteractions();
}

@Test
void 새로_등록할_일렉기타의_정보가_주어지면_일렉기타_매물을_등록한다() throws Exception {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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;
Expand All @@ -39,12 +40,14 @@
import com.ajou.hertz.domain.instrument.constant.InstrumentSortOption;
import com.ajou.hertz.domain.instrument.dto.AcousticAndClassicGuitarDto;
import com.ajou.hertz.domain.instrument.dto.AmplifierDto;
import com.ajou.hertz.domain.instrument.dto.AudioEquipmentDto;
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.InstrumentDto;
import com.ajou.hertz.domain.instrument.entity.AcousticAndClassicGuitar;
import com.ajou.hertz.domain.instrument.entity.Amplifier;
import com.ajou.hertz.domain.instrument.entity.AudioEquipment;
import com.ajou.hertz.domain.instrument.entity.BassGuitar;
import com.ajou.hertz.domain.instrument.entity.Effector;
import com.ajou.hertz.domain.instrument.entity.ElectricGuitar;
Expand Down Expand Up @@ -227,6 +230,33 @@ class InstrumentQueryServiceTest {
);
}

@Test
void 음향_장비_목록을_조회한다() throws Exception {
// given
int page = 0;
int pageSize = 10;
InstrumentSortOption sort = InstrumentSortOption.CREATED_BY_DESC;
User user = createUser();
Page<AudioEquipment> expectedResult = new PageImpl<>(List.of(
createAudioEquipment(1L, user),
createAudioEquipment(2L, user),
createAudioEquipment(3L, user)
));
given(instrumentRepository.findAudioEquipments(page, pageSize, sort)).willReturn(expectedResult);

// when
Page<AudioEquipmentDto> actualResult = sut.findAudioEquipments(page, pageSize, sort);

// then
then(instrumentRepository).should().findAudioEquipments(page, pageSize, sort);
verifyEveryMocksShouldHaveNoMoreInteractions();
assertThat(actualResult.getNumberOfElements()).isEqualTo(actualResult.getNumberOfElements());
assertIterableEquals(
expectedResult.getContent().stream().map(AudioEquipment::getId).toList(),
actualResult.getContent().stream().map(AudioEquipmentDto::getId).toList()
);
}

private void verifyEveryMocksShouldHaveNoMoreInteractions() {
then(instrumentRepository).shouldHaveNoMoreInteractions();
}
Expand Down Expand Up @@ -398,6 +428,27 @@ private Amplifier createAmplifier(long id, User seller) throws Exception {
);
}

private AudioEquipment createAudioEquipment(long id, User seller) throws Exception {
Constructor<AudioEquipment> audioEquipmentConstructor = AudioEquipment.class.getDeclaredConstructor(
Long.class, User.class, String.class, InstrumentProgressStatus.class, Address.class,
Short.class, Integer.class, Boolean.class, String.class,
AudioEquipmentType.class
);
audioEquipmentConstructor.setAccessible(true);
return audioEquipmentConstructor.newInstance(
id,
seller,
"Title",
InstrumentProgressStatus.SELLING,
createAddress(),
(short)3,
550000,
true,
"description",
AudioEquipmentType.AUDIO_EQUIPMENT
);
}

private User createUser() throws Exception {
return createUser(1L);
}
Expand Down

0 comments on commit e34855b

Please sign in to comment.