diff --git a/back/src/main/java/be/naaturel/unluckiest/controllers/ScoreController.java b/back/src/main/java/be/naaturel/unluckiest/controllers/ScoreController.java index c413224..c631f38 100644 --- a/back/src/main/java/be/naaturel/unluckiest/controllers/ScoreController.java +++ b/back/src/main/java/be/naaturel/unluckiest/controllers/ScoreController.java @@ -1,8 +1,11 @@ package be.naaturel.unluckiest.controllers; +import be.naaturel.unluckiest.entities.ScoreEntity; import be.naaturel.unluckiest.models.Score; +import be.naaturel.unluckiest.repositories.ScoreRepo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @@ -10,12 +13,32 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class ScoreController { + private final ScoreRepo scoreRepo; + @Autowired - public ScoreController(){ + public ScoreController(ScoreRepo scoreRepo){ + this.scoreRepo = scoreRepo; } @PostMapping("/api/submit") - public ResponseEntity register(@RequestBody Score s){ - return null; + public ResponseEntity submit(@RequestBody Score s){ + + ScoreEntity se = new ScoreEntity(); + se.owner = s.getOwner(); + se.value = s.getValue(); + + try{ + scoreRepo.save(se); + } catch (Exception e){ + return ResponseEntity.internalServerError().build(); + } + + return ResponseEntity.ok().build(); } + + @GetMapping("/api/leaderboard") + public ResponseEntity leaderboard(){ + return ResponseEntity.ok(scoreRepo.findLeaderboard()); + } + } \ No newline at end of file diff --git a/back/src/main/java/be/naaturel/unluckiest/models/Score.java b/back/src/main/java/be/naaturel/unluckiest/models/Score.java index ad4638e..1566ddf 100644 --- a/back/src/main/java/be/naaturel/unluckiest/models/Score.java +++ b/back/src/main/java/be/naaturel/unluckiest/models/Score.java @@ -10,4 +10,12 @@ public class Score { this.value = value; } + public String getOwner(){ + return this.owner; + } + + public int getValue(){ + return this.value; + } + } diff --git a/back/src/main/java/be/naaturel/unluckiest/repositories/ScoreRepo.java b/back/src/main/java/be/naaturel/unluckiest/repositories/ScoreRepo.java new file mode 100644 index 0000000..c1fff7d --- /dev/null +++ b/back/src/main/java/be/naaturel/unluckiest/repositories/ScoreRepo.java @@ -0,0 +1,19 @@ +package be.naaturel.unluckiest.repositories; + +import be.naaturel.unluckiest.entities.ScoreEntity; +import be.naaturel.unluckiest.models.Score; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; +import java.util.Optional; + +public interface ScoreRepo + extends JpaRepository { + + @Query( + value = "SELECT * FROM Score s HAVING 10;", + nativeQuery = true) + + List findLeaderboard(); +} diff --git a/back/src/main/resources/application.properties b/back/src/main/resources/application.properties index 5797206..b066a38 100644 --- a/back/src/main/resources/application.properties +++ b/back/src/main/resources/application.properties @@ -7,9 +7,9 @@ sec.cors.authorizedMethods=GET,POST,PUT,DELETE,OPTION sec.cors.authorizedHeader=Authorization,Content-type #=============DATABASE============= -spring.datasource.url=jdbc:${DB_URL} -spring.datasource.username=${DB_USER} -spring.datasource.password=${DB_PASSWORD} +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/unluckiest_db +spring.datasource.username=root +spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect spring.jpa.show-sql=true diff --git a/front/.idea/vcs.xml b/front/.idea/vcs.xml index 35eb1dd..62bd7a0 100644 --- a/front/.idea/vcs.xml +++ b/front/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/front/.svelte-kit/generated/server/internal.js b/front/.svelte-kit/generated/server/internal.js index 49ace5c..2be0982 100644 --- a/front/.svelte-kit/generated/server/internal.js +++ b/front/.svelte-kit/generated/server/internal.js @@ -21,7 +21,7 @@ export const options = { app: ({ head, body, assets, nonce, env }) => "\n\n\t\n\t\tUnluckiest\n\n\t\t\n\t\t\n\n\t\t\n\t\t\n\n\t\t\n\t\t\n\n\t\t\n\n\t\t" + head + "\n\t\n\t\n\t\t
" + body + "
\n\t\n\n", error: ({ status, message }) => "\n\n\t\n\t\t\n\t\t" + message + "\n\n\t\t\n\t\n\t\n\t\t
\n\t\t\t" + status + "\n\t\t\t
\n\t\t\t\t

" + message + "

\n\t\t\t
\n\t\t
\n\t\n\n" }, - version_hash: "i17ecb" + version_hash: "jr2x5n" }; export async function get_hooks() { diff --git a/front/src/lib/components/LeaderBoard.svelte b/front/src/lib/components/LeaderBoard.svelte index e7db7ad..1fafd38 100644 --- a/front/src/lib/components/LeaderBoard.svelte +++ b/front/src/lib/components/LeaderBoard.svelte @@ -8,7 +8,7 @@ {#each scores as score, i}
  • -
    {score.playerName}
    {score.value} pts.
    +
    {score.owner}
    {score.value} pts.
  • {/each} diff --git a/front/src/lib/stores/requests.ts b/front/src/lib/stores/requests.ts new file mode 100644 index 0000000..c5ce037 --- /dev/null +++ b/front/src/lib/stores/requests.ts @@ -0,0 +1,31 @@ +import {scoreStore} from "./scoreStore"; +import {Score} from "../models/score"; + +export async function submitScore(owner: string, value : number){ + return await handleRequest("http://localhost:8080/api/submit/", + { + method:"POST", + body : JSON.stringify({owner:owner, value:value}), + headers: new Headers({'content-type': 'application/json'}) + }); +} + +export async function getLeaderboard(){ + return await handleRequest("http://localhost:8080/api/leaderboard", + { method:"GET" }); +} + +async function handleRequest(url : string, init : object){ + return await fetch(url, init) + .then(response => { + if(response.status === 500) throw new Error(`${response.text()}`); + if(!response.ok) throw new Error(`${response.status} : ${response.body}`); + return response + }) + .then(response => { + return response.json(); + }) + .catch(error => { + return `Exception: ${error.message}`; + }); +} \ No newline at end of file diff --git a/front/src/lib/stores/scoreStore.ts b/front/src/lib/stores/scoreStore.ts index 3031038..7019cf2 100644 --- a/front/src/lib/stores/scoreStore.ts +++ b/front/src/lib/stores/scoreStore.ts @@ -1,6 +1,7 @@ import {writable} from "svelte/store"; import {Score} from "../models/score"; import {browser} from "$app/environment" +import {getLeaderboard} from "./requests"; let localStorageKey = "scores" @@ -8,13 +9,12 @@ function isBrowser() { return typeof window !== 'undefined' && typeof window.localStorage !== 'undefined'; } -function createStore(){ +function createStore(scores : Score[]){ + //const storedValue = isBrowser() ? localStorage.getItem(localStorageKey) : null; - const storedValue = isBrowser() ? localStorage.getItem(localStorageKey) : null; + const { set, update, subscribe } = writable(scores); - const { set, update, subscribe } = writable(!storedValue ? [] : JSON.parse(storedValue)); - - if (isBrowser()) subscribe(value => localStorage.setItem(localStorageKey, JSON.stringify(value))); + //if (isBrowser()) subscribe(value => localStorage.setItem(localStorageKey, JSON.stringify(value))); return { update, @@ -31,4 +31,9 @@ function createStore(){ }; } -export const scoreStore = createStore(); \ No newline at end of file +async function createStoreFromAPI(){ + let scores = await getLeaderboard(); + return createStore(scores) +} + +export const scoreStore= createStoreFromAPI(); \ No newline at end of file diff --git a/front/src/routes/+page.svelte b/front/src/routes/+page.svelte index 970024d..5097f2e 100644 --- a/front/src/routes/+page.svelte +++ b/front/src/routes/+page.svelte @@ -1,7 +1,18 @@ - \ No newline at end of file +{#if scores} + +{:else} +

    Loading leaderboard...

    +{/if} \ No newline at end of file