Hinzugefügt Highscore abfrage API
This commit is contained in:
@@ -5,6 +5,7 @@ const path = require("path");
|
|||||||
const DataBaseManager = require("../Database/DataBaseManager");
|
const DataBaseManager = require("../Database/DataBaseManager");
|
||||||
const AccountRoute = require("./Routes/AccountRoute");
|
const AccountRoute = require("./Routes/AccountRoute");
|
||||||
const DashboardRoute = require("./Routes/DashboardRoute");
|
const DashboardRoute = require("./Routes/DashboardRoute");
|
||||||
|
const HighScoreRoute = require("./Routes/HighScoreRoute");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
|
||||||
class ExpressManager{
|
class ExpressManager{
|
||||||
@@ -39,6 +40,7 @@ class ExpressManager{
|
|||||||
// Routen Einbinden
|
// Routen Einbinden
|
||||||
this.app.use("/api/account/", new AccountRoute(this.db).router);
|
this.app.use("/api/account/", new AccountRoute(this.db).router);
|
||||||
this.app.use("/api/dashboard/", new DashboardRoute(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*/
|
/**@param {express.Request} req @param {express.Response} res @param {express.NextFunction} next*/
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class AccountRoute {
|
|||||||
this.router.get("/logout", async (req, res) => await this.logout(req, res));
|
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){
|
async register(req, res){
|
||||||
const body = req.body;
|
const body = req.body;
|
||||||
if(!body.username || !body.password) return res.redirect("/register?error=1");
|
if(!body.username || !body.password) return res.redirect("/register?error=1");
|
||||||
@@ -29,7 +29,7 @@ class AccountRoute {
|
|||||||
res.redirect("/login");
|
res.redirect("/login");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**@param {express.Request} req @param {express.Response}*/
|
/**@param {express.Request} req @param {express.Response} res */
|
||||||
async login(req, res){
|
async login(req, res){
|
||||||
const body = req.body;
|
const body = req.body;
|
||||||
if(!body.username || !body.password) return res.redirect("/login?error=1")
|
if(!body.username || !body.password) return res.redirect("/login?error=1")
|
||||||
@@ -49,7 +49,7 @@ class AccountRoute {
|
|||||||
res.redirect("/dashboard")
|
res.redirect("/dashboard")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**@param {express.Request} req @param {express.Response}*/
|
/**@param {express.Request} req @param {express.Response} res*/
|
||||||
async update(req, res){
|
async update(req, res){
|
||||||
const user = await this.db.usermanager.getUser({id: req.session.user.id});
|
const user = await this.db.usermanager.getUser({id: req.session.user.id});
|
||||||
const body = req.body;
|
const body = req.body;
|
||||||
@@ -74,7 +74,7 @@ class AccountRoute {
|
|||||||
res.redirect("/dashboard/account");
|
res.redirect("/dashboard/account");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**@param {express.Request} req @param {express.Response}*/
|
/**@param {express.Request} req @param {express.Response} res*/
|
||||||
async logout(req, res){
|
async logout(req, res){
|
||||||
req.session.destroy();
|
req.session.destroy();
|
||||||
res.redirect("/");
|
res.redirect("/");
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class DashboardRoute{
|
|||||||
this.router.get("/", async (req, res) => await this.userInfo(req, res))
|
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){
|
async userInfo(req, res){
|
||||||
const user = await this.db.usermanager.getUser({id: req.session.user.id});
|
const user = await this.db.usermanager.getUser({id: req.session.user.id});
|
||||||
|
|
||||||
|
|||||||
48
backend/src/Express/Routes/HighScoreRoute.js
Normal file
48
backend/src/Express/Routes/HighScoreRoute.js
Normal 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;
|
||||||
Reference in New Issue
Block a user