Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 쿼리 메서드 이름 개선 및 연관 로직 주석 추가 #274

Merged
merged 4 commits into from
Mar 6, 2024
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
Expand Up @@ -4,7 +4,7 @@
import com.gdschongik.gdsc.domain.member.domain.RequirementStatus;
import com.gdschongik.gdsc.domain.member.dto.request.MemberGrantRequest;
import com.gdschongik.gdsc.domain.member.dto.request.MemberPaymentRequest;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryRequest;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryOption;
import com.gdschongik.gdsc.domain.member.dto.request.MemberUpdateRequest;
import com.gdschongik.gdsc.domain.member.dto.response.AdminMemberResponse;
import com.gdschongik.gdsc.domain.member.dto.response.MemberGrantResponse;
Expand Down Expand Up @@ -37,8 +37,8 @@ public class AdminMemberController {

@Operation(summary = "전체 회원 목록 조회", description = "전체 회원 목록을 조회합니다.")
@GetMapping
public ResponseEntity<Page<AdminMemberResponse>> getMembers(MemberQueryRequest queryRequest, Pageable pageable) {
Page<AdminMemberResponse> response = adminMemberService.findAll(queryRequest, pageable);
public ResponseEntity<Page<AdminMemberResponse>> getMembers(MemberQueryOption queryOption, Pageable pageable) {
Page<AdminMemberResponse> response = adminMemberService.findAll(queryOption, pageable);
return ResponseEntity.ok().body(response);
}

Expand All @@ -52,8 +52,8 @@ public ResponseEntity<Void> withdrawMember(@PathVariable Long memberId) {
@Operation(summary = "대기중인 회원 목록 조회", description = "대기중인 회원 목록을 조회합니다.")
@GetMapping("/pending")
public ResponseEntity<Page<AdminMemberResponse>> getPendingMembers(
MemberQueryRequest queryRequest, Pageable pageable) {
Page<AdminMemberResponse> response = adminMemberService.findAllPendingMembers(queryRequest, pageable);
MemberQueryOption queryOption, Pageable pageable) {
Page<AdminMemberResponse> response = adminMemberService.findAllPendingMembers(queryOption, pageable);
return ResponseEntity.ok().body(response);
}

Expand All @@ -75,19 +75,19 @@ public ResponseEntity<MemberGrantResponse> grantMember(@Valid @RequestBody Membe
@Operation(summary = "승인 가능 회원 전체 조회", description = "승인 가능한 회원 전체를 조회합니다.")
@GetMapping("/grantable")
public ResponseEntity<Page<AdminMemberResponse>> getGrantableMembers(
MemberQueryRequest queryRequest, Pageable pageable) {
Page<AdminMemberResponse> response = adminMemberService.getGrantableMembers(queryRequest, pageable);
MemberQueryOption queryOption, Pageable pageable) {
Page<AdminMemberResponse> response = adminMemberService.getGrantableMembers(queryOption, pageable);
return ResponseEntity.ok().body(response);
}

@Operation(summary = "회비 납부 상태에 따른 회원 전체 조회", description = "회비 납부 상태에 따라 회원 목록을 조회합니다.")
@GetMapping("/payment")
public ResponseEntity<Page<AdminMemberResponse>> getMembersByPaymentStatus(
MemberQueryRequest queryRequest,
MemberQueryOption queryOption,
@RequestParam(name = "status", required = false) RequirementStatus paymentStatus,
Pageable pageable) {
Page<AdminMemberResponse> response =
adminMemberService.getMembersByPaymentStatus(queryRequest, paymentStatus, pageable);
adminMemberService.getMembersByPaymentStatus(queryOption, paymentStatus, pageable);
return ResponseEntity.ok().body(response);
}

Expand All @@ -102,8 +102,8 @@ public ResponseEntity<Void> updatePayment(
@Operation(summary = "승인된 회원 전체 조회", description = "승인된 회원 전체를 조회합니다.")
@GetMapping("/granted")
public ResponseEntity<Page<AdminMemberResponse>> getGrantedMembers(
MemberQueryRequest queryRequest, Pageable pageable) {
Page<AdminMemberResponse> response = adminMemberService.findAllGrantedMembers(queryRequest, pageable);
MemberQueryOption queryOption, Pageable pageable) {
Page<AdminMemberResponse> response = adminMemberService.findAllGrantedMembers(queryOption, pageable);
return ResponseEntity.ok().body(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.gdschongik.gdsc.domain.member.domain.RequirementStatus;
import com.gdschongik.gdsc.domain.member.dto.request.MemberGrantRequest;
import com.gdschongik.gdsc.domain.member.dto.request.MemberPaymentRequest;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryRequest;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryOption;
import com.gdschongik.gdsc.domain.member.dto.request.MemberUpdateRequest;
import com.gdschongik.gdsc.domain.member.dto.response.AdminMemberResponse;
import com.gdschongik.gdsc.domain.member.dto.response.MemberGrantResponse;
Expand All @@ -32,8 +32,8 @@ public class AdminMemberService {
private final MemberRepository memberRepository;
private final ExcelUtil excelUtil;

public Page<AdminMemberResponse> findAll(MemberQueryRequest queryRequest, Pageable pageable) {
Page<Member> members = memberRepository.findAllByRole(queryRequest, pageable, null);
public Page<AdminMemberResponse> findAll(MemberQueryOption queryOption, Pageable pageable) {
Page<Member> members = memberRepository.findAllByRole(queryOption, pageable, null);
return members.map(AdminMemberResponse::from);
}

Expand All @@ -57,8 +57,8 @@ public void updateMember(Long memberId, MemberUpdateRequest request) {
request.nickname());
}

public Page<AdminMemberResponse> findAllPendingMembers(MemberQueryRequest queryRequest, Pageable pageable) {
Page<Member> members = memberRepository.findAllByRole(queryRequest, pageable, GUEST);
public Page<AdminMemberResponse> findAllPendingMembers(MemberQueryOption queryOption, Pageable pageable) {
Page<Member> members = memberRepository.findAllByRole(queryOption, pageable, GUEST);
return members.map(AdminMemberResponse::from);
}

Expand All @@ -70,14 +70,14 @@ public MemberGrantResponse grantMember(MemberGrantRequest request) {
return MemberGrantResponse.from(classifiedMember);
}

public Page<AdminMemberResponse> getGrantableMembers(MemberQueryRequest queryRequest, Pageable pageable) {
Page<Member> members = memberRepository.findAllGrantable(queryRequest, pageable);
public Page<AdminMemberResponse> getGrantableMembers(MemberQueryOption queryOption, Pageable pageable) {
Page<Member> members = memberRepository.findAllGrantable(queryOption, pageable);
return members.map(AdminMemberResponse::from);
}

public Page<AdminMemberResponse> getMembersByPaymentStatus(
MemberQueryRequest queryRequest, RequirementStatus paymentStatus, Pageable pageable) {
Page<Member> members = memberRepository.findAllByPaymentStatus(queryRequest, paymentStatus, pageable);
MemberQueryOption queryOption, RequirementStatus paymentStatus, Pageable pageable) {
Page<Member> members = memberRepository.findAllByPaymentStatus(queryOption, paymentStatus, pageable);
return members.map(AdminMemberResponse::from);
}

Expand All @@ -87,8 +87,8 @@ public void updatePaymentStatus(Long memberId, MemberPaymentRequest request) {
member.updatePaymentStatus(request.status());
}

public Page<AdminMemberResponse> findAllGrantedMembers(MemberQueryRequest queryRequest, Pageable pageable) {
Page<Member> members = memberRepository.findAllByRole(queryRequest, pageable, USER);
public Page<AdminMemberResponse> findAllGrantedMembers(MemberQueryOption queryOption, Pageable pageable) {
Page<Member> members = memberRepository.findAllByRole(queryOption, pageable, USER);
return members.map(AdminMemberResponse::from);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.member.domain.MemberRole;
import com.gdschongik.gdsc.domain.member.domain.RequirementStatus;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryRequest;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryOption;
import jakarta.annotation.Nullable;
import java.util.List;
import java.util.Map;
Expand All @@ -14,12 +14,12 @@
public interface MemberCustomRepository {
Optional<Member> findNormalByOauthId(String oauthId);

Page<Member> findAllGrantable(MemberQueryRequest queryRequest, Pageable pageable);
Page<Member> findAllGrantable(MemberQueryOption queryOption, Pageable pageable);

Page<Member> findAllByRole(MemberQueryRequest queryRequest, Pageable pageable, @Nullable MemberRole role);
Page<Member> findAllByRole(MemberQueryOption queryOption, Pageable pageable, @Nullable MemberRole role);

Page<Member> findAllByPaymentStatus(
MemberQueryRequest queryRequest, RequirementStatus paymentStatus, Pageable pageable);
MemberQueryOption queryOption, RequirementStatus paymentStatus, Pageable pageable);

Map<Boolean, List<Member>> groupByVerified(List<Long> memberIdList);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.gdschongik.gdsc.domain.member.dao;

import static com.gdschongik.gdsc.domain.member.domain.QMember.*;
import static com.gdschongik.gdsc.domain.member.domain.RequirementStatus.*;
import static com.querydsl.core.group.GroupBy.*;

import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.member.domain.MemberRole;
import com.gdschongik.gdsc.domain.member.domain.RequirementStatus;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryRequest;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryOption;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.annotation.Nullable;
Expand All @@ -33,10 +32,10 @@ public Optional<Member> findNormalByOauthId(String oauthId) {
}

@Override
public Page<Member> findAllGrantable(MemberQueryRequest queryRequest, Pageable pageable) {
public Page<Member> findAllGrantable(MemberQueryOption queryOption, Pageable pageable) {
List<Member> fetch = queryFactory
.selectFrom(member)
.where(queryOption(queryRequest), eqRole(MemberRole.GUEST), requirementVerified())
.where(matchesQueryOption(queryOption), eqRole(MemberRole.GUEST), isGrantAvailable())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(member.createdAt.desc())
Expand All @@ -45,16 +44,16 @@ public Page<Member> findAllGrantable(MemberQueryRequest queryRequest, Pageable p
JPAQuery<Long> countQuery = queryFactory
.select(member.count())
.from(member)
.where(queryOption(queryRequest), eqRole(MemberRole.GUEST), requirementVerified());
.where(matchesQueryOption(queryOption), eqRole(MemberRole.GUEST), isGrantAvailable());

return PageableExecutionUtils.getPage(fetch, pageable, countQuery::fetchOne);
}

@Override
public Page<Member> findAllByRole(MemberQueryRequest queryRequest, Pageable pageable, @Nullable MemberRole role) {
public Page<Member> findAllByRole(MemberQueryOption queryOption, Pageable pageable, @Nullable MemberRole role) {
List<Member> fetch = queryFactory
.selectFrom(member)
.where(queryOption(queryRequest), eqRole(role), isStudentIdNotNull())
.where(matchesQueryOption(queryOption), eqRole(role), isStudentIdNotNull())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(member.createdAt.desc())
Expand All @@ -63,18 +62,18 @@ public Page<Member> findAllByRole(MemberQueryRequest queryRequest, Pageable page
JPAQuery<Long> countQuery = queryFactory
.select(member.count())
.from(member)
.where(queryOption(queryRequest), eqRole(role), isStudentIdNotNull());
.where(matchesQueryOption(queryOption), eqRole(role), isStudentIdNotNull());

return PageableExecutionUtils.getPage(fetch, pageable, countQuery::fetchOne);
}

@Override
public Page<Member> findAllByPaymentStatus(
MemberQueryRequest queryRequest, RequirementStatus paymentStatus, Pageable pageable) {
MemberQueryOption queryOption, RequirementStatus paymentStatus, Pageable pageable) {
List<Member> fetch = queryFactory
.selectFrom(member)
.where(
queryOption(queryRequest),
matchesQueryOption(queryOption),
eqRequirementStatus(member.requirement.paymentStatus, paymentStatus),
isStudentIdNotNull())
.offset(pageable.getOffset())
Expand All @@ -86,7 +85,7 @@ public Page<Member> findAllByPaymentStatus(
.select(member.count())
.from(member)
.where(
queryOption(queryRequest),
matchesQueryOption(queryOption),
eqRequirementStatus(member.requirement.paymentStatus, paymentStatus),
isStudentIdNotNull());

Expand All @@ -98,7 +97,7 @@ public Map<Boolean, List<Member>> groupByVerified(List<Long> memberIdList) {
Map<Boolean, List<Member>> groupByVerified = queryFactory
.selectFrom(member)
.where(member.id.in(memberIdList))
.transform(groupBy(requirementVerified()).as(list(member)));
.transform(groupBy(isGrantAvailable()).as(list(member)));

return replaceNullByEmptyList(groupByVerified);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.gdschongik.gdsc.domain.member.domain.Department;
import com.gdschongik.gdsc.domain.member.domain.MemberRole;
import com.gdschongik.gdsc.domain.member.domain.RequirementStatus;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryRequest;
import com.gdschongik.gdsc.domain.member.dto.request.MemberQueryOption;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.EnumPath;
Expand Down Expand Up @@ -59,22 +59,22 @@ protected BooleanExpression isStudentIdNotNull() {
return member.studentId.isNotNull();
}

protected BooleanBuilder requirementVerified() {
protected BooleanBuilder isGrantAvailable() {
return new BooleanBuilder()
.and(eqRequirementStatus(member.requirement.discordStatus, VERIFIED))
.and(eqRequirementStatus(member.requirement.univStatus, VERIFIED))
.and(eqRequirementStatus(member.requirement.paymentStatus, VERIFIED))
.and(eqRequirementStatus(member.requirement.bevyStatus, VERIFIED));
}

protected BooleanBuilder queryOption(MemberQueryRequest queryRequest) {
protected BooleanBuilder matchesQueryOption(MemberQueryOption queryOption) {
return new BooleanBuilder()
.and(eqStudentId(queryRequest.studentId()))
.and(eqName(queryRequest.name()))
.and(eqPhone(queryRequest.phone()))
.and(inDepartmentList(Department.searchDepartments(queryRequest.department())))
.and(eqEmail(queryRequest.email()))
.and(eqDiscordUsername(queryRequest.discordUsername()))
.and(eqNickname(queryRequest.nickname()));
.and(eqStudentId(queryOption.studentId()))
.and(eqName(queryOption.name()))
.and(eqPhone(queryOption.phone()))
.and(inDepartmentList(Department.searchDepartments(queryOption.department())))
.and(eqEmail(queryOption.email()))
.and(eqDiscordUsername(queryOption.discordUsername()))
.and(eqNickname(queryOption.nickname()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public boolean isGranted() {
return role.equals(USER) || role.equals(MemberRole.ADMIN);
}

/**
* 회원 승인 가능 여부를 반환합니다.
*
* @see com.gdschongik.gdsc.domain.member.dao.MemberQueryMethod#isGrantAvailable()
*/
public boolean isGrantAvailable() {
try {
validateGrantAvailable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import io.swagger.v3.oas.annotations.media.Schema;

public record MemberQueryRequest(
public record MemberQueryOption(
@Schema(description = "학번", pattern = STUDENT_ID) String studentId,
@Schema(description = "이름") String name,
@Schema(description = "전화번호", pattern = PHONE_WITHOUT_HYPHEN) String phone,
Expand Down