Skip to content

Commit

Permalink
Refactor: Enum 그룹화를 통해 Member 역할 구체화 (#160)
Browse files Browse the repository at this point in the history
* refactor: Enum 그룹화를 통한 MemberRole 분리

- Active Members: Member, Admin, Education Team
- Clients: Member, Admin
- Managers: Education, Admin

* style: 코드 공백 조정

* refactor: enum을 부연 설명하는 description 필드 추가
  • Loading branch information
Youthhing authored May 8, 2024
1 parent 6cfa00a commit 4b9e5bf
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 40 deletions.
11 changes: 8 additions & 3 deletions src/main/java/cotato/csquiz/domain/enums/ChoiceCorrect.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package cotato.csquiz.domain.enums;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum ChoiceCorrect {
ANSWER,
NO_ANSWER,
SECRET
ANSWER("퀴즈 정답"),
NO_ANSWER("퀴즈 오답"),
SECRET("정답 여부를 알려주지 않음");

private final String description;
}
11 changes: 8 additions & 3 deletions src/main/java/cotato/csquiz/domain/enums/EducationStatus.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package cotato.csquiz.domain.enums;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum EducationStatus {
BEFORE,
ONGOING,
FINISHED
BEFORE("교육 시작 전"),
ONGOING("교육 진행"),
FINISHED("교육 종료");

private final String description;
}
21 changes: 21 additions & 0 deletions src/main/java/cotato/csquiz/domain/enums/MemberRoleGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cotato.csquiz.domain.enums;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum MemberRoleGroup {

ACTIVE_MEMBERS("현재 활동 중인 멤버", List.of(MemberRole.MEMBER, MemberRole.ADMIN, MemberRole.EDUCATION)),
CLIENTS("교육 중 문제 풀이가 가능한 멤버", List.of(MemberRole.MEMBER, MemberRole.ADMIN)),
MANAGERS("교육 진행 관리자", List.of(MemberRole.EDUCATION, MemberRole.ADMIN));

private final String description;
private final List<MemberRole> roles;

public static boolean hasRole(MemberRoleGroup group, MemberRole role) {
return group.getRoles().contains(role);
}
}
9 changes: 7 additions & 2 deletions src/main/java/cotato/csquiz/domain/enums/QuizType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cotato.csquiz.domain.enums;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum QuizType {
MULTIPLE_QUIZ,
SHORT_QUIZ
MULTIPLE_QUIZ("객관식 문제"),
SHORT_QUIZ("주관식 문제");

private final String description;
}
33 changes: 11 additions & 22 deletions src/main/java/cotato/csquiz/global/websocket/WebSocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cotato.csquiz.controller.dto.socket.QuizStopResponse;
import cotato.csquiz.domain.entity.Quiz;
import cotato.csquiz.domain.enums.MemberRole;
import cotato.csquiz.domain.enums.MemberRoleGroup;
import cotato.csquiz.domain.enums.QuizStatus;
import cotato.csquiz.exception.AppException;
import cotato.csquiz.exception.ErrorCode;
Expand Down Expand Up @@ -44,11 +45,11 @@ public void afterConnectionEstablished(WebSocketSession session) {
String memberId = findAttributeByToken(session, MEMBER_ID_KEY);
Long educationId = Long.parseLong(findAttributeByToken(session, EDUCATION_ID_KEY));
String role = findAttributeByToken(session, ROLE_KEY);
MemberRole memberRole = findMemberRole(role);
MemberRole memberRole = MemberRole.fromKey(role);

addMemberToSession(memberId, memberRole, session);
addMemberToSession(memberId, session);

if (isMemberClient(memberRole)) {
if (MemberRoleGroup.hasRole(MemberRoleGroup.CLIENTS, memberRole)) {
sendQuiz(educationId, session);
}

Expand All @@ -74,10 +75,10 @@ private void sendQuiz(Long educationId, WebSocketSession session) {
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
String memberId = findAttributeByToken(session, MEMBER_ID_KEY);
String role = findAttributeByToken(session, ROLE_KEY);
MemberRole memberRole = findMemberRole(role);
String roleAttribute = findAttributeByToken(session, ROLE_KEY);
MemberRole memberRole = MemberRole.fromKey(roleAttribute);

if (isMemberClient(memberRole)) {
if (MemberRoleGroup.hasRole(MemberRoleGroup.CLIENTS, memberRole)) {
CLIENTS.remove(memberId);
} else {
MANAGERS.remove(memberId);
Expand Down Expand Up @@ -131,23 +132,11 @@ public void stopAllQuiz(Long educationId) {
}
}

private MemberRole findMemberRole(String roleName) {
return MemberRole.fromKey(roleName);
}

private Boolean isMemberClient(MemberRole memberRole) {
return switch (memberRole) {
case EDUCATION -> false;
case MEMBER, ADMIN -> true;
default -> {
log.warn("Unknown role for member {}.", memberRole);
throw new AppException(ErrorCode.MEMBER_CANT_ACCESS);
}
};
}
private void addMemberToSession(String memberId, WebSocketSession session) {
String roleAttribute = findAttributeByToken(session, ROLE_KEY);
MemberRole role = MemberRole.fromKey(roleAttribute);

private void addMemberToSession(String memberId, MemberRole role, WebSocketSession session) {
if (isMemberClient(role)) {
if (MemberRoleGroup.hasRole(MemberRoleGroup.CLIENTS, role)) {
CLIENTS.put(memberId, session);
log.info("{} connect with Session {} in CLIENTS", memberId, session);
} else {
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/cotato/csquiz/service/AdminService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package cotato.csquiz.service;

import static cotato.csquiz.domain.enums.MemberRole.ADMIN;
import static cotato.csquiz.domain.enums.MemberRole.EDUCATION;
import static cotato.csquiz.domain.enums.MemberRole.GENERAL;
import static cotato.csquiz.domain.enums.MemberRole.MEMBER;
import static cotato.csquiz.domain.enums.MemberRole.OLD_MEMBER;
Expand All @@ -17,7 +15,7 @@
import cotato.csquiz.domain.entity.Generation;
import cotato.csquiz.domain.entity.Member;
import cotato.csquiz.domain.entity.RefusedMember;
import cotato.csquiz.domain.enums.MemberRole;
import cotato.csquiz.domain.enums.MemberRoleGroup;
import cotato.csquiz.exception.AppException;
import cotato.csquiz.exception.ErrorCode;
import cotato.csquiz.repository.GenerationRepository;
Expand Down Expand Up @@ -106,7 +104,7 @@ public List<MemberEnrollInfoResponse> findCurrentActiveMembers() {
@Transactional
public void updateActiveMemberRole(UpdateActiveMemberRoleRequest request) {
Member member = findMember(request.memberId());
if (member.getRole() == MemberRole.GENERAL || member.getRole() == REFUSED || member.getRole() == OLD_MEMBER) {
if (!MemberRoleGroup.hasRole(MemberRoleGroup.ACTIVE_MEMBERS, member.getRole())) {
throw new AppException(ErrorCode.ROLE_IS_NOT_MATCH);
}
member.updateRole(request.role());
Expand All @@ -117,12 +115,11 @@ public void updateActiveMemberRole(UpdateActiveMemberRoleRequest request) {
public void updateActiveMembersToOldMembers(UpdateActiveMemberToOldMemberRequest request) {
for (Long memberId : request.memberIds()) {
Member member = findMember(memberId);
if (member.getRole() == MEMBER || member.getRole() == ADMIN || member.getRole() == EDUCATION) {
member.updateRole(OLD_MEMBER);
memberRepository.save(member);
} else {
if (!MemberRoleGroup.hasRole(MemberRoleGroup.ACTIVE_MEMBERS, member.getRole())) {
throw new AppException(ErrorCode.ROLE_IS_NOT_MATCH);
}
member.updateRole(OLD_MEMBER);
memberRepository.save(member);
}
}

Expand Down Expand Up @@ -172,4 +169,4 @@ private List<ApplyMemberInfoResponse> createApplyInfoList(List<Member> applicant
.map(member -> ApplyMemberInfoResponse.from(member, memberService.findBackFourNumber(member)))
.toList();
}
}
}
1 change: 0 additions & 1 deletion src/main/java/cotato/csquiz/service/EducationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ private void validateNotEmpty(String newSubject) {
.orElseThrow(() -> new AppException(ErrorCode.SUBJECT_INVALID));
}


public List<AllEducationResponse> findEducationListByGeneration(Long generationId) {
return sessionRepository.findAllByGenerationId(generationId).stream()
.map(educationRepository::findBySession)
Expand Down

0 comments on commit 4b9e5bf

Please sign in to comment.