-
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
1 parent
9abd725
commit 9225bd6
Showing
6 changed files
with
101 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.bernd; | ||
|
||
import com.bernd.model.Chat; | ||
import com.bernd.model.ChatMessage; | ||
import com.bernd.model.ChatRequest; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.messaging.core.MessageSendingOperations; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@Controller | ||
public class ChatController { | ||
|
||
private final Chats chats; | ||
private final MessageSendingOperations<String> operations; | ||
|
||
ChatController( | ||
Chats chats, | ||
MessageSendingOperations<String> operations) { | ||
this.chats = chats; | ||
this.operations = operations; | ||
} | ||
|
||
@ResponseBody | ||
@GetMapping("/api/chat/{id}") | ||
public Chat getChat(@PathVariable String id) { | ||
return chats.get(id); | ||
} | ||
|
||
@PostMapping("/api/send_chat") | ||
public ResponseEntity<?> sendChat(@RequestBody ChatRequest chatRequest) { | ||
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
String user = Objects.toString(principal); | ||
ChatMessage message = new ChatMessage(chatRequest.message(), user); | ||
Chat chat = chats.get(chatRequest.id()); | ||
if (chat != null) { | ||
chat.messages().add(message); | ||
} else { | ||
List<ChatMessage> messages = new ArrayList<>(); | ||
messages.add(message); | ||
chat = new Chat(chatRequest.id(), messages); | ||
} | ||
chats.put(chat); | ||
operations.convertAndSend("/topic/chat/" + chat.id(), chat); | ||
return ResponseEntity.ok().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,25 @@ | ||
package com.bernd; | ||
|
||
import com.bernd.model.Chat; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class Chats { | ||
private final Map<String, Chat> map = new LinkedHashMap<>(); | ||
|
||
Chat get(String id) { | ||
return map.get(id); | ||
} | ||
|
||
Chat put(Chat chat) { | ||
map.put(chat.id(), chat); | ||
return chat; | ||
} | ||
|
||
List<Chat> chats() { | ||
return List.copyOf(map.values()); | ||
} | ||
} |
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,8 @@ | ||
package com.bernd.model; | ||
|
||
import java.util.List; | ||
|
||
public record Chat( | ||
String id, | ||
List<ChatMessage> messages) { | ||
} |
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,6 @@ | ||
package com.bernd.model; | ||
|
||
public record ChatMessage( | ||
String message, | ||
String user) { | ||
} |
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,6 @@ | ||
package com.bernd.model; | ||
|
||
public record ChatRequest( | ||
String message, | ||
String id) { | ||
} |