Skip to content

Commit

Permalink
Add basic skeleton for the views
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriandmen committed May 22, 2021
1 parent 67310e8 commit 3f89193
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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";
}
}
19 changes: 19 additions & 0 deletions src/main/resources/static/main.js
Original file line number Diff line number Diff line change
@@ -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");
}
}
3 changes: 3 additions & 0 deletions src/main/resources/static/welcome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function setUsername() {
document.getElementById("player-username").innerHTML = localStorage.getItem("username");
}
4 changes: 3 additions & 1 deletion src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
</head>
<body>
<div>
Hello, World!
<label for="username">Username: </label>
<input type="text" id="username" value="">
<input type="submit" onclick="submitUsername()">
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions src/main/resources/templates/welcome.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Le Crocodile</title>
<link rel="stylesheet" href="style.css"/>
<script src="welcome.js"></script>
</head>
<body>
<p>Welcome <span id="player-username"></span></p>

<script>
setUsername();
</script>
</body>
</html>
5 changes: 2 additions & 3 deletions src/test/java/it/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/test/java/it/PlayerControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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));

Expand Down

0 comments on commit 3f89193

Please sign in to comment.