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 0ea905c commit f571b74
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package intbyte4.learnsmate.member.controller;

import intbyte4.learnsmate.member.domain.dto.FindSingleStudentDTO;
import intbyte4.learnsmate.member.domain.dto.FindSingleTutorDTO;
import intbyte4.learnsmate.member.domain.dto.MemberFilterRequestDTO;
import intbyte4.learnsmate.member.domain.dto.*;
import intbyte4.learnsmate.member.domain.vo.request.RequestEditMemberVO;
import intbyte4.learnsmate.member.domain.vo.request.RequestFilterMembertVO;
import intbyte4.learnsmate.member.domain.vo.response.ResponseFindStudentDetailVO;
import intbyte4.learnsmate.member.domain.vo.response.ResponseFindTutorDetailVO;
import intbyte4.learnsmate.member.mapper.MemberMapper;
import intbyte4.learnsmate.member.domain.MemberType;
import intbyte4.learnsmate.member.domain.dto.MemberDTO;
import intbyte4.learnsmate.member.domain.vo.request.RequestSaveMemberVO;
import intbyte4.learnsmate.member.domain.vo.response.ResponseFindMemberVO;
import intbyte4.learnsmate.member.service.MemberFacade;
Expand All @@ -36,30 +33,23 @@ public class MemberController {

// 1. 모든 학생 조회(member_flag가 true인 사람 + member_type이 STUDENT)
@GetMapping("/students")
public ResponseEntity<List<ResponseFindMemberVO>> findAllStudent() {
public ResponseEntity<MemberPageResponse<ResponseFindMemberVO>> findAllStudent(
@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "15") int size) {

List<MemberDTO> memberDTOList = memberService.findAllMemberByMemberType(MemberType.STUDENT);

// DTO 리스트를 VO 리스트로 변환
List<ResponseFindMemberVO> responseVOList = memberDTOList.stream()
.map(memberMapper::fromMemberDTOtoResponseFindMemberVO)
.collect(Collectors.toList());

return ResponseEntity.status(HttpStatus.OK).body(responseVOList);
MemberPageResponse<ResponseFindMemberVO> response
= memberService.findAllMemberByMemberType(page, size, MemberType.STUDENT);
return ResponseEntity.ok(response);
}

// 1-2. 모든 강사 조회(member_flag가 true + member_type이 TUTOR)
@GetMapping("/tutors")
public ResponseEntity<List<ResponseFindMemberVO>> findAllTutor() {

List<MemberDTO> memberDTOList = memberService.findAllMemberByMemberType(MemberType.TUTOR);
public ResponseEntity<MemberPageResponse<ResponseFindMemberVO>> findAllTutor(
@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "15") int size) {

// DTO 리스트를 VO 리스트로 변환
List<ResponseFindMemberVO> responseVOList = memberDTOList.stream()
.map(memberMapper::fromMemberDTOtoResponseFindMemberVO)
.collect(Collectors.toList());
MemberPageResponse<ResponseFindMemberVO> response
= memberService.findAllMemberByMemberType(page, size, MemberType.TUTOR);

return ResponseEntity.status(HttpStatus.OK).body(responseVOList);
return ResponseEntity.ok(response);
}

// 2-1. 학생 단건 조회(member flag가 true + member_type이 STUDENT)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package intbyte4.learnsmate.member.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class MemberPageResponse<T> {

private List<T> content;
private long totalElements;
private int totalPages;
private int currentPage;
private int size;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import intbyte4.learnsmate.campaign.domain.vo.request.RequestEditCampaignStudentVO;
import intbyte4.learnsmate.campaign.domain.vo.request.RequestFindCampaignStudentVO;
import intbyte4.learnsmate.member.domain.MemberType;
import intbyte4.learnsmate.member.domain.dto.FindSingleStudentDTO;
import intbyte4.learnsmate.member.domain.dto.FindSingleTutorDTO;
import intbyte4.learnsmate.member.domain.dto.MemberDTO;
Expand All @@ -17,6 +18,7 @@
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class MemberMapper{
Expand Down Expand Up @@ -202,4 +204,22 @@ public MemberFilterRequestDTO fromRequestFilterVOtoMemberFilterRequestDTO(Reques
.createdEndDate(request.getCreatedEndDate())
.build();
}

public ResponseFindMemberVO fromMemberToResponseFindMemberVO(Member member) {
return ResponseFindMemberVO.builder()
.memberCode(member.getMemberCode())
.memberType(member.getMemberType())
.memberEmail(member.getMemberEmail())
.memberPassword(member.getMemberPassword())
.memberName(member.getMemberName())
.memberAge(member.getMemberAge())
.memberPhone(member.getMemberPhone())
.memberAddress(member.getMemberAddress())
.memberBirth(member.getMemberBirth())
.memberFlag(member.getMemberFlag())
.memberDormantStatus(member.getMemberDormantStatus())
.createdAt(member.getCreatedAt())
.updatedAt(member.getUpdatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import intbyte4.learnsmate.member.domain.MemberType;
import intbyte4.learnsmate.member.domain.entity.Member;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Primary
public interface MemberRepository extends JpaRepository<Member, Long>, MemberRepositoryCustom {

List<Member> findByMemberFlagTrueAndMemberType(MemberType memberType);
Page<Member> findByMemberFlagTrueAndMemberType(MemberType memberType, PageRequest pageable);

Member findByMemberFlagTrueAndMemberCodeAndMemberType(Long memberCode, MemberType memberType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import intbyte4.learnsmate.common.exception.CommonException;
import intbyte4.learnsmate.common.exception.StatusEnum;
import intbyte4.learnsmate.member.domain.dto.MemberFilterRequestDTO;
import intbyte4.learnsmate.member.domain.dto.MemberPageResponse;
import intbyte4.learnsmate.member.domain.vo.response.ResponseFindMemberVO;
import intbyte4.learnsmate.member.mapper.MemberMapper;
import intbyte4.learnsmate.member.domain.MemberType;
import intbyte4.learnsmate.member.domain.dto.MemberDTO;
Expand All @@ -11,6 +13,8 @@
import intbyte4.learnsmate.member.repository.MemberRepositoryCustom;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
Expand All @@ -34,16 +38,26 @@ public void saveMember(MemberDTO memberDTO) {
memberRepository.save(member);
}

public List<MemberDTO> findAllMemberByMemberType(MemberType memberType) {
public MemberPageResponse<ResponseFindMemberVO> findAllMemberByMemberType(int page, int size, MemberType memberType) {

List<Member> allMember = memberRepository.findByMemberFlagTrueAndMemberType(memberType);
// Pageable 객체 생성
PageRequest pageable = PageRequest.of(page, size);
// 페이징 처리된 데이터 조회
Page<Member> memberPage = memberRepository.findByMemberFlagTrueAndMemberType(memberType, pageable);

List<MemberDTO> memberDTOList = new ArrayList<>();
for (Member member : allMember) {
memberDTOList.add(memberMapper.fromMembertoMemberDTO(member));
}
// Member -> ResponseFindMemberVO 변환
List<ResponseFindMemberVO> responseVOList = memberPage.getContent().stream()
.map(memberMapper::fromMemberToResponseFindMemberVO)
.collect(Collectors.toList());

return memberDTOList;
// MemberPageResponse 반환
return new MemberPageResponse<>(
responseVOList,
memberPage.getTotalElements(),
memberPage.getTotalPages(),
memberPage.getNumber(),
memberPage.getSize()
);
}

public MemberDTO findByStudentCode(Long memberCode) {
Expand Down

0 comments on commit f571b74

Please sign in to comment.