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

Feature/#210 교직원 관련 api 연결 #217

Merged
merged 15 commits into from
Oct 4, 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
16 changes: 15 additions & 1 deletion backend/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ include::{snippets}/student-find-all/http-response.adoc[]
.Response Body
include::{snippets}/student-find-all/response-fields.adoc[]

=== `GET`: 교직원 목록 조회

.HTTP Request
include::{snippets}/faculty-find-all/http-request.adoc[]

.Path Parameters
include::{snippets}/faculty-find-all/query-parameters.adoc[]

.HTTP Response
include::{snippets}/faculty-find-all/http-response.adoc[]

.Response Body
include::{snippets}/faculty-find-all/response-fields.adoc[]


=== `PATCH`: 비밀번호 수정

.HTTP Request
Expand Down Expand Up @@ -334,4 +349,3 @@ include::{snippets}/admin-auth-delete/request-body.adoc[]

.HTTP Response
include::{snippets}/admin-auth-delete/http-response.adoc[]

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ResponseEntity<Void> registerFaculties(
public ResponseEntity<Void> deleteFaculty(
@SuperAdminInterface FacultyMember facultyMember,
@RequestBody @Valid DeleteFacultyRequest request) {
adminAuthCommandService.deleteFaculty(request.member_id());
adminAuthCommandService.deleteFaculty(request.faculty_id());
return ResponseEntity.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ public void registerFaculties(MultipartFile file) {
}

@Transactional
public void deleteFaculty(Long memberId) {
FacultyMember facultyMember = facultyMemberRepository.findById(memberId)
public void deleteFaculty(Long facultyId) {
FacultyMember facultyMember = facultyMemberRepository.findById(facultyId)
.orElseThrow(() -> new AdminAuthException(AdminAuthExceptionType.MEMBER_NOT_FOUND));

if(facultyMember.getId() == 1) throw new AdminAuthException(AdminAuthExceptionType.MEMBER_NOT_FOUND);

Member member = facultyMember.getMember();

checkIsMemberDeleted(member);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sw_css.admin.auth.application.dto.request;

public record DeleteFacultyRequest(
Long member_id
Long faculty_id
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum AdminAuthExceptionType implements BaseExceptionType {
FAILED_REGISTER_FACULTY(HttpStatus.BAD_REQUEST, ""),
MEMBER_EMAIL_DUPLICATE(HttpStatus.CONFLICT, "이메일이 중복됩니다."),
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 회원을 찾을 수 없습니다."),
CANNOT_DELETE_SUPER_ADMIN(HttpStatus.FORBIDDEN, "최고 관리자는 삭제할 수 없습니다.")
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sw_css.admin.member.application.MemberAdminQueryService;
import sw_css.admin.member.application.dto.response.FacultyMemberResponse;
import sw_css.member.application.dto.response.StudentMemberResponse;
import sw_css.member.domain.FacultyMember;
import sw_css.utils.annotation.AdminInterface;

@Validated
@RequestMapping("/admin/members")
@RequestMapping("/admin/member")
@RestController
@RequiredArgsConstructor
public class MemberAdminController {
private final MemberAdminQueryService memberAdminQueryService;

@GetMapping
public ResponseEntity<List<StudentMemberResponse>> findAllStudent() {
@GetMapping("/students")
public ResponseEntity<List<StudentMemberResponse>> findAllStudent(@AdminInterface FacultyMember facultyMember) {
return ResponseEntity.ok(memberAdminQueryService.findStudentMembers());
}

@GetMapping("/faculties")
public ResponseEntity<Page<FacultyMemberResponse>> findAllFaculty(
@AdminInterface FacultyMember facultyMember,
@RequestParam(value = "field", required = false) final Integer field,
@RequestParam(value = "keyword", required = false) final String keyword,
final Pageable pageable) {
return ResponseEntity.ok(memberAdminQueryService.findFacultyMembers(field, keyword, pageable));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sw_css.admin.member.application.dto.response.FacultyMemberResponse;
import sw_css.member.application.dto.response.StudentMemberResponse;
import sw_css.member.domain.FacultyMember;
import sw_css.member.domain.StudentMember;
import sw_css.member.domain.repository.FacultyMemberCustomRepository;
import sw_css.member.domain.repository.FacultyMemberRepository;
import sw_css.member.domain.repository.StudentMemberRepository;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MemberAdminQueryService {
private final StudentMemberRepository studentMemberRepository;
private final FacultyMemberCustomRepository facultyMemberCustomRepository;

public List<StudentMemberResponse> findStudentMembers() {
final List<StudentMember> students = studentMemberRepository.findAll();
return students.stream().map(StudentMemberResponse::from).toList();
}

public Page<FacultyMemberResponse> findFacultyMembers(final Integer field, final String keyword, final Pageable pageable) {
final Page<FacultyMember> faculties = facultyMemberCustomRepository.findFacultyMembers(field, keyword, pageable);

return FacultyMemberResponse.from(faculties, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sw_css.admin.member.application.dto.response;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import sw_css.member.domain.FacultyMember;

public record FacultyMemberResponse(
Long id,
Long facultyId,
String email,
String name,
String phoneNumber) {
public static FacultyMemberResponse from(final FacultyMember facultyMember) {
return new FacultyMemberResponse(
facultyMember.getMember().getId(),
facultyMember.getId(),
facultyMember.getMember().getEmail(),
facultyMember.getMember().getName(),
facultyMember.getMember().getPhoneNumber()
);
}

public static Page<FacultyMemberResponse> from(final Page<FacultyMember> faculties, final Pageable pageable) {
return new PageImpl<>(faculties.stream()
.map(FacultyMemberResponse::from)
.toList(), pageable, faculties.getTotalElements());
}
}
26 changes: 26 additions & 0 deletions backend/src/main/java/sw_css/member/domain/FacultySearchField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sw_css.member.domain;

import java.util.Arrays;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import sw_css.member.exception.MemberException;
import sw_css.member.exception.MemberExceptionType;

@RequiredArgsConstructor
public enum FacultySearchField {
FACULTY_ID(1),
MEMBER_NAME(2),
MEMBER_EMAIL(3);

final Integer fieldId;

public static FacultySearchField fromValue(final Integer fieldId) {
return Arrays.stream(values()).filter(field -> Objects.equals(field.getFieldId(), fieldId)).findFirst()
.orElseThrow(() -> new MemberException(
MemberExceptionType.INVALID_SEARCH_FIELD_ID));
}

public Integer getFieldId() {
return fieldId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sw_css.member.domain.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.Nullable;
import sw_css.member.domain.FacultyMember;

public interface FacultyMemberCustomRepository {

Page<FacultyMember> findFacultyMembers(@Nullable final Integer field,
@Nullable final String keyword,
final Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
import sw_css.member.domain.FacultyMember;

public interface FacultyMemberRepository extends JpaRepository<FacultyMember, Long> {
boolean existsByMemberId(Long memberId);

Optional<FacultyMember> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
public enum MemberExceptionType implements BaseExceptionType {
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 회원을 찾을 수 없습니다."),
MEMBER_WRONG_PASSWORD(HttpStatus.BAD_REQUEST, "비밀번호가 잘못되었습니다."),
NOT_FOUND_STUDENT(HttpStatus.NOT_FOUND, "해당하는 학생이 존재하지 않습니다.");
NOT_FOUND_STUDENT(HttpStatus.NOT_FOUND, "해당하는 학생이 존재하지 않습니다."),
INVALID_SEARCH_FIELD_ID(HttpStatus.BAD_REQUEST, "검색 유형이 올바르지 않습니다."),
;


private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package sw_css.member.persistence;

import static sw_css.member.domain.QFacultyMember.facultyMember;
import static sw_css.member.domain.QMember.member;
import static sw_css.member.domain.QStudentMember.studentMember;
import static sw_css.milestone.domain.QMilestone.milestone;
import static sw_css.milestone.domain.QMilestoneHistory.milestoneHistory;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;
import sw_css.member.domain.FacultyMember;
import sw_css.member.domain.FacultySearchField;
import sw_css.member.domain.repository.FacultyMemberCustomRepository;
import sw_css.milestone.domain.MilestoneHistorySearchField;
import sw_css.milestone.persistence.dto.MilestoneHistoryWithStudentInfo;

@Repository
@AllArgsConstructor
public class FacultyMemberCustomRepositoryImpl implements FacultyMemberCustomRepository {
private final JPAQueryFactory jpaQueryFactory;

@Override
public Page<FacultyMember> findFacultyMembers(@Nullable final Integer field,
@Nullable final String keyword,
final Pageable pageable) {
final BooleanBuilder booleanBuilder = getBooleanBuilder(field, keyword);

List<FacultyMember> facultyMembers = jpaQueryFactory
.selectFrom(facultyMember)
.join(facultyMember.member, member)
.where(booleanBuilder)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();


final Long count = jpaQueryFactory
.select(facultyMember.count())
.from(facultyMember)
.where(booleanBuilder)
.fetchOne();

return new PageImpl<>(facultyMembers, pageable, count);
}

private static BooleanBuilder getBooleanBuilder(final Integer field, final String keyword) {
BooleanBuilder booleanBuilder = new BooleanBuilder();
booleanBuilder.and(facultyMember.member.isDeleted.eq(false));
if (field != null && keyword != null && !keyword.isEmpty()) {
switch (FacultySearchField.fromValue(field)) {
case FACULTY_ID:
booleanBuilder.and(facultyMember.id.stringValue().like("%" + keyword + "%"));
break;
case MEMBER_NAME:
booleanBuilder.and(facultyMember.member.name.containsIgnoreCase(keyword));
break;
case MEMBER_EMAIL:
booleanBuilder.and(facultyMember.member.email.containsIgnoreCase(keyword));
break;
}
}
return booleanBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void registerFaculties() throws Exception {
public void deleteFaculty() throws Exception {
// given
final RequestFieldsSnippet requestFieldsSnippet = requestFields(
fieldWithPath("member_id").type(JsonFieldType.NUMBER).description("관리자의 id")
fieldWithPath("faculty_id").type(JsonFieldType.NUMBER).description("교직원 번호")
);

final long faculty_id = 1L;
Expand All @@ -96,7 +96,7 @@ public void deleteFaculty() throws Exception {
final String token = "Bearer AccessToken";

// when
doNothing().when(adminAuthCommandService).deleteFaculty(request.member_id());
doNothing().when(adminAuthCommandService).deleteFaculty(request.faculty_id());

// then
mockMvc.perform(RestDocumentationRequestBuilders.delete("/admin/auth")
Expand Down
Loading