From 3f8919352bf105d1646c63e0d49770bd10f7a3f2 Mon Sep 17 00:00:00 2001 From: Adriandmen Date: Sat, 22 May 2021 15:44:01 +0200 Subject: [PATCH] Add basic skeleton for the views --- .../controller/api/PlayerController.java | 2 +- .../controller/web/MainController.java | 13 +++++++++++++ src/main/resources/static/main.js | 19 +++++++++++++++++++ src/main/resources/static/welcome.js | 3 +++ src/main/resources/templates/index.html | 4 +++- src/main/resources/templates/welcome.html | 16 ++++++++++++++++ src/test/java/it/IntegrationTest.java | 5 ++--- src/test/java/it/PlayerControllerTest.java | 4 ++-- 8 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 src/main/resources/static/main.js create mode 100644 src/main/resources/static/welcome.js create mode 100644 src/main/resources/templates/welcome.html diff --git a/src/main/java/nl/adrianmensing/krokodil/controller/api/PlayerController.java b/src/main/java/nl/adrianmensing/krokodil/controller/api/PlayerController.java index 6e5bd06..107e956 100644 --- a/src/main/java/nl/adrianmensing/krokodil/controller/api/PlayerController.java +++ b/src/main/java/nl/adrianmensing/krokodil/controller/api/PlayerController.java @@ -43,7 +43,7 @@ public ResponseEntity requestInfo(@RequestParam(value = "user_id") String use return new JSONResponse<>(player).build(); } - @PutMapping("/username") + @PostMapping("/username") public ResponseEntity username(@CookieValue(value = "session_id") String playerID, @RequestParam(value = "username") String username) { Player player = PlayerDataManager.getPlayerByID(playerID); diff --git a/src/main/java/nl/adrianmensing/krokodil/controller/web/MainController.java b/src/main/java/nl/adrianmensing/krokodil/controller/web/MainController.java index 450fb9e..b8a9f34 100644 --- a/src/main/java/nl/adrianmensing/krokodil/controller/web/MainController.java +++ b/src/main/java/nl/adrianmensing/krokodil/controller/web/MainController.java @@ -3,11 +3,13 @@ import nl.adrianmensing.krokodil.controller.ControllerUtils; import nl.adrianmensing.krokodil.database.manager.PlayerDataManager; import nl.adrianmensing.krokodil.logic.Player; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.GetMapping; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; @Controller public class MainController { @@ -21,4 +23,15 @@ public String hello(@CookieValue(value = "session_id", defaultValue = "") String return "index"; } + + @GetMapping("/welcome") + public String welcome(@CookieValue(value = "session_id", required = false) String sessionID, + HttpServletResponse response) { + if (sessionID == null || PlayerDataManager.getPlayerByID(sessionID) == null) { + response.setHeader("Location", "/"); + response.setStatus(HttpStatus.FOUND.value()); + } + + return "welcome"; + } } diff --git a/src/main/resources/static/main.js b/src/main/resources/static/main.js new file mode 100644 index 0000000..e18b570 --- /dev/null +++ b/src/main/resources/static/main.js @@ -0,0 +1,19 @@ +function submitUsername() { + let username = document.getElementById("username").value; + + if (/^[0-9A-Za-z][0-9A-Za-z ]*/.test(username) && username.length > 1) { + let formData = new FormData(); + formData.append("username", username); + fetch("/api/player/username", { + method: "POST", + body: formData + }).then(res => { + if (res.ok) { + window.localStorage.setItem("username", username); + window.location.replace("/welcome"); + } + }) + } else { + alert("Invalid username"); + } +} \ No newline at end of file diff --git a/src/main/resources/static/welcome.js b/src/main/resources/static/welcome.js new file mode 100644 index 0000000..df56024 --- /dev/null +++ b/src/main/resources/static/welcome.js @@ -0,0 +1,3 @@ +function setUsername() { + document.getElementById("player-username").innerHTML = localStorage.getItem("username"); +} diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index c3558dc..4882b9b 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -11,7 +11,9 @@
- Hello, World! + + +
diff --git a/src/main/resources/templates/welcome.html b/src/main/resources/templates/welcome.html new file mode 100644 index 0000000..28e4f2a --- /dev/null +++ b/src/main/resources/templates/welcome.html @@ -0,0 +1,16 @@ + + + + + Le Crocodile + + + + +

Welcome

+ + + + \ No newline at end of file diff --git a/src/test/java/it/IntegrationTest.java b/src/test/java/it/IntegrationTest.java index 7edd88e..a9e9581 100644 --- a/src/test/java/it/IntegrationTest.java +++ b/src/test/java/it/IntegrationTest.java @@ -6,8 +6,7 @@ import javax.servlet.http.Cookie; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @AutoConfigureMockMvc public abstract class IntegrationTest { @@ -24,7 +23,7 @@ Cookie generateNewPlayerCookie() throws Exception { Cookie generateNewPlayerCookie(String username) throws Exception { Cookie session_id = generateNewPlayerCookie(); - mvc.perform(put("/api/player/username").cookie(session_id).param("username", username)); + mvc.perform(post("/api/player/username").cookie(session_id).param("username", username)); return session_id; } } diff --git a/src/test/java/it/PlayerControllerTest.java b/src/test/java/it/PlayerControllerTest.java index 7416bdd..1ffc233 100644 --- a/src/test/java/it/PlayerControllerTest.java +++ b/src/test/java/it/PlayerControllerTest.java @@ -56,7 +56,7 @@ public void GetPlayerInfoTest() throws Exception { @Test public void SetUsernameForPlayer() throws Exception { Cookie session_id = generateNewPlayerCookie(); - mvc.perform(put("/api/player/username").cookie(session_id).param("username", "Adnan")) + mvc.perform(post("/api/player/username").cookie(session_id).param("username", "Adnan")) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)); @@ -81,7 +81,7 @@ public void CreateNewGameTest() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)); - mvc.perform(put("/api/player/username").cookie(session_id).param("username", "Adnan")) + mvc.perform(post("/api/player/username").cookie(session_id).param("username", "Adnan")) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));