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 : 맴버 스터디룸 조회,탈퇴 리펙토링 #48

Merged
merged 2 commits into from
Sep 23, 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 @@ -43,10 +43,10 @@ public BaseResponse<BaseExceptionResponseStatus> deleteStudyroom(@RequestHeader(
public BaseResponse<MemberStudyroomListResponse> leaveStudyroom(@RequestHeader("Authorization") String authorization, @RequestParam long studyroomId){
log.info("[StudyroomController.leaveStudyroom]");
long memberId = jwtProvider.extractIdFromHeader(authorization);
BaseExceptionResponseStatus responseStatus = memberStudyroomService.leaveStudyroom(studyroomId, memberId);
memberStudyroomService.leaveStudyroom(studyroomId, memberId);
MemberStudyroomListResponse latestStudyroomList = memberStudyroomService.getMemberStudyroomList(memberId);
log.info("Run leaveStudyroom API ");
return new BaseResponse<>(responseStatus, latestStudyroomList);
return new BaseResponse<>(SUCCESS, latestStudyroomList);
}

@PostMapping("/generation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ public interface MemberstudyroomRepository extends JpaRepository<MemberStudyroom
@Query("UPDATE MemberStudyroom ms SET ms.status = :status WHERE ms IN :memberStudyrooms")
void updateMemberStudyroomStatus(@Param("memberStudyrooms") List<MemberStudyroom> memberStudyrooms, @Param("status") BaseStatus status);

@Query("SELECT ms.role, s.studyroomName, s.studyroomProfile, m.memberId, m.nickname, m.avatar.id, m.color.id " +
@Query("SELECT ms " +
"FROM MemberStudyroom ms " +
"JOIN ms.studyroom s " +
"JOIN s.memberStudyroomList msl " +
"JOIN msl.member m " +
"WHERE ms.studyroom.studyroomId = :studyroomId " +
"AND ms.member.memberId = :memberId " +
"JOIN FETCH ms.studyroom s " +
"JOIN FETCH ms.member m " +
"JOIN FETCH s.memberStudyroomList msl " +
"JOIN FETCH msl.member " +
"WHERE s.studyroomId = :studyroomId " +
"AND m.memberId = :memberId " +
"AND ms.status = :status")
List<Object[]> getStudyroomDetail(long studyroomId,long memberId, BaseStatus status);
Optional<MemberStudyroom> getStudyroomDetail(long studyroomId,long memberId, BaseStatus status);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,19 @@ public void deleteMember(Long memerId){
* */
public DetailStudyroomResponse getStudyroomDetail(long studyroomId, long memberId) {
log.info("[MemberStudyroomService.getStudyroomDetail]");
List<Object[]> results = memberstudyroomRepository.getStudyroomDetail(studyroomId, memberId, BaseStatus.ACTIVE);
MemberStudyroom memberStudyroom = memberstudyroomRepository.getStudyroomDetail(studyroomId,memberId,BaseStatus.ACTIVE)
.orElseThrow(()-> new MemberStudyroomException(NOT_FOUND_MEMBER_STUDYROOM));

if (results.isEmpty()) {
throw new MemberStudyroomException(NOT_FOUND_MEMBER_STUDYROOM);
}
Object[] firstRow = results.get(0);
MemberRole role = (MemberRole) firstRow[0];
String studyroomName = (String) firstRow[1];
String studyroomProfile = (String) firstRow[2];


List<DetailStudyroomResponse.Member> members = results.stream()
.map(row -> DetailStudyroomResponse.Member.builder()
.memberId((Long) row[3])
.nickname((String) row[4])
.avatarId((Long) row[5])
.colorId((Long) row[6])
MemberRole role = memberStudyroom.getRole();
String studyroomName = memberStudyroom.getStudyroom().getStudyroomName();
String studyroomProfile = memberStudyroom.getStudyroom().getStudyroomProfile();

List<DetailStudyroomResponse.Member> members = memberStudyroom.getStudyroom().getMemberStudyroomList().stream()
.map(member -> DetailStudyroomResponse.Member.builder()
.memberId(member.getMember().getMemberId())
.nickname(member.getMember().getNickname())
.avatarId(member.getMember().getAvatar().getAvatarId())
.colorId(member.getMember().getColor().getColorId())
.build())
.collect(Collectors.toList());

Expand All @@ -108,29 +104,13 @@ public DetailStudyroomResponse getStudyroomDetail(long studyroomId, long memberI
*
* */
@Transactional
public BaseExceptionResponseStatus leaveStudyroom(long studyroomId, long memberId){
public void leaveStudyroom(long studyroomId, long memberId){
log.info("[MemberStudyroomService.leaveStudyroom]");
try {
MemberStudyroom memberStudyroom = memberstudyroomRepository
.findByMember_MemberIdAndStudyroom_StudyroomIdAndStatus(memberId,studyroomId,BaseStatus.ACTIVE)
.orElseThrow(()-> new MemberStudyroomException(NOT_FOUND_MEMBER_STUDYROOM));
if(memberStudyroom.getRole()==MemberRole.CAPTAIN) throw new
LeaveStudyroomExeption(CANNOT_LEAVE_STUDYROOM);
if(memberStudyroom.getRole()==MemberRole.CAPTAIN) throw new MemberStudyroomException(CANNOT_LEAVE_STUDYROOM);
memberStudyroom.updateMemberStudyroomStatus(BaseStatus.DELETE);
memberstudyroomRepository.save(memberStudyroom);
return BaseExceptionResponseStatus.SUCCESS;
}
catch (LeaveStudyroomExeption e) {
log.error("MemberStudyroomException! -> ", e);
return CANNOT_LEAVE_STUDYROOM;
}
catch (MemberStudyroomException e) {
log.error("MemberStudyroomException! -> ", e);
return NOT_FOUND_MEMBER_STUDYROOM;
}
catch (Exception e){
return FAILURE;
}
}

/**
Expand Down
Loading