Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package site.icebang.common.dto;

import lombok.Data;

@Data
public class PageParams {
private int current = 1;
private int pageSize = 10;
private String search;
private String[] sorters;
private String[] filters;

// 계산된 offset
public int getOffset() {
return (current - 1) * pageSize;
}

public boolean hasSearch() {
return search != null && !search.trim().isEmpty();
}

public boolean hasSorters() {
return sorters != null && sorters.length > 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package site.icebang.common.dto;

import java.util.List;
import java.util.function.Supplier;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class PageResult<T> {
private List<T> data;
private int total;
private int current;
private int pageSize;
private int totalPages;
private boolean hasNext;
private boolean hasPrevious;

public PageResult(List<T> data, int total, int current, int pageSize) {
this.data = data;
this.total = total;
this.current = current;
this.pageSize = pageSize;
calculatePagination();
}

// 페이징 계산 로직 분리
private void calculatePagination() {
this.totalPages = total > 0 ? (int) Math.ceil((double) total / pageSize) : 0;
this.hasNext = current < totalPages;
this.hasPrevious = current > 1;
}

// 기존 of 메서드
public static <T> PageResult<T> of(List<T> data, int total, int current, int pageSize) {
return new PageResult<>(data, total, current, pageSize);
}

// PageParams를 받는 of 메서드
public static <T> PageResult<T> of(List<T> data, int total, PageParams pageParams) {
return new PageResult<>(data, total, pageParams.getCurrent(), pageParams.getPageSize());
}

// 함수형 인터페이스를 활용한 from 메서드 (트랜잭션 내에서 실행)
public static <T> PageResult<T> from(
PageParams pageParams, Supplier<List<T>> dataSupplier, Supplier<Integer> countSupplier) {
List<T> data = dataSupplier.get();
int total = countSupplier.get();
return new PageResult<>(data, total, pageParams.getCurrent(), pageParams.getPageSize());
}

// 빈 페이지 결과 생성
public static <T> PageResult<T> empty(PageParams pageParams) {
return new PageResult<>(List.of(), 0, pageParams.getCurrent(), pageParams.getPageSize());
}

// 빈 페이지 결과 생성 (기본값)
public static <T> PageResult<T> empty() {
return new PageResult<>(List.of(), 0, 1, 10);
}

// 데이터가 있는지 확인
public boolean hasData() {
return data != null && !data.isEmpty();
}

// 첫 번째 페이지인지 확인
public boolean isFirstPage() {
return current == 1;
}

// 마지막 페이지인지 확인
public boolean isLastPage() {
return current == totalPages;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package site.icebang.common.service;

import site.icebang.common.dto.PageParams;
import site.icebang.common.dto.PageResult;

public interface PageableService<T> {
PageResult<T> getPagedResult(PageParams pageParams);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package site.icebang.domain.workflow.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;

import site.icebang.common.dto.ApiResponse;
import site.icebang.common.dto.PageParams;
import site.icebang.common.dto.PageResult;
import site.icebang.domain.workflow.dto.WorkflowCardDto;
import site.icebang.domain.workflow.service.WorkflowService;

@RestController
@RequestMapping("/v0/workflows")
@RequiredArgsConstructor
public class WorkflowController {
private final WorkflowService workflowService;

@GetMapping("")
public ApiResponse<PageResult<WorkflowCardDto>> getWorkflowList(
@ModelAttribute PageParams pageParams) {
PageResult<WorkflowCardDto> result = workflowService.getPagedResult(pageParams);
return ApiResponse.success(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package site.icebang.domain.workflow.dto;

import lombok.Data;

@Data
public class WorkflowCardDto {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package site.icebang.domain.workflow.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

import site.icebang.common.dto.PageParams;
import site.icebang.common.dto.PageResult;
import site.icebang.common.service.PageableService;
import site.icebang.domain.workflow.dto.WorkflowCardDto;

@Service
@RequiredArgsConstructor
public class WorkflowService implements PageableService<WorkflowCardDto> {

@Override
@Transactional(readOnly = true)
public PageResult<WorkflowCardDto> getPagedResult(PageParams pageParams) {
throw new RuntimeException("Not implemented");
// return PageResult.from(
// pageParams,
// () -> workflowMapper.selectWorkflowList(pageParams),
// () -> workflowMapper.selectWorkflowCount(pageParams)
// );
}
}
Loading