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

[feat] 에러 메시지 상세화 및 Redis 만료 시간 24시간 설정 추가 #34

Merged
merged 2 commits into from
Jun 13, 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 @@ -15,6 +15,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; // 로깅을 위해 추가
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.security.access.AccessDeniedException;
Expand Down Expand Up @@ -59,10 +60,16 @@ public void message(MessageDto messageDto) {
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content(mediaType = "application/json"))
})
@Parameter(name = "roomId", description = "메시지를 조회할 채팅방의 ID", required = true, example = "123")
public ResponseEntity<List<MessageDto>> loadMessage(
public ResponseEntity<?> loadMessage(
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken,
@PathVariable String roomId) {
return ResponseEntity.ok(messageService.loadMessage(roomId));
try {
List<MessageDto> messages = messageService.loadMessage(roomId);
return ResponseEntity.ok(messages);
} catch (Exception e) {
log.error("Failed to load messages for room {}: {}", roomId, e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to load messages: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public MessageService(
this.messageRepository = messageRepository;
this.messageRoomRepository = messageRoomRepository;
}

public void saveMessage(MessageDto messageDto) {
MessageRoom messageRoom = messageRoomRepository.findByRoomId(messageDto.getRoomId())
.orElseThrow(() -> new IllegalArgumentException("해당 쪽지방이 존재하지 않습니다."));
Expand All @@ -49,7 +50,7 @@ public void saveMessage(MessageDto messageDto) {

redisTemplateMessage.setValueSerializer(new Jackson2JsonRedisSerializer<>(MessageDto.class));
redisTemplateMessage.opsForList().rightPush(messageDto.getRoomId(), messageDto);
redisTemplateMessage.expire(messageDto.getRoomId(), 1, TimeUnit.HOURS); // redis에서 1시간마다 삭제
redisTemplateMessage.expire(messageDto.getRoomId(), 24, TimeUnit.HOURS); // redis에서 24시간마다 삭제
}

/**
Expand Down Expand Up @@ -78,7 +79,8 @@ public List<MessageDto> loadMessage(String roomId) {
.build();
messageList.add(messageDto);
redisTemplateMessage.setValueSerializer(new Jackson2JsonRedisSerializer<>(Message.class)); // 직렬화
redisTemplateMessage.opsForList().rightPush(roomId, messageDto); // redis 저장
redisTemplateMessage.opsForList().rightPush(roomId, messageDto); // redis 저장
redisTemplateMessage.expire(roomId, 24, TimeUnit.HOURS); // redis에서 24시간마다 삭제
}
} else { // 4. 뒤쪽에 데이터 붙이기
messageList.addAll(redisMessageList);
Expand Down
Loading