feat : chatController 추가

This commit is contained in:
yulmam 2024-07-22 17:32:23 +09:00
parent 1a556ff91f
commit 4b483eea2d
10 changed files with 187 additions and 16 deletions

View File

@ -30,22 +30,18 @@ public class BoardController {
}
@GetMapping()
public ResponseEntity<?> searchBoards(
public ResponseEntity<List<ResponseBoardSummaryDto>> searchBoards(
@RequestParam(value = "category", required = false, defaultValue = "announcement") String category,
@RequestParam(value = "lectureId", required = true) long lectureId,
@RequestParam(value = "lectureId") long lectureId,
@RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo
){
List<ResponseBoardSummaryDto> boardSummaries = boardService.findBoards(pageNo, category, lectureId);
if(boardSummaries.isEmpty()){
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(boardSummaries, HttpStatus.OK);
}
@GetMapping(value = "/{boardId}")
public ResponseEntity<?> getBoardDetail(
public ResponseEntity<ResponseBoardDetailDto> getBoardDetail(
@PathVariable int boardId
){
ResponseBoardDetailDto responseBoardDetailDto = boardService.findBoardDetail(boardId);
@ -87,7 +83,7 @@ public class BoardController {
}
@GetMapping(value = "/comment/{boardId}")
public ResponseEntity<?> getComments(
public ResponseEntity<List<ResponseCommentDto>> getComments(
@PathVariable int boardId
){
List<ResponseCommentDto> comments = boardService.findComments(boardId);
@ -127,4 +123,10 @@ public class BoardController {
return new ResponseEntity<>(HttpStatus.OK);
}
@ExceptionHandler()
public ResponseEntity<?> NoContentException(Exception exception){
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}

View File

@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
@Service
@ -49,14 +50,14 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public ResponseBoardDetailDto findBoardDetail(long boardId) {
return boardRepository.findById(boardId)
.orElseThrow()
.orElseThrow(NoSuchElementException::new)
.makeDetailDto();
}
@Transactional
public void createBoard(long userId, RequestBoardDto requestBoardDto) {
User user = userRepository.findById(userId).orElseThrow();
Lecture lecture = lectureRepository.findById(requestBoardDto.getLectureId()).get();
Lecture lecture = lectureRepository.findById(requestBoardDto.getLectureId()).orElseThrow(IllegalArgumentException::new);
Board board = Board.builder()
.title(requestBoardDto.getTitle())
@ -71,7 +72,7 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto) {
Board board = boardRepository.findById(boardId).get();
Board board = boardRepository.findById(boardId).orElseThrow(IllegalArgumentException::new);
board.setTitle(requestBoardUpdateDto.getTitle());
board.setContent(requestBoardUpdateDto.getContent());
@ -82,7 +83,7 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public void deleteBoard(long boardId) {
Board board = boardRepository.findById(boardId).get();
Board board = boardRepository.findById(boardId).orElseThrow(IllegalArgumentException::new);
boardRepository.delete(board);
}
@ -96,8 +97,8 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public void createComment(long userId, long boardId, RequestCommentDto requestCommentDto) {
User user = userRepository.findById(userId).get();
Board board = boardRepository.findById(boardId).get();
User user = userRepository.findById(userId).orElseThrow(IllegalArgumentException::new);
Board board = boardRepository.findById(boardId).orElseThrow(IllegalArgumentException::new);
Comment comment = Comment.builder()
.content(requestCommentDto.getContent())
@ -112,7 +113,7 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public void updateComment(long commentId, RequestCommentDto requestCommentDto) {
Comment comment = commentRepository.findById(commentId).get();
Comment comment = commentRepository.findById(commentId).orElseThrow(IllegalArgumentException::new);
comment.setContent(requestCommentDto.getContent());
@ -121,7 +122,7 @@ public class BoardServiceImpl implements BoardService {
@Transactional
public void deleteComment(long commentId) {
Comment comment = commentRepository.findById(commentId).get();
Comment comment = commentRepository.findById(commentId).orElseThrow(NoSuchElementException::new);
commentRepository.delete(comment);
}

View File

@ -0,0 +1,81 @@
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.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;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
import java.util.List;
@RestController
public class ChatController {
private final SimpMessageSendingOperations simpMessageSendingOperations;
private final ChatService chatService;
private final JWTUtil jwtUtil;
public ChatController(SimpMessageSendingOperations simpMessageSendingOperations, ChatService chatService, JWTUtil jwtUtil){
this.simpMessageSendingOperations = simpMessageSendingOperations;
this.chatService = chatService;
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);
}
@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("/message/{lectureId}")
@SendTo("/sub/channel/{lectureId}")
public MessageDto sendMessage(@DestinationVariable long lectureId, MessageDto messageDto){return messageDto;}
@MessageMapping("/quiz/{lectureId}")
@SendTo("/sub/channel/{lectureId}")
public QuizDto quizStart(@DestinationVariable long lectureId, QuizDto quizDto){
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);
}
}

View File

@ -0,0 +1,13 @@
package com.edufocus.edufocus.ws.entity.dto;
import lombok.*;
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class HelloDto {
long channelId;
String name;
}

View File

@ -0,0 +1,11 @@
package com.edufocus.edufocus.ws.entity.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MessageDto {
int channelId;
}

View File

@ -0,0 +1,13 @@
package com.edufocus.edufocus.ws.entity.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class QuizDto {
int channelId;
int userId;
int quizSetId;
}

View File

@ -0,0 +1,6 @@
package com.edufocus.edufocus.ws.entity.dto;
public class ResponseChatUserDto {
int userId;
int name;
}

View File

@ -0,0 +1,25 @@
package com.edufocus.edufocus.ws.entity.vo;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.user.model.entity.User;
import jakarta.persistence.*;
import lombok.*;
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ChatUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 자동 증가 설정
int id;
@Column
String sessionId;
@OneToOne
User user;
@ManyToOne
Lecture lecture;
}

View File

@ -0,0 +1,10 @@
package com.edufocus.edufocus.ws.service;
import com.edufocus.edufocus.ws.entity.dto.ResponseChatUserDto;
public interface ChatService {
public void saveChatUserInfo(long userId, long channelId, String sessionId);
public ResponseChatUserDto getChatUserInfo(long userId);
}

View File

@ -0,0 +1,9 @@
package com.edufocus.edufocus.ws.service;
import org.springframework.stereotype.Service;
@Service
public class ChatServiceImpl implements ChatService{
}