Skip to content

Commit d35f01b

Browse files
author
Christopher Strauss
committed
chat endpoints
1 parent 267b0de commit d35f01b

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.bernd;
2+
3+
import com.bernd.model.Chat;
4+
import com.bernd.model.ChatRequest;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
12+
@Controller
13+
public class ChatController {
14+
15+
@Autowired
16+
Chats chats;
17+
18+
@GetMapping("/chat")
19+
public Chat getChat(@RequestBody String id) {
20+
return chats.get(id);
21+
}
22+
23+
@PostMapping("/sendChat")
24+
public ResponseEntity<?> sendChat(@RequestBody ChatRequest chatRequest) {
25+
chats.put(chatRequest.chat());
26+
return ResponseEntity.ok().build();
27+
}
28+
29+
}

src/main/java/com/bernd/Chats.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.bernd;
2+
3+
import com.bernd.model.Chat;
4+
import com.bernd.model.Game;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.util.LinkedHashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
@Component
12+
public class Chats {
13+
private final Map<String, Chat> map = new LinkedHashMap<>();
14+
15+
Chat get(String id) {
16+
return map.get(id);
17+
}
18+
19+
Chat put(Chat chat) {
20+
map.put(chat.getId(), chat);
21+
return chat;
22+
}
23+
24+
List<Chat> chats() {
25+
return List.copyOf(map.values());
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.bernd.model;
2+
3+
import java.util.List;
4+
5+
public class Chat {
6+
7+
private String id;
8+
private List<ChatMessage> chatMessages;
9+
10+
public Chat(){
11+
}
12+
13+
public Chat(String id, List<ChatMessage> chatMessages){
14+
this.id = id;
15+
this.chatMessages = chatMessages;
16+
}
17+
18+
public String getId() {
19+
return id;
20+
}
21+
22+
public void setId(String id) {
23+
this.id = id;
24+
}
25+
26+
public List<ChatMessage> getChatMessages() {
27+
return chatMessages;
28+
}
29+
30+
public void setChatMessages(List<ChatMessage> chatMessages) {
31+
this.chatMessages = chatMessages;
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.bernd.model;
2+
3+
public class ChatMessage {
4+
5+
private String message;
6+
private String messageId;
7+
8+
public ChatMessage(String message, String messageId) {
9+
this.message = message;
10+
this.messageId = messageId;
11+
}
12+
13+
public String getMessage() {
14+
return message;
15+
}
16+
17+
public void setMessage(String message) {
18+
this.message = message;
19+
}
20+
21+
public String getMessageId() {
22+
return messageId;
23+
}
24+
25+
public void setMessageId(String messageId) {
26+
this.messageId = messageId;
27+
}
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.bernd.model;
2+
3+
public record ChatRequest(
4+
Chat chat) {
5+
}

0 commit comments

Comments
 (0)