From 7977b15ab38f19f9e23a56935ecc52b380c3a484 Mon Sep 17 00:00:00 2001 From: Christopher Strauss Date: Fri, 2 Aug 2024 09:25:06 +0200 Subject: [PATCH] chat endpoints --- src/main/java/com/bernd/ChatController.java | 39 +++++++++++++++++++ src/main/java/com/bernd/Chats.java | 25 ++++++++++++ src/main/java/com/bernd/model/Chat.java | 6 +++ .../java/com/bernd/model/ChatMessage.java | 6 +++ .../java/com/bernd/model/ChatRequest.java | 6 +++ 5 files changed, 82 insertions(+) create mode 100644 src/main/java/com/bernd/ChatController.java create mode 100644 src/main/java/com/bernd/Chats.java create mode 100644 src/main/java/com/bernd/model/Chat.java create mode 100644 src/main/java/com/bernd/model/ChatMessage.java create mode 100644 src/main/java/com/bernd/model/ChatRequest.java diff --git a/src/main/java/com/bernd/ChatController.java b/src/main/java/com/bernd/ChatController.java new file mode 100644 index 0000000..a9ac27d --- /dev/null +++ b/src/main/java/com/bernd/ChatController.java @@ -0,0 +1,39 @@ +package com.bernd; + +import com.bernd.model.Chat; +import com.bernd.model.ChatMessage; +import com.bernd.model.ChatRequest; +import java.util.Objects; +import org.springframework.http.ResponseEntity; +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 { + + final Chats chats; + + ChatController(Chats chats) { + this.chats = chats; + } + + @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); + chats.put(new Chat(chatRequest.id(), new ChatMessage(chatRequest.message(), user))); + return ResponseEntity.ok().build(); + } + +} diff --git a/src/main/java/com/bernd/Chats.java b/src/main/java/com/bernd/Chats.java new file mode 100644 index 0000000..663d07b --- /dev/null +++ b/src/main/java/com/bernd/Chats.java @@ -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 map = new LinkedHashMap<>(); + + Chat get(String id) { + return map.get(id); + } + + Chat put(Chat chat) { + map.put(chat.id(), chat); + return chat; + } + + List chats() { + return List.copyOf(map.values()); + } +} diff --git a/src/main/java/com/bernd/model/Chat.java b/src/main/java/com/bernd/model/Chat.java new file mode 100644 index 0000000..3f908e2 --- /dev/null +++ b/src/main/java/com/bernd/model/Chat.java @@ -0,0 +1,6 @@ +package com.bernd.model; + +public record Chat( + String id, + ChatMessage chatMessage) { +} diff --git a/src/main/java/com/bernd/model/ChatMessage.java b/src/main/java/com/bernd/model/ChatMessage.java new file mode 100644 index 0000000..b4179d7 --- /dev/null +++ b/src/main/java/com/bernd/model/ChatMessage.java @@ -0,0 +1,6 @@ +package com.bernd.model; + +public record ChatMessage( + String message, + String user) { +} diff --git a/src/main/java/com/bernd/model/ChatRequest.java b/src/main/java/com/bernd/model/ChatRequest.java new file mode 100644 index 0000000..7fa03df --- /dev/null +++ b/src/main/java/com/bernd/model/ChatRequest.java @@ -0,0 +1,6 @@ +package com.bernd.model; + +public record ChatRequest( + String message, + String id) { +}