-
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
Christopher Strauss
committed
Aug 2, 2024
1 parent
267b0de
commit eedfcad
Showing
5 changed files
with
132 additions
and
0 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,38 @@ | ||
package com.bernd; | ||
|
||
import com.bernd.model.Chat; | ||
import com.bernd.model.ChatMessage; | ||
import com.bernd.model.ChatRequest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.Objects; | ||
|
||
@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); | ||
} | ||
|
||
@ResponseBody | ||
@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.gameId(), new ChatMessage(chatRequest.message(), user))); | ||
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,27 @@ | ||
package com.bernd; | ||
|
||
import com.bernd.model.Chat; | ||
import com.bernd.model.Game; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@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.getId(), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.bernd.model; | ||
|
||
import java.util.List; | ||
|
||
public class Chat { | ||
|
||
private String id; | ||
private ChatMessage chatMessage; | ||
|
||
public Chat(){ | ||
} | ||
|
||
public Chat(String id, ChatMessage chatMessage){ | ||
this.id = id; | ||
this.chatMessage = chatMessage; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public ChatMessage getChatMessage() { | ||
return chatMessage; | ||
} | ||
|
||
public void setChatMessage(ChatMessage chatMessage) { | ||
this.chatMessage = chatMessage; | ||
} | ||
} |
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,28 @@ | ||
package com.bernd.model; | ||
|
||
public class ChatMessage { | ||
|
||
private String message; | ||
private String userId; | ||
|
||
public ChatMessage(String message, String userId) { | ||
this.message = message; | ||
this.userId = userId; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public void setUserId(String userId) { | ||
this.userId = userId; | ||
} | ||
} |
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 gameId) { | ||
} |