Hinzugefügt Highscore abfrage API

This commit is contained in:
2025-04-29 10:28:21 +02:00
parent decd4193c3
commit 873f1e367e
4 changed files with 55 additions and 5 deletions

View File

@@ -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*/

View File

@@ -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("/");

View File

@@ -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});

View File

@@ -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<Score>} */
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;