Skip to content

Commit

Permalink
chat endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Strauss committed Aug 2, 2024
1 parent 267b0de commit 7977b15
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/java/com/bernd/ChatController.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/bernd/Chats.java
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());
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/bernd/model/Chat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bernd.model;

public record Chat(
String id,
ChatMessage chatMessage) {
}
6 changes: 6 additions & 0 deletions src/main/java/com/bernd/model/ChatMessage.java
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) {
}
6 changes: 6 additions & 0 deletions src/main/java/com/bernd/model/ChatRequest.java
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) {
}

0 comments on commit 7977b15

Please sign in to comment.