refactor : header 필요 기능 주석

This commit is contained in:
yulmam 2024-07-24 10:22:47 +09:00
parent 45a7e78014
commit 9cfa34ed00
12 changed files with 170 additions and 46 deletions

View File

@ -19,6 +19,7 @@ import java.util.List;
*/
@RestController
@RequestMapping("/board")
@CrossOrigin("*")
public class BoardController {
private final JWTUtil jwtUtil;

View File

@ -56,8 +56,8 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public void createBoard(long userId, RequestBoardDto requestBoardDto) {
User user = userRepository.findById(userId).orElseThrow();
Lecture lecture = lectureRepository.findById(requestBoardDto.getLectureId()).orElseThrow(IllegalArgumentException::new);
User user = userRepository.getReferenceById(userId);
Lecture lecture = lectureRepository.getReferenceById(requestBoardDto.getLectureId());
Board board = Board.builder()
.title(requestBoardDto.getTitle())
@ -83,7 +83,7 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public void deleteBoard(long boardId) {
Board board = boardRepository.findById(boardId).orElseThrow(IllegalArgumentException::new);
Board board = boardRepository.getReferenceById(boardId);
boardRepository.delete(board);
}
@ -97,8 +97,8 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public void createComment(long userId, long boardId, RequestCommentDto requestCommentDto) {
User user = userRepository.findById(userId).orElseThrow(IllegalArgumentException::new);
Board board = boardRepository.findById(boardId).orElseThrow(IllegalArgumentException::new);
User user = userRepository.getReferenceById(userId);
Board board = boardRepository.getReferenceById(boardId);
Comment comment = Comment.builder()
.content(requestCommentDto.getContent())
@ -122,7 +122,7 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public void deleteComment(long commentId) {
Comment comment = commentRepository.findById(commentId).orElseThrow(NoSuchElementException::new);
Comment comment = commentRepository.getReferenceById(commentId);
commentRepository.delete(comment);
}

View File

@ -16,4 +16,5 @@ public interface LectureRepository extends JpaRepository<Lecture, Long> {
List<Lecture> findLecturesByUserId(Long userId);
Lecture findByIdAndUserId(long id, long userId);
}

View File

@ -2,16 +2,13 @@ package com.edufocus.edufocus.ws.controller;
import com.edufocus.edufocus.user.util.JWTUtil;
import com.edufocus.edufocus.ws.entity.dto.HelloDto;
import com.edufocus.edufocus.ws.entity.dto.MessageDto;
import com.edufocus.edufocus.ws.entity.dto.QuizDto;
import com.edufocus.edufocus.ws.entity.dto.ResponseChatUserDto;
import com.edufocus.edufocus.ws.entity.dto.ChatUserDto;
import com.edufocus.edufocus.ws.service.ChatService;
import jakarta.websocket.Session;
import org.springframework.context.event.EventListener;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
@ -41,24 +38,27 @@ public class ChatController {
this.jwtUtil = jwtUtil;
}
@GetMapping("/chat/enter/{lectureId}")
public ResponseEntity<?> enter(@PathVariable long lectureId){
List<ResponseChatUserDto> chatUsers = chatService.findChatUsers(lectureId);
return new ResponseEntity<>(chatUsers, HttpStatus.OK);
}
// @GetMapping("/chat/enter/{lectureId}")
// public ResponseEntity<?> enter(@PathVariable long lectureId){
// List<ChatUserDto> chatUsers = chatService.findChatUsers(lectureId);
//
// return new ResponseEntity<>(chatUsers, HttpStatus.OK);
// }
@MessageMapping("/hello/{channelId}")
@SendTo("/sub/channel/{channelId}")
public ResponseChatUserDto hello(@DestinationVariable long channelId, SimpMessageHeaderAccessor simpMessageHeaderAccessor, @Header("Authorization") String token){
String sessionId = simpMessageHeaderAccessor.getSessionId();
long userId = Long.parseLong(jwtUtil.getUserId(token));
chatService.saveChatUserInfo(userId, channelId, sessionId);
return chatService.getChatUserInfo(userId);
}
// @MessageMapping("/hello/{channelId}")
// @SendTo("/sub/channel/{channelId}")
// public ChatUserDto hello(@DestinationVariable long channelId, SimpMessageHeaderAccessor simpMessageHeaderAccessor, @Header("Authorization") String token){
// String sessionId = simpMessageHeaderAccessor.getSessionId();
//
// System.out.println("session" + sessionId);
//
// long userId = Long.parseLong(jwtUtil.getUserId(token));
//
// chatService.saveChatUserInfo(userId, channelId, sessionId);
//
// return chatService.getChatUserInfo(userId);
// }
@MessageMapping("/message/{lectureId}")
@SendTo("/sub/channel/{lectureId}")
@ -70,12 +70,23 @@ public class ChatController {
return quizDto;
}
@EventListener
public void handleWebSocketDisconnectListener(SessionDisconnectEvent event){
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
String sessionId = headerAccessor.getSessionId();
long channelId = chatService.getChannelId(sessionId);
simpMessageSendingOperations.convertAndSend("/sub/channel/" + channelId);
}
// @EventListener
// public void handleWebSocketDisconnectListener(SessionDisconnectEvent event){
// StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
// String sessionId = headerAccessor.getSessionId();
//
//
// ChatUserDto chatUserDto = chatService.getChatUserInfo(sessionId);
//
//
// if(chatService.checkTeacher(chatUserDto)){
// chatService.closeChatRoom(chatUserDto.getLectureId());
// simpMessageSendingOperations.convertAndSend("/sub/channel/" + chatUserDto.getLectureId(), "강의가 종료 됐습니다.");
// return;
// }
//
// chatService.deleteChatUserInfo(chatUserDto.getUserId());
// simpMessageSendingOperations.convertAndSend("/sub/channel/" + chatUserDto.getLectureId(), chatUserDto);
// }
}

View File

@ -1,6 +1,14 @@
package com.edufocus.edufocus.ws.entity.dto;
public class ResponseChatUserDto {
int userId;
int name;
import lombok.*;
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ChatUserDto {
long userId;
long lectureId;
String name;
}

View File

@ -8,6 +8,6 @@ import lombok.*;
@NoArgsConstructor
@AllArgsConstructor
public class HelloDto {
long channelId;
long lectureId;
String name;
}

View File

@ -7,5 +7,8 @@ import lombok.Setter;
@Getter
@Setter
public class MessageDto {
int channelId;
long lectureId;
long userId;
String name;
String content;
}

View File

@ -7,7 +7,7 @@ import lombok.Setter;
@Getter
@Setter
public class QuizDto {
int channelId;
int lectureId;
int userId;
int quizSetId;
}

View File

@ -2,19 +2,19 @@ package com.edufocus.edufocus.ws.entity.vo;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.ws.entity.dto.ChatUserDto;
import jakarta.persistence.*;
import lombok.*;
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ChatUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 자동 증가 설정
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column
String sessionId;
@ -22,4 +22,12 @@ public class ChatUser {
User user;
@ManyToOne
Lecture lecture;
public ChatUserDto makeChatUserDto(){
return ChatUserDto.builder()
.userId(user.getId())
.name(user.getName())
.lectureId(lecture.getId())
.build();
}
}

View File

@ -1,2 +1,15 @@
package com.edufocus.edufocus.ws.repository;public interface ChatUserRepository {
package com.edufocus.edufocus.ws.repository;
import com.edufocus.edufocus.ws.entity.vo.ChatUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface ChatUserRepository extends JpaRepository<ChatUser, Long> {
public ChatUser findBySessionId(String sessionId);
public List<ChatUser> findByLectureId(long lectureId);
@Query("delete from ChatUser c where c.lecture.id=:lectureId")
public void deleteByLectureId(long lectureId);
}

View File

@ -1,10 +1,15 @@
package com.edufocus.edufocus.ws.service;
import com.edufocus.edufocus.ws.entity.dto.ResponseChatUserDto;
import com.edufocus.edufocus.ws.entity.dto.ChatUserDto;
import java.util.List;
public interface ChatService {
public void saveChatUserInfo(long userId, long channelId, String sessionId);
public ResponseChatUserDto getChatUserInfo(long userId);
public ChatUserDto getChatUserInfo(long userId);
public ChatUserDto getChatUserInfo(String sessionId);
public List<ChatUserDto> findChatUsers(long lectureId);
public boolean checkTeacher(ChatUserDto chatUser);
public void closeChatRoom(long chatRoomId);
public void deleteChatUserInfo(long userId);
}

View File

@ -1,9 +1,83 @@
package com.edufocus.edufocus.ws.service;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.repository.LectureRepository;
import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.repository.UserRepository;
import com.edufocus.edufocus.ws.entity.dto.ChatUserDto;
import com.edufocus.edufocus.ws.entity.vo.ChatUser;
import com.edufocus.edufocus.ws.repository.ChatUserRepository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ChatServiceImpl implements ChatService{
private final ChatUserRepository chatUserRepository;
private final UserRepository userRepository;
private final LectureRepository lectureRepository;
public ChatServiceImpl(ChatUserRepository chatUserRepository, UserRepository userRepository, LectureRepository lectureRepository) {
this.chatUserRepository = chatUserRepository;
this.userRepository = userRepository;
this.lectureRepository = lectureRepository;
}
@Override
public void saveChatUserInfo(long userId, long lectureId, String sessionId) {
User user = userRepository.getReferenceById(userId);
Lecture lecture = lectureRepository.getReferenceById(lectureId);
ChatUser chatUser = ChatUser.builder()
.user(user)
.lecture(lecture)
.sessionId(sessionId)
.build();
chatUserRepository.save(chatUser);
}
@Override
public ChatUserDto getChatUserInfo(long userId) {
ChatUser chatUser = chatUserRepository.findById(userId).orElseThrow(IllegalArgumentException::new);
return chatUser.makeChatUserDto();
}
@Override
public ChatUserDto getChatUserInfo(String sessionId) {
ChatUser chatUser = chatUserRepository.findBySessionId(sessionId);
return chatUser.makeChatUserDto();
}
@Override
public List<ChatUserDto> findChatUsers(long lectureId) {
return chatUserRepository.findByLectureId(lectureId)
.stream()
.map(ChatUser::makeChatUserDto)
.collect(Collectors.toList());
}
@Override
public boolean checkTeacher(ChatUserDto chatUser) {
Lecture lecture = lectureRepository.findByIdAndUserId(chatUser.getLectureId(), chatUser.getUserId());
return lecture != null;
}
@Override
public void closeChatRoom(long lectureId) {
chatUserRepository.deleteByLectureId(lectureId);
}
@Override
public void deleteChatUserInfo(long userId) {
ChatUser chatUser = chatUserRepository.getReferenceById(userId);
chatUserRepository.delete(chatUser);
}
}