-
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 7977b15
Showing
5 changed files
with
82 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,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(); | ||
} | ||
|
||
} |
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
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) { | ||
} |
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) { | ||
} |