forked from codesquad-members-2023/second-hand-max
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
304 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
be/src/main/java/kr/codesquad/chat/controller/ChatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package kr.codesquad.chat.controller; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import kr.codesquad.chat.dto.ChatRoomCreateRequest; | ||
import kr.codesquad.chat.service.ChatService; | ||
import kr.codesquad.util.Constants; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class ChatController { | ||
|
||
private final ChatService chatService; | ||
|
||
@PostMapping("/") | ||
public ResponseEntity<Void> createChatRoom(@RequestBody ChatRoomCreateRequest chatRoomCreateRequest, | ||
HttpServletRequest request) { | ||
String loginId = (String)request.getAttribute(Constants.LOGIN_ID); | ||
chatService.createRoom(chatRoomCreateRequest, loginId); | ||
return ResponseEntity.status(HttpStatus.CREATED).build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package kr.codesquad.chat.dto; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
import kr.codesquad.chat.entity.ChatRoom; | ||
|
||
@Mapper | ||
public interface ChatMapper { | ||
ChatMapper INSTANCE = Mappers.getMapper(ChatMapper.class); | ||
|
||
@Mapping(target = "senderId", source = "userId") | ||
ChatRoom toChatRoom(ChatRoomCreateRequest chatRoomCreateRequest, Long userId); | ||
} |
8 changes: 8 additions & 0 deletions
8
be/src/main/java/kr/codesquad/chat/dto/ChatRoomCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package kr.codesquad.chat.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ChatRoomCreateRequest { | ||
private Long itemId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
be/src/main/java/kr/codesquad/chat/repository/ChatMessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package kr.codesquad.chat.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import kr.codesquad.chat.entity.ChatMessage; | ||
|
||
public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
be/src/main/java/kr/codesquad/chat/repository/MessageRepository.java
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
be/src/main/java/kr/codesquad/chat/service/ChatService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package kr.codesquad.chat.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import kr.codesquad.chat.dto.ChatMapper; | ||
import kr.codesquad.chat.dto.ChatRoomCreateRequest; | ||
import kr.codesquad.chat.repository.ChatMessageRepository; | ||
import kr.codesquad.chat.repository.ChatRoomRepository; | ||
import kr.codesquad.user.entity.User; | ||
import kr.codesquad.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class ChatService { | ||
|
||
private final ChatRoomRepository chatRoomRepository; | ||
private final ChatMessageRepository chatMessageRepository; | ||
private final UserRepository userRepository; | ||
|
||
public void createRoom(ChatRoomCreateRequest chatRoomCreateRequest, String loginId) { | ||
User user = userRepository.findByLoginId(loginId); | ||
chatRoomRepository.save(ChatMapper.INSTANCE.toChatRoom(chatRoomCreateRequest, user.getId())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
be/src/main/java/kr/codesquad/core/config/WebSocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package kr.codesquad.core.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocket; | ||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; | ||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; | ||
|
||
import kr.codesquad.core.websocket.WebSocketHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
@EnableWebSocket | ||
public class WebSocketConfig implements WebSocketConfigurer { | ||
private final WebSocketHandler webSocketHandler; | ||
|
||
@Override | ||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { | ||
registry.addHandler(webSocketHandler, "/ws/chat").setAllowedOrigins("*"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package kr.codesquad.core.websocket; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class Message { | ||
private String roomId; | ||
private String sender; | ||
private String message; | ||
} |
29 changes: 29 additions & 0 deletions
29
be/src/main/java/kr/codesquad/core/websocket/MsgController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package kr.codesquad.core.websocket; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/chat") | ||
public class MsgController { | ||
private final MsgService msgService; | ||
|
||
@PostMapping | ||
public MsgRoom createRoom(@RequestParam String name) { | ||
return msgService.createRoom(name); | ||
} | ||
|
||
@GetMapping | ||
public List<MsgRoom> findAllRoom() { | ||
return msgService.findAllRoom(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package kr.codesquad.core.websocket; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
public class MsgRoom { | ||
private String roomId; | ||
private Set<WebSocketSession> sessions = new HashSet<>(); | ||
|
||
@Builder | ||
public MsgRoom(String roomId) { | ||
this.roomId = roomId; | ||
} | ||
|
||
public void handleActions(WebSocketSession session, Message message, MsgService msgService) { | ||
sessions.add(session); | ||
sendMessage(message, msgService); | ||
} | ||
|
||
public <T> void sendMessage(T message, MsgService messageService) { | ||
sessions.parallelStream().forEach(session -> messageService.sendMessage(session, message)); | ||
} | ||
|
||
} | ||
|
52 changes: 52 additions & 0 deletions
52
be/src/main/java/kr/codesquad/core/websocket/MsgService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package kr.codesquad.core.websocket; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class MsgService { | ||
private final ObjectMapper objectMapper; | ||
private Map<String, MsgRoom> msgRooms; | ||
|
||
@PostConstruct | ||
private void init() { | ||
msgRooms = new LinkedHashMap<>(); | ||
} | ||
|
||
public List<MsgRoom> findAllRoom() { | ||
return new ArrayList<>(msgRooms.values()); | ||
} | ||
|
||
public MsgRoom findById(String roomId) { | ||
return msgRooms.get(roomId); | ||
} | ||
|
||
public MsgRoom createRoom(String name) { | ||
String roomId = name; | ||
return MsgRoom.builder().roomId(roomId).build(); | ||
} | ||
|
||
public <T> void sendMessage(WebSocketSession session, T message) { | ||
try { | ||
session.sendMessage(new TextMessage(objectMapper.writeValueAsString(message))); | ||
} catch (IOException e) { | ||
log.error(e.getMessage(), e); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
be/src/main/java/kr/codesquad/core/websocket/WebSocketHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package kr.codesquad.core.websocket; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
import org.springframework.web.socket.handler.TextWebSocketHandler; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class WebSocketHandler extends TextWebSocketHandler { | ||
|
||
private final MsgService msgService; | ||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { | ||
String payload = message.getPayload(); | ||
log.info("payload:{}", payload); | ||
|
||
Message msg = objectMapper.readValue(payload, Message.class); | ||
MsgRoom room = msgService.findById(msg.getRoomId()); | ||
room.handleActions(session, msg, msgService); | ||
} | ||
} |
Oops, something went wrong.