refactor
This commit is contained in:
parent
637a867c7e
commit
53f5a2f820
5 changed files with 99 additions and 39 deletions
|
|
@ -6,10 +6,11 @@ import {leaderboardRoute} from "./leaderboardRoute.js";
|
||||||
import {userRoute} from "./userRoute.js";
|
import {userRoute} from "./userRoute.js";
|
||||||
import {gameRoute} from "./gameRoute.js";
|
import {gameRoute} from "./gameRoute.js";
|
||||||
|
|
||||||
// TODO: Rename variables --> Responsotory + Comments
|
// initialize express
|
||||||
const app = express()
|
const app = express()
|
||||||
const port = 3000
|
const port = 3000
|
||||||
|
|
||||||
|
// use needed middlewares
|
||||||
app.use(helmet())
|
app.use(helmet())
|
||||||
app.use(cors())
|
app.use(cors())
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,34 +2,32 @@ import express from "express";
|
||||||
import {GameRepository} from "./repositories/GameRepository.js";
|
import {GameRepository} from "./repositories/GameRepository.js";
|
||||||
import {GamePgPromiseRepository} from "./repositories/pgPromise/GamePgPromiseRepository.js";
|
import {GamePgPromiseRepository} from "./repositories/pgPromise/GamePgPromiseRepository.js";
|
||||||
import {Game} from "./model/Game.js";
|
import {Game} from "./model/Game.js";
|
||||||
import {body, CustomValidator, validationResult} from "express-validator";
|
import {body, validationResult} from "express-validator";
|
||||||
import {UserRepository} from "./repositories/UserRepository.js";
|
import {TIME_VALIDATION_REGEX, userWithIdExists} from "./validators.js";
|
||||||
import {UserPgPromiseRepository} from "./repositories/pgPromise/UserPgPromiseRepository.js";
|
|
||||||
|
|
||||||
export const gameRoute = express.Router()
|
export const gameRoute = express.Router()
|
||||||
|
|
||||||
gameRoute.use(express.json())
|
gameRoute.use(express.json())
|
||||||
|
|
||||||
const userWithIdExists: CustomValidator = userId => {
|
|
||||||
try {
|
|
||||||
const userRepo: UserRepository = new UserPgPromiseRepository;
|
|
||||||
return userRepo.withIdExists(userId).then(exists => {
|
|
||||||
if (!exists) return Promise.reject("User does not exist");
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
gameRoute.post(
|
gameRoute.post(
|
||||||
'/add',
|
'/add',
|
||||||
body('playtime')
|
body('playtime')
|
||||||
.matches("([0-5]\\d:)?[0-5]\\d:[0-5]\\d"),
|
.matches(TIME_VALIDATION_REGEX),
|
||||||
body('date')
|
body('date')
|
||||||
.isDate(),
|
.isDate(),
|
||||||
body('userId')
|
body('userId')
|
||||||
.isInt({min: 1})
|
.isInt({min: 1})
|
||||||
.custom(userWithIdExists),
|
.custom(userWithIdExists),
|
||||||
|
/**
|
||||||
|
* After processing the errors of express-validator, inserts the game into the DB
|
||||||
|
* @param req
|
||||||
|
* body {
|
||||||
|
* playtime: string,
|
||||||
|
* date: Date,
|
||||||
|
* userId: int
|
||||||
|
* }
|
||||||
|
* @param res json: Game
|
||||||
|
*/
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
try {
|
try {
|
||||||
//region validate parameters
|
//region validate parameters
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,16 @@ import {HighscoreLeaderboard, TimeLeaderboard} from "./model/Leaderboard.js";
|
||||||
|
|
||||||
export const leaderboardRoute = express.Router()
|
export const leaderboardRoute = express.Router()
|
||||||
|
|
||||||
leaderboardRoute.get('/highscore', async (req, res) => {
|
leaderboardRoute.get('/highscore',
|
||||||
|
/**
|
||||||
|
* Returns the highscore leaderboard as JSON response, fetched from DB
|
||||||
|
* @param req
|
||||||
|
* @param res json: HighscoreLeaderboard
|
||||||
|
*/
|
||||||
|
async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const highscoreLeaderboardManager: HighscoreLeaderboardRepository = new HighscoreLeaderboardPgPromiseRepository;
|
const highscoreLeaderboardRepo: HighscoreLeaderboardRepository = new HighscoreLeaderboardPgPromiseRepository;
|
||||||
const highscoreLeaderboard: HighscoreLeaderboard = await highscoreLeaderboardManager.getAll();
|
const highscoreLeaderboard: HighscoreLeaderboard = await highscoreLeaderboardRepo.getAll();
|
||||||
res.send(highscoreLeaderboard);
|
res.send(highscoreLeaderboard);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// handle errors
|
// handle errors
|
||||||
|
|
@ -19,10 +25,16 @@ leaderboardRoute.get('/highscore', async (req, res) => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
leaderboardRoute.get('/totalplaytime', async (req, res) => {
|
leaderboardRoute.get('/totalplaytime',
|
||||||
|
/**
|
||||||
|
* Returns the total playtime leaderboard as JSON response, fetched from DB
|
||||||
|
* @param req
|
||||||
|
* @param res json: TimeLeaderboard
|
||||||
|
*/
|
||||||
|
async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const timeLeaderboardManager: TimeLeaderboardRepository = new TimeLeaderboardPgPromiseRepository;
|
const timeLeaderboardRepo: TimeLeaderboardRepository = new TimeLeaderboardPgPromiseRepository;
|
||||||
const timeLeaderboard: TimeLeaderboard = await timeLeaderboardManager.getAll();
|
const timeLeaderboard: TimeLeaderboard = await timeLeaderboardRepo.getAll();
|
||||||
res.send(timeLeaderboard);
|
res.send(timeLeaderboard);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// handle errors
|
// handle errors
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,28 @@ import {UserPgPromiseRepository} from "./repositories/pgPromise/UserPgPromiseRep
|
||||||
import {UserRepository} from "./repositories/UserRepository.js";
|
import {UserRepository} from "./repositories/UserRepository.js";
|
||||||
import {UserScoresRepository} from "./repositories/UserScoresRepository.js";
|
import {UserScoresRepository} from "./repositories/UserScoresRepository.js";
|
||||||
import {User} from "./model/User.js";
|
import {User} from "./model/User.js";
|
||||||
|
import {USERNAME_VALIDATION_REGEX, userWithIdExists, userWithNameDoesNotExists} from "./validators.js";
|
||||||
|
|
||||||
export const userRoute = express.Router()
|
export const userRoute = express.Router()
|
||||||
userRoute.use(express.json())
|
userRoute.use(express.json())
|
||||||
|
|
||||||
|
|
||||||
userRoute.post(
|
userRoute.post(
|
||||||
'/register',
|
'/register',
|
||||||
body('name')
|
body('name')
|
||||||
.isString()
|
.isString()
|
||||||
.isLength({min: 3, max: 32})
|
.isLength({min: 3, max: 32})
|
||||||
.matches('[a-zA-Z0-9_.\\- ]*'),
|
.matches(USERNAME_VALIDATION_REGEX)
|
||||||
|
.custom(userWithNameDoesNotExists),
|
||||||
|
/**
|
||||||
|
* After processing the errors of express-validator, inserts the user into DB
|
||||||
|
* Returns the inserted user
|
||||||
|
* @param req
|
||||||
|
* body {
|
||||||
|
* name: string
|
||||||
|
* }
|
||||||
|
* @param res json: User
|
||||||
|
*/
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
try {
|
try {
|
||||||
//region validate parameters
|
//region validate parameters
|
||||||
|
|
@ -24,14 +36,10 @@ userRoute.post(
|
||||||
}
|
}
|
||||||
//endregion
|
//endregion
|
||||||
const username: string = req.body.name;
|
const username: string = req.body.name;
|
||||||
const userManager: UserRepository = new UserPgPromiseRepository();
|
const userRepo: UserRepository = new UserPgPromiseRepository();
|
||||||
|
|
||||||
// check if username already exists
|
|
||||||
if (await userManager.withNameExists(username)) {
|
|
||||||
return res.status(400).json({ errors: [{msg: `User with name '${username}' already exists.`}] })
|
|
||||||
}
|
|
||||||
// insert & return user
|
// insert & return user
|
||||||
const inserted: User = await userManager.insert({name: username});
|
const inserted: User = await userRepo.insert({name: username});
|
||||||
res.json(inserted);
|
res.json(inserted);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// handle errors
|
// handle errors
|
||||||
|
|
@ -42,8 +50,18 @@ userRoute.post(
|
||||||
)
|
)
|
||||||
|
|
||||||
userRoute.get('/:userId/scores',
|
userRoute.get('/:userId/scores',
|
||||||
// TODO: With id exists --> cusotm validator
|
param('userId')
|
||||||
param('userId').isInt({min: 1}),
|
.isInt({min: 1})
|
||||||
|
.custom(userWithIdExists),
|
||||||
|
/**
|
||||||
|
* After processing the errors of express-validator, fetches the scores from the DB
|
||||||
|
* Returns user scores
|
||||||
|
* @param req
|
||||||
|
* params {
|
||||||
|
* userId: number
|
||||||
|
* }
|
||||||
|
* @param res json: UserScores
|
||||||
|
*/
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
//region validate parameters
|
//region validate parameters
|
||||||
const errors = validationResult(req);
|
const errors = validationResult(req);
|
||||||
|
|
@ -53,16 +71,11 @@ userRoute.get('/:userId/scores',
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
const userId: number = req.params.userId;
|
const userId: number = req.params.userId;
|
||||||
const userManager: UserRepository = new UserPgPromiseRepository;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// check if user with given id exists
|
|
||||||
if (!await userManager.withIdExists(userId)) {
|
|
||||||
return res.status(400).json({ errors: [{msg: `User with id ${userId} does not exist.`}] })
|
|
||||||
}
|
|
||||||
// get & return data
|
// get & return data
|
||||||
const userScoresManager: UserScoresRepository = new UserScoresPgPromiseRepository;
|
const userScoresRepo: UserScoresRepository = new UserScoresPgPromiseRepository;
|
||||||
const userScores = await userScoresManager.getById(userId);
|
const userScores = await userScoresRepo.getById(userId);
|
||||||
res.json(userScores);
|
res.json(userScores);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// handle errors
|
// handle errors
|
||||||
|
|
|
||||||
36
backend/api/src/validators.ts
Normal file
36
backend/api/src/validators.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import {CustomValidator} from "express-validator";
|
||||||
|
import {UserRepository} from "./repositories/UserRepository.js";
|
||||||
|
import {UserPgPromiseRepository} from "./repositories/pgPromise/UserPgPromiseRepository.js";
|
||||||
|
|
||||||
|
export const USERNAME_VALIDATION_REGEX: string = '[a-zA-Z0-9_.\\- ]*';
|
||||||
|
export const TIME_VALIDATION_REGEX: string = '([0-5]\\d:)?[0-5]\\d:[0-5]\\d';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom express-validator to ensure that the user with given ID exists
|
||||||
|
* @param userId
|
||||||
|
*/
|
||||||
|
export const userWithIdExists: CustomValidator = userId => {
|
||||||
|
try {
|
||||||
|
const userRepo: UserRepository = new UserPgPromiseRepository;
|
||||||
|
return userRepo.withIdExists(userId).then(exists => {
|
||||||
|
if (!exists) return Promise.reject("User does not exist");
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom express-validator to ensure that the user with given name does not exist yet
|
||||||
|
* @param username
|
||||||
|
*/
|
||||||
|
export const userWithNameDoesNotExists: CustomValidator = username => {
|
||||||
|
try {
|
||||||
|
const userRepo: UserRepository = new UserPgPromiseRepository;
|
||||||
|
return userRepo.withNameExists(username).then(exists => {
|
||||||
|
if (exists) return Promise.reject("User with given name already exists");
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue