Skip to content

Commit

Permalink
✨기능 추가/수정: 멤버 필터링 페이지네이션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellin22 committed Nov 21, 2024
1 parent f571b74 commit a450c3b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import intbyte4.learnsmate.member.domain.vo.response.ResponseFindMemberVO;
import intbyte4.learnsmate.member.service.MemberFacade;
import intbyte4.learnsmate.member.service.MemberService;
import intbyte4.learnsmate.voc.domain.dto.VOCPageResponse;
import intbyte4.learnsmate.voc.domain.vo.response.ResponseFindVOCVO;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -112,35 +114,35 @@ public ResponseEntity<String> deleteMember(@PathVariable("membercode") Long memb

@Operation(summary = "직원 - 학생 필터링 검색")
@PostMapping("/filter/student")
public ResponseEntity<List<ResponseFindMemberVO>> findStudentByFilter(@RequestBody RequestFilterMembertVO request) {
public ResponseEntity<MemberPageResponse<ResponseFindMemberVO>> findStudentByFilter(
@RequestBody RequestFilterMembertVO request,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "15") int size) {

MemberFilterRequestDTO dto =
memberMapper.fromRequestFilterVOtoMemberFilterRequestDTO(request);

dto.setMemberType(MemberType.STUDENT);

List<MemberDTO> memberDTOList = memberService.filterStudent(dto);
MemberPageResponse<ResponseFindMemberVO> response = memberService.filterStudent(dto, page, size);

return ResponseEntity.status(HttpStatus.OK)
.body(memberDTOList.stream()
.map(memberMapper::fromMemberDTOtoResponseFindMemberVO)
.collect(Collectors.toList()));
return ResponseEntity.ok(response);
}

@Operation(summary = "직원 - 강사 필터링 검색")
@PostMapping("/filter/tutor")
public ResponseEntity<?> findTutorByFilter(@RequestBody RequestFilterMembertVO request) {
public ResponseEntity<?> findTutorByFilter(
@RequestBody RequestFilterMembertVO request,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "15") int size) {

MemberFilterRequestDTO dto =
memberMapper.fromRequestFilterVOtoMemberFilterRequestDTO(request);

dto.setMemberType(MemberType.TUTOR);

List<MemberDTO> memberDTOList = memberService.filterTutor(dto);
MemberPageResponse<ResponseFindMemberVO> response = memberService.filterTutor(dto, page, size);

return ResponseEntity.status(HttpStatus.OK)
.body(memberDTOList.stream()
.map(memberMapper::fromMemberDTOtoResponseFindMemberVO)
.collect(Collectors.toList()));
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import intbyte4.learnsmate.member.domain.entity.Member;
import intbyte4.learnsmate.member.domain.dto.MemberFilterRequestDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface MemberRepositoryCustom {
List<Member> searchBy(MemberFilterRequestDTO request);
Page<Member> searchBy(MemberFilterRequestDTO request, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import intbyte4.learnsmate.member.domain.MemberType;
import intbyte4.learnsmate.member.domain.entity.Member;
import intbyte4.learnsmate.member.domain.entity.QMember;
import intbyte4.learnsmate.member.domain.dto.MemberFilterRequestDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -18,26 +22,39 @@ public class MemberRepositoryImpl implements MemberRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public List<Member> searchBy(MemberFilterRequestDTO request) {
public Page<Member> searchBy(MemberFilterRequestDTO request, Pageable pageable) {
QMember member = QMember.member;

// BooleanBuilder를 사용해 필터 조건 생성
BooleanBuilder builder = new BooleanBuilder()
.and(eqMemberCode(request.getMemberCode()))
.and(eqMemberType(request.getMemberType()))
.and(likeEmail(request.getMemberEmail()))
.and(likeName(request.getMemberName()))
.and(betweenAge(request.getMemberStartAge(), request.getMemberEndAge())) // 나이 범위
.and(betweenAge(request.getMemberStartAge(), request.getMemberEndAge()))
.and(likePhone(request.getMemberPhone()))
.and(likeAddress(request.getMemberAddress()))
.and(eqMemberFlag(request.getMemberFlag())) // 멤버 플래그
.and(eqMemberDormantFlag(request.getMemberDormantFlag())) // 휴면 멤버 플래그
.and(betweenBirth(request.getBirthStartDate(), request.getBirthEndDate())) // 생년월일 범위
.and(betweenCreatedAt(request.getCreatedStartDate(), request.getCreatedEndDate())); // 생성일 범위
.and(eqMemberFlag(request.getMemberFlag()))
.and(eqMemberDormantFlag(request.getMemberDormantFlag()))
.and(betweenBirth(request.getBirthStartDate(), request.getBirthEndDate()))
.and(betweenCreatedAt(request.getCreatedStartDate(), request.getCreatedEndDate()));

return queryFactory
// Query 생성
JPAQuery<Member> query = queryFactory
.selectFrom(member)
.where(builder)
.where(builder);

// 전체 데이터 수 조회
long total = query.fetchCount();

// 페이징 적용
List<Member> members = query
.offset(pageable.getOffset()) // 시작 위치
.limit(pageable.getPageSize()) // 페이지 크기
.fetch();

// PageImpl 객체 생성
return new PageImpl<>(members, pageable, total);
}

// memberCode 검색 조건
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
Expand All @@ -29,7 +30,6 @@ public class MemberService {

private final MemberRepository memberRepository;
private final MemberMapper memberMapper;
private final MemberRepositoryCustom memberRepositoryCustom;

public void saveMember(MemberDTO memberDTO) {
LocalDateTime now = LocalDateTime.now();
Expand Down Expand Up @@ -107,20 +107,48 @@ public MemberDTO findById(Long memberCode){
}

// 학생 필터링하는 서비스 코드
public List<MemberDTO> filterStudent(MemberFilterRequestDTO dto){
List<Member> memberList = memberRepositoryCustom.searchBy(dto);
public MemberPageResponse<ResponseFindMemberVO> filterStudent(MemberFilterRequestDTO dto, int page, int size){
// Pageable 객체 생성
Pageable pageable = PageRequest.of(page, size);

// 필터 조건과 페이징 처리된 데이터 조회
Page<Member> memberPage = memberRepository.searchBy(dto, pageable);

return memberList.stream()
.map(memberMapper::fromMembertoMemberDTO)
// DTO 리스트로 변환
List<ResponseFindMemberVO> memberVOList = memberPage.getContent().stream()
.map(memberMapper::fromMemberToResponseFindMemberVO)
.collect(Collectors.toList());

// MemberPageResponse 생성 후 반환
return new MemberPageResponse<>(
memberVOList, // 데이터 리스트
memberPage.getTotalElements(), // 전체 데이터 수
memberPage.getTotalPages(), // 전체 페이지 수
memberPage.getNumber() + 1, // 현재 페이지 (0-based → 1-based)
memberPage.getSize() // 페이지 크기
);
}

// 강사 필터링하는 코드 -> 강사 필터링은 조건이 더 적음(멤버에 다 포함됨)
public List<MemberDTO> filterTutor(MemberFilterRequestDTO dto) {
List<Member> memberList = memberRepositoryCustom.searchBy(dto);
public MemberPageResponse<ResponseFindMemberVO> filterTutor(MemberFilterRequestDTO dto, int page, int size){
// Pageable 객체 생성
Pageable pageable = PageRequest.of(page, size);

// 필터 조건과 페이징 처리된 데이터 조회
Page<Member> memberPage = memberRepository.searchBy(dto, pageable);

return memberList.stream()
.map(memberMapper::fromMembertoMemberDTO)
// DTO 리스트로 변환
List<ResponseFindMemberVO> memberVOList = memberPage.getContent().stream()
.map(memberMapper::fromMemberToResponseFindMemberVO)
.collect(Collectors.toList());

// MemberPageResponse 생성 후 반환
return new MemberPageResponse<>(
memberVOList, // 데이터 리스트
memberPage.getTotalElements(), // 전체 데이터 수
memberPage.getTotalPages(), // 전체 페이지 수
memberPage.getNumber() + 1, // 현재 페이지 (0-based → 1-based)
memberPage.getSize() // 페이지 크기
);
}
}

0 comments on commit a450c3b

Please sign in to comment.