From 9cfa34ed002f23afe2c719b8f1501548963704f7 Mon Sep 17 00:00:00 2001 From: yulmam Date: Wed, 24 Jul 2024 10:22:47 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20header=20=ED=95=84=EC=9A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=A3=BC=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/BoardController.java | 1 + .../board/service/BoardServiceImpl.java | 12 +-- .../lecture/repository/LectureRepository.java | 1 + .../ws/controller/ChatController.java | 65 +++++++++------- .../edufocus/ws/entity/dto/ChatUserDto.java | 14 +++- .../edufocus/ws/entity/dto/HelloDto.java | 2 +- .../edufocus/ws/entity/dto/MessageDto.java | 5 +- .../edufocus/ws/entity/dto/QuizDto.java | 2 +- .../edufocus/ws/entity/vo/ChatUser.java | 12 ++- .../ws/repository/ChatUserRepository.java | 15 +++- .../edufocus/ws/service/ChatService.java | 13 +++- .../edufocus/ws/service/ChatServiceImpl.java | 74 +++++++++++++++++++ 12 files changed, 170 insertions(+), 46 deletions(-) diff --git a/backend/src/main/java/com/edufocus/edufocus/board/controller/BoardController.java b/backend/src/main/java/com/edufocus/edufocus/board/controller/BoardController.java index a1ead74..dd45f9b 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/controller/BoardController.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/controller/BoardController.java @@ -19,6 +19,7 @@ import java.util.List; */ @RestController @RequestMapping("/board") +@CrossOrigin("*") public class BoardController { private final JWTUtil jwtUtil; diff --git a/backend/src/main/java/com/edufocus/edufocus/board/service/BoardServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/board/service/BoardServiceImpl.java index 10426fb..4e047f9 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/service/BoardServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/service/BoardServiceImpl.java @@ -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); } diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java b/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java index a8615d2..eb708fd 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/repository/LectureRepository.java @@ -16,4 +16,5 @@ public interface LectureRepository extends JpaRepository { List findLecturesByUserId(Long userId); + Lecture findByIdAndUserId(long id, long userId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/ws/controller/ChatController.java b/backend/src/main/java/com/edufocus/edufocus/ws/controller/ChatController.java index 30af11a..a5a0e58 100644 --- a/backend/src/main/java/com/edufocus/edufocus/ws/controller/ChatController.java +++ b/backend/src/main/java/com/edufocus/edufocus/ws/controller/ChatController.java @@ -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 chatUsers = chatService.findChatUsers(lectureId); - return new ResponseEntity<>(chatUsers, HttpStatus.OK); - } +// @GetMapping("/chat/enter/{lectureId}") +// public ResponseEntity enter(@PathVariable long lectureId){ +// List 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); +// } } diff --git a/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/ChatUserDto.java b/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/ChatUserDto.java index f418b8f..3830c35 100644 --- a/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/ChatUserDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/ChatUserDto.java @@ -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; } diff --git a/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/HelloDto.java b/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/HelloDto.java index 55a6c4f..6d1eb41 100644 --- a/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/HelloDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/HelloDto.java @@ -8,6 +8,6 @@ import lombok.*; @NoArgsConstructor @AllArgsConstructor public class HelloDto { - long channelId; + long lectureId; String name; } diff --git a/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/MessageDto.java b/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/MessageDto.java index 17522a9..27fea4d 100644 --- a/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/MessageDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/MessageDto.java @@ -7,5 +7,8 @@ import lombok.Setter; @Getter @Setter public class MessageDto { - int channelId; + long lectureId; + long userId; + String name; + String content; } diff --git a/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/QuizDto.java b/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/QuizDto.java index 1900b3d..57dacb6 100644 --- a/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/QuizDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/ws/entity/dto/QuizDto.java @@ -7,7 +7,7 @@ import lombok.Setter; @Getter @Setter public class QuizDto { - int channelId; + int lectureId; int userId; int quizSetId; } diff --git a/backend/src/main/java/com/edufocus/edufocus/ws/entity/vo/ChatUser.java b/backend/src/main/java/com/edufocus/edufocus/ws/entity/vo/ChatUser.java index 7a5422a..7a1d8d6 100644 --- a/backend/src/main/java/com/edufocus/edufocus/ws/entity/vo/ChatUser.java +++ b/backend/src/main/java/com/edufocus/edufocus/ws/entity/vo/ChatUser.java @@ -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(); + } } \ No newline at end of file diff --git a/backend/src/main/java/com/edufocus/edufocus/ws/repository/ChatUserRepository.java b/backend/src/main/java/com/edufocus/edufocus/ws/repository/ChatUserRepository.java index 02408c5..a59fd9e 100644 --- a/backend/src/main/java/com/edufocus/edufocus/ws/repository/ChatUserRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/ws/repository/ChatUserRepository.java @@ -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 { + public ChatUser findBySessionId(String sessionId); + public List findByLectureId(long lectureId); + + @Query("delete from ChatUser c where c.lecture.id=:lectureId") + public void deleteByLectureId(long lectureId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/ws/service/ChatService.java b/backend/src/main/java/com/edufocus/edufocus/ws/service/ChatService.java index 765ddc1..5399bb9 100644 --- a/backend/src/main/java/com/edufocus/edufocus/ws/service/ChatService.java +++ b/backend/src/main/java/com/edufocus/edufocus/ws/service/ChatService.java @@ -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 findChatUsers(long lectureId); + public boolean checkTeacher(ChatUserDto chatUser); + public void closeChatRoom(long chatRoomId); + public void deleteChatUserInfo(long userId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/ws/service/ChatServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/ws/service/ChatServiceImpl.java index 8baccca..7cc7866 100644 --- a/backend/src/main/java/com/edufocus/edufocus/ws/service/ChatServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/ws/service/ChatServiceImpl.java @@ -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 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); + } }