From 873f1e367ea8e3c20631ba51b0511df3d3008c3d Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 29 Apr 2025 10:28:21 +0200 Subject: [PATCH] =?UTF-8?q?Hinzugef=C3=BCgt=20Highscore=20abfrage=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/Express/ExpressManager.js | 2 + backend/src/Express/Routes/AccountRoute.js | 8 ++-- backend/src/Express/Routes/DashboardRoute.js | 2 +- backend/src/Express/Routes/HighScoreRoute.js | 48 ++++++++++++++++++++ 4 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 backend/src/Express/Routes/HighScoreRoute.js diff --git a/backend/src/Express/ExpressManager.js b/backend/src/Express/ExpressManager.js index 3c13605..8dbcf0f 100644 --- a/backend/src/Express/ExpressManager.js +++ b/backend/src/Express/ExpressManager.js @@ -5,6 +5,7 @@ const path = require("path"); const DataBaseManager = require("../Database/DataBaseManager"); const AccountRoute = require("./Routes/AccountRoute"); const DashboardRoute = require("./Routes/DashboardRoute"); +const HighScoreRoute = require("./Routes/HighScoreRoute"); require("dotenv").config(); class ExpressManager{ @@ -39,6 +40,7 @@ class ExpressManager{ // Routen Einbinden this.app.use("/api/account/", new AccountRoute(this.db).router); this.app.use("/api/dashboard/", new DashboardRoute(this.db).router); + this.app.use("/api/highscore", new HighScoreRoute(this.db).router); } /**@param {express.Request} req @param {express.Response} res @param {express.NextFunction} next*/ diff --git a/backend/src/Express/Routes/AccountRoute.js b/backend/src/Express/Routes/AccountRoute.js index 566310e..ee4d69f 100644 --- a/backend/src/Express/Routes/AccountRoute.js +++ b/backend/src/Express/Routes/AccountRoute.js @@ -16,7 +16,7 @@ class AccountRoute { this.router.get("/logout", async (req, res) => await this.logout(req, res)); } - /**@param {express.Request} req @param {express.Response}*/ + /**@param {express.Request} req @param {express.Response} res */ async register(req, res){ const body = req.body; if(!body.username || !body.password) return res.redirect("/register?error=1"); @@ -29,7 +29,7 @@ class AccountRoute { res.redirect("/login"); } - /**@param {express.Request} req @param {express.Response}*/ + /**@param {express.Request} req @param {express.Response} res */ async login(req, res){ const body = req.body; if(!body.username || !body.password) return res.redirect("/login?error=1") @@ -49,7 +49,7 @@ class AccountRoute { res.redirect("/dashboard") } - /**@param {express.Request} req @param {express.Response}*/ + /**@param {express.Request} req @param {express.Response} res*/ async update(req, res){ const user = await this.db.usermanager.getUser({id: req.session.user.id}); const body = req.body; @@ -74,7 +74,7 @@ class AccountRoute { res.redirect("/dashboard/account"); } - /**@param {express.Request} req @param {express.Response}*/ + /**@param {express.Request} req @param {express.Response} res*/ async logout(req, res){ req.session.destroy(); res.redirect("/"); diff --git a/backend/src/Express/Routes/DashboardRoute.js b/backend/src/Express/Routes/DashboardRoute.js index e39762d..e072a25 100644 --- a/backend/src/Express/Routes/DashboardRoute.js +++ b/backend/src/Express/Routes/DashboardRoute.js @@ -12,7 +12,7 @@ class DashboardRoute{ this.router.get("/", async (req, res) => await this.userInfo(req, res)) } - /**@param {express.Request} req @param {express.Response}*/ + /**@param {express.Request} req @param {express.Response} res*/ async userInfo(req, res){ const user = await this.db.usermanager.getUser({id: req.session.user.id}); diff --git a/backend/src/Express/Routes/HighScoreRoute.js b/backend/src/Express/Routes/HighScoreRoute.js new file mode 100644 index 0000000..cfcf58a --- /dev/null +++ b/backend/src/Express/Routes/HighScoreRoute.js @@ -0,0 +1,48 @@ +const express = require("express"); +const DataBaseManager = require("../../Database/DataBaseManager"); +const Score = require("../../Database/ScoreManager/Score"); + +class HighScoreRoute { + /**@param {DataBaseManager} dbManager*/ + constructor(dbManager) { + this.router = express.Router(); + + this.db = dbManager; + + this.router.get("/", async (req, res) => await this.getHighscores(req, res)); + } + + /**@param {express.Request} req @param {express.Response} res*/ + async getHighscores(req, res){ + /** @type {Array} */ + const highscores = await this.db.scoremanager.getAllScores(); + + let returnJSON = { + scores: [] + } + + for(const highscore of highscores){ + const user1 = await this.db.usermanager.getUser({id: highscore.user1ID}); + const user2 = await this.db.usermanager.getUser({id: highscore.user2ID}); + + returnJSON.scores.push({ + id: highscore.id, + score: highscore.score, + user1: { + id: user1.id, + username: user1.username, + fullName: user1.fullName + }, + user2: { + id: user2.id, + username: user2.username, + fullName: user2.fullName + }, + }); + } + + res.json(returnJSON); + } +} + +module.exports = HighScoreRoute; \ No newline at end of file