Skip to content

Commit

Permalink
Merge pull request #34 from dnd-side-project/feat/api-pagination
Browse files Browse the repository at this point in the history
Feat/api pagination
  • Loading branch information
hyh1016 authored Mar 13, 2023
2 parents d9fc07a + 343585f commit f4377bf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ public class StoreController {
@GetMapping()
public ResponseEntity<BaseResponseDto<List<StoreResponseDto>>> getAll(@RequestParam(name = "cond", required = false) String condition) throws JsonProcessingException {
try {
StoreConditionDto conditionDto = objectMapper.readValue(condition, StoreConditionDto.class);
StoreConditionDto conditionDto;
if (condition == null) {
conditionDto = new StoreConditionDto();
} else {
conditionDto = objectMapper.readValue(condition, StoreConditionDto.class);
}
return ResponseEntity.ok(BaseResponseDto.ok(storeService.getAll(conditionDto)));
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("유효하지 않은 필터링 형식 cond=" + condition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ public class StoreConditionDto {
@Builder.Default
private String sort = "latest";

@Builder.Default
private int page = 0;

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.team9ookie.dangdo.entity.QMenu;
import com.team9ookie.dangdo.entity.QReview;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -27,7 +28,7 @@ public class CustomStoreRepository {

private final JPQLQueryFactory queryFactory;

public List<StoreDetailDto> getStoreListByCondition(StoreConditionDto condition) {
public List<StoreDetailDto> getStoreListByCondition(StoreConditionDto condition, Pageable pageable) {

QMenu menu = QMenu.menu;
QReview review = QReview.review;
Expand Down Expand Up @@ -85,10 +86,16 @@ public List<StoreDetailDto> getStoreListByCondition(StoreConditionDto condition)
default -> query;
};

return query.fetch();
List<StoreDetailDto> result = query
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

return result;
}

private BooleanExpression searchFiltering(String search) {
if (search == null) return null;
return store.name.like("%" + search + "%");
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/team9ookie/dangdo/service/StoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
import com.team9ookie.dangdo.entity.Store;
import com.team9ookie.dangdo.entity.StoreLink;
import com.team9ookie.dangdo.repository.*;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

@Service
@Slf4j
Expand All @@ -41,7 +40,8 @@ public class StoreService {

@Transactional(readOnly = true)
public List<StoreResponseDto> getAll(StoreConditionDto conditionDto) {
List<StoreDetailDto> storeDetailDtoList = customStoreRepository.getStoreListByCondition(conditionDto);
Pageable pageable = PageRequest.of(conditionDto.getPage(), 10);
List<StoreDetailDto> storeDetailDtoList = customStoreRepository.getStoreListByCondition(conditionDto, pageable);

return storeDetailDtoList.stream().map(dto -> {
// 업체와 연결된 링크, 파일
Expand Down

0 comments on commit f4377bf

Please sign in to comment.