Skip to content
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 @@ -38,4 +38,11 @@ public ApiResponse<?> getQuestions(@PathVariable("lectureId") UUID lectureId) {
return ApiResponse.onSuccess(result);
}
}

// 이전 내용 불러오기
@GetMapping("/chatting/before/{lectureId}")
public ApiResponse<List<QuestionResponseDTO.beforeChatting>> getBeforeChatting(@PathVariable("lectureId") UUID lectureId) {
List<QuestionResponseDTO.beforeChatting> result = questionService.getBeforeChatting(lectureId);
return ApiResponse.onSuccess(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.backend.domain.user.entity.Role;

import java.time.LocalDateTime;
import java.util.UUID;
Expand Down Expand Up @@ -33,4 +34,14 @@ public static class student {
private String content;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class beforeChatting {
private String content;
private LocalDateTime timestamp;
private Role role;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
@AllArgsConstructor
public enum QuestionErrorCode implements BaseErrorCode {
_FORBIDDEN_LECTURE_ACCESS(HttpStatus.FORBIDDEN,"QUESTION403_1","해당 강의를 수강중인 학생만 조회가능합니다."),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나 자꾸 이 에러가 나는데 학생권한 관련 로직 다시 한 번 체크해줄 수 있을까용

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GET api/lectures/chatting/before/{lectureId} 이 API 맞나용
위에 API에서는 학생검증 로직이 없어서용

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image 웅 여기 403에러가떠서..ㅜㅡㅜ

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

미아내 해결함!!

_CHAT_MESSAGE_SEND_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "QUESTION500_1", "채팅 메시지 전송에 실패했습니다.");
_CHAT_MESSAGE_SEND_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "QUESTION500_1", "채팅 메시지 전송에 실패했습니다."),
_INVALID_JSON_FORMAT(HttpStatus.BAD_REQUEST,"QUESTION400_1","저장된 채팅 데이터를 읽는 데 실패했습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface QuestionService {
List<QuestionResponseDTO.teacher> getTeacherQuestions(UUID lectureId, User user);
//학생용 질문 조회
List<QuestionResponseDTO.student> getStudentQuestions(UUID lectureId, User user);
// 이전 채팅 내용 불러오개
List<QuestionResponseDTO.beforeChatting> getBeforeChatting(UUID lectureId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.example.backend.domain.question.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.example.backend.domain.question.converter.QuestionConverter;
import org.example.backend.domain.question.dto.response.QuestionResponseDTO;
Expand All @@ -8,8 +10,11 @@
import org.example.backend.domain.question.repository.QuestionRepository;
import org.example.backend.domain.studentClass.repository.StudentClassRepository;
import org.example.backend.domain.user.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

Expand All @@ -20,6 +25,8 @@ public class QuestionServiceImpl implements QuestionService {
private final QuestionRepository questionRepository;
private final StudentClassRepository studentClassRepository;
private final QuestionConverter questionConverter;
private final RedisTemplate<String,String> redisTemplate;
private final ObjectMapper objectMapper;

@Override
public List<QuestionResponseDTO.teacher> getTeacherQuestions(UUID lectureId, User user ) {
Expand All @@ -41,4 +48,34 @@ public List<QuestionResponseDTO.student> getStudentQuestions(UUID lectureId, Use
.map(questionConverter::toStudentQuestions)
.toList();
}

// 이전 내용 불러오기
@Override
public List<QuestionResponseDTO.beforeChatting> getBeforeChatting(UUID lectureId) {
String redisKey = "chat:lecture:" + lectureId;

List<String> rawMessages = redisTemplate.opsForList().range(redisKey, 0, -1);

// 이전 내용이 없다면 빈배열 반환
if(rawMessages == null || rawMessages.isEmpty()){
return Collections.emptyList();
}

// 반환리스트
List<QuestionResponseDTO.beforeChatting> chatList = new ArrayList<>();

// 최신 메시지가 먼저 저장됨 -> 역순으로 순회
for(int i=rawMessages.size()-1;i>=0;i--){
try{
// JSON 문자열 -> DTO 변환
QuestionResponseDTO.beforeChatting dto = objectMapper.readValue(rawMessages.get(i), QuestionResponseDTO.beforeChatting.class);
chatList.add(dto);
}
catch (JsonProcessingException e){
// 파싱 실패
throw new QuestionException(QuestionErrorCode._INVALID_JSON_FORMAT);
}
}
return chatList;
}
}