Skip to content

Commit

Permalink
Merge pull request #55 from Linkode2024/LINKODE-119-스터디룸입장-코드-구조-개선
Browse files Browse the repository at this point in the history
Refactor : 스터디룸 입장 코드 개선
  • Loading branch information
jsilver01 authored Oct 8, 2024
2 parents ab27997 + 21e4793 commit 7cc0290
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public enum BaseExceptionResponseStatus implements ResponseStatus{
*/
INVALID_ROLE(5000,HttpStatus.OK.value(), "해당 스터디룸에 접근할 권한이 없습니다."),
NOT_FOUND_STUDYROOM(5001,HttpStatus.OK.value(), "스터디룸을 찾을 수 없습니다."),
ALREADY_CREATE_CODE(5002, HttpStatus.OK.value(), "이미 생성된 초대코드가 있습니다."),

/**
* 멤버_스터디룸 관련 code : 6000 대
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ public BaseResponse<DetailStudyroomResponse> getStudyroomDetail(@RequestHeader("
public BaseResponse<JoinStudyroomByCodeResponse> joinStudyroom(@RequestHeader("Authorization") String authorization, @RequestBody JoinStudyroomByCodeRequest request){
log.info("[StudyroomController.joinStudyroom]");
Long memberId = jwtProvider.extractIdFromHeader(authorization);
try {
JoinStudyroomByCodeResponse response = studyroomService.joinStudyroomByCode(request,memberId);
return new BaseResponse<>(response);
}catch (NullPointerException e){
return new BaseResponse<>(INVALID_INVITE_CODE,null);
}
JoinStudyroomByCodeResponse response = studyroomService.joinStudyroomByCode(request,memberId);
return new BaseResponse<>(response);
}

/**
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/linkode/api_server/service/InviteService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkode.api_server.service;

import com.linkode.api_server.common.exception.StudyroomException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -10,9 +11,12 @@
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static com.linkode.api_server.common.response.status.BaseExceptionResponseStatus.INVALID_INVITE_CODE;

@Slf4j
@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -57,27 +61,21 @@ public String generateInviteCode(Long roomId) {
/**
* 초대 코드로 스터디룸 아이디 찾기
*/
public Long findRoomIdByInviteCode(String code) {
public Optional<Long> findRoomIdByInviteCode(String code) {
String keyPattern = "invite:*";
Set<String> keys = redisTemplate.keys(keyPattern);

if (keys == null || keys.isEmpty()) {
return null;
}

for (String key : keys) {
String value = redisTemplate.opsForValue().get(key);
if (value != null) {
String[] values = value.split(",");
String storedCode = values[0];
if (storedCode.equals(code)) {
// key 형식이 "invite:{roomId}"이므로, roomId를 추출하여 반환
return Long.parseLong(key.split(":")[1]);
return Optional.of(Long.parseLong(key.split(":")[1]));
}
}
}

return null;
throw new StudyroomException(INVALID_INVITE_CODE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,20 @@ public void joinStudyroomAsCaptain(long studyroomId, long memberId){
@Transactional
public JoinStudyroomByCodeResponse joinStudyroomByCode(JoinStudyroomByCodeRequest request, long memberId){
log.info("[StudyroomService.joinStudyroomByCode]");
long studyroomId = inviteService.findRoomIdByInviteCode(request.getInviteCode());
Studyroom studyroom = studyroomRepository
.findById(studyroomId).orElseThrow(()->new StudyroomException(INVALID_INVITE_CODE));
if (memberstudyroomRepository.findByMemberIdAndStudyroomIdStatus(memberId,studyroomId,BaseStatus.ACTIVE).isPresent()){
throw new MemberException(JOINED_STUDYROOM);
}
Long studyroomId = inviteService.findRoomIdByInviteCode(request.getInviteCode())
.orElseThrow(()->new StudyroomException(INVALID_INVITE_CODE));
Studyroom studyroom = studyroomRepository
.findById(studyroomId).orElseThrow(()->new StudyroomException(INVALID_INVITE_CODE));
if (memberstudyroomRepository.findByMemberIdAndStudyroomIdStatus(memberId,studyroomId,BaseStatus.ACTIVE).isPresent()){
throw new MemberException(JOINED_STUDYROOM);
}
JoinStudyroomRequest joinStudyroomRequest = JoinStudyroomRequest.builder()
.studyroomId(studyroomId)
.memberId(memberId)
.memberRole(MemberRole.CREW)
.build();

joinStudyroom(joinStudyroomRequest);
joinStudyroom(joinStudyroomRequest);
return JoinStudyroomByCodeResponse.from(studyroom);
}

Expand Down

0 comments on commit 7cc0290

Please sign in to comment.