Better connection handling
Jenkins Production Deployment

This commit is contained in:
Paddy committed 2022-06-26 14:48:00 +02:00
1 parent fc65474930
commit 0325534d53
64 files changed
+1697 -1505

No files matched your search

@@ -5,15 +5,15 @@ const mariadb = require('mariadb');
dotenv.config();
export namespace ClimbingRouteRatingDB {
const pool = mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.CRR_DATABASE,
connectionLimit: 5
});
const pool = mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.CRR_DATABASE,
connectionLimit: 5
});
export function getConnection() {
return pool;
}
export const getConnection = async () => {
return pool.getConnection();
};
}
@@ -1,6 +1,6 @@
export interface ClimbingGym {
gym_id: number;
name: string;
city: string;
verified: boolean;
gym_id: number;
name: string;
city: string;
verified: boolean;
}
@@ -6,12 +6,15 @@ import {ClimbingGym} from './ClimbingGym.interface';
* @return Promise<ClimbingHall[]> The climbing halls
*/
export const findAll = async (): Promise<ClimbingGym[]> => {
let conn = ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT gym_id, name, city, verified FROM climbing_gyms');
} catch (err) {
throw err;
}
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT gym_id, name, city, verified FROM climbing_gyms');
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
/**
@@ -21,13 +24,16 @@ export const findAll = async (): Promise<ClimbingGym[]> => {
* @return number The id of the climbing hall
*/
export const createGym = async (name: string, city: string): Promise<number> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO climbing_gyms (name, city) VALUES (?, ?) RETURNING gym_id', [name, city]);
try {
let res = await conn.query('INSERT INTO climbing_gyms (name, city) VALUES (?, ?) RETURNING gym_id', [name, city]);
return res[0].gym_id;
} catch (err) {
throw err;
}
return res[0].gym_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@@ -1,7 +1,7 @@
export interface ClimbingRoute {
route_id: string;
gym_id: number;
name: string;
difficulty: string;
route_setting_date: Date;
route_id: string;
gym_id: number;
name: string;
difficulty: string;
route_setting_date: Date;
}
@@ -7,12 +7,15 @@ import random from 'random-words';
* @return Promise<ClimbingRoute[]> The climbing routes
*/
export const findAll = async (): Promise<ClimbingRoute[]> => {
let conn = ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT route_id, gym_id, name, difficulty, route_setting_date FROM climbing_routes');
} catch (err) {
throw err;
}
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT route_id, gym_id, name, difficulty, route_setting_date FROM climbing_routes');
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
/**
@@ -21,12 +24,15 @@ export const findAll = async (): Promise<ClimbingRoute[]> => {
* @return Promise<ClimbingRoute> The climbing route
*/
export const findById = async (route_id: string): Promise<ClimbingRoute> => {
let conn = ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT route_id, gym_id, name, difficulty, route_setting_date FROM climbing_routes WHERE route_id = ?', route_id);
} catch (err) {
throw err;
}
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT route_id, gym_id, name, difficulty, route_setting_date FROM climbing_routes WHERE route_id = ?', route_id);
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
/**
@@ -37,23 +43,26 @@ export const findById = async (route_id: string): Promise<ClimbingRoute> => {
* @return string The id of the created route
*/
export const createRoute = async (gym_id: number, name: string, difficulty: string): Promise<string> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
// Generate route id
let route_id = '';
let randWords = random(3);
for (let i = 0; i <= 2; i++) {
route_id += randWords[i];
if (i < 2) {
route_id += '-';
}
}
// Generate route id
let route_id = '';
let randWords = random(3);
for (let i = 0; i <= 2; i++) {
route_id += randWords[i];
if (i < 2) {
route_id += '-';
}
}
try {
await conn.query('INSERT INTO climbing_routes (route_id, gym_id, name, difficulty) VALUES (?, ?, ?, ?)', [route_id, gym_id, name, difficulty]);
try {
await conn.query('INSERT INTO climbing_routes (route_id, gym_id, name, difficulty) VALUES (?, ?, ?, ?)', [route_id, gym_id, name, difficulty]);
return route_id;
} catch (err) {
throw err;
}
return route_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@@ -5,11 +5,11 @@ import axios from 'axios';
dotenv.config();
export const verifyCaptcha = async (captcha_token: string): Promise<boolean> => {
let postData = querystring.stringify({
response: captcha_token,
secret: process.env.HCAPTCHA_SECRET
});
let postData = querystring.stringify({
response: captcha_token,
secret: process.env.HCAPTCHA_SECRET
});
let res = await axios.post('https://hcaptcha.com/siteverify', postData);
return res.data.success;
let res = await axios.post('https://hcaptcha.com/siteverify', postData);
return res.data.success;
};
@@ -1,6 +1,6 @@
export interface RouteComment {
comment_id: number;
route_id: string;
comment: string;
timestamp: Date;
comment_id: number;
route_id: string;
comment: string;
timestamp: Date;
}
@@ -6,12 +6,15 @@ import {RouteComment} from './RouteComment.interface';
* @return Promise<RouteComment[]> The comments
*/
export const findByRoute = async (route_id: string): Promise<RouteComment[]> => {
let conn = ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT comment_id, route_id, comment, timestamp FROM route_comments WHERE route_id = ?', route_id);
} catch (err) {
throw err;
}
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT comment_id, route_id, comment, timestamp FROM route_comments WHERE route_id = ?', route_id);
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
/**
@@ -21,12 +24,15 @@ export const findByRoute = async (route_id: string): Promise<RouteComment[]> =>
* @return number The id of the comment
*/
export const createComment = async (route_id: string, comment: string): Promise<number> => {
let conn = ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO route_comments (route_id, comment) VALUES (?, ?) RETURNING comment_id', [route_id, comment]);
let conn = await ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO route_comments (route_id, comment) VALUES (?, ?) RETURNING comment_id', [route_id, comment]);
return res[0].comment_id;
} catch (err) {
throw err;
}
return res[0].comment_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@@ -1,6 +1,6 @@
export interface RouteRating {
rating_id: number;
route_id: string;
stars: number;
timestamp: Date;
rating_id: number;
route_id: string;
stars: number;
timestamp: Date;
}
@@ -7,12 +7,15 @@ import {RouteRating} from './RouteRating.interface';
* @return Promise<RouteRating[]> The ratings
*/
export const findByRoute = async (route_id: string): Promise<RouteRating[]> => {
let conn = ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT rating_id, route_id, stars, timestamp FROM route_ratings WHERE route_id = ?', route_id);
} catch (err) {
throw err;
}
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT rating_id, route_id, stars, timestamp FROM route_ratings WHERE route_id = ?', route_id);
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
/**
@@ -21,17 +24,17 @@ export const findByRoute = async (route_id: string): Promise<RouteRating[]> => {
* @return number The median amount of stars with 1 fraction digit.
*/
export const getStarsForRoute = async (route_id: string): Promise<number> => {
let ratings = await findByRoute(route_id);
let ratings = await findByRoute(route_id);
let starsSum = 0;
let starsAmount = 0;
let starsSum = 0;
let starsAmount = 0;
for (let rating of ratings) {
starsSum += rating.stars;
starsAmount++;
}
for (let rating of ratings) {
starsSum += rating.stars;
starsAmount++;
}
return Number((starsSum / starsAmount).toFixed(1));
return Number((starsSum / starsAmount).toFixed(1));
};
/**
@@ -41,12 +44,15 @@ export const getStarsForRoute = async (route_id: string): Promise<number> => {
* @return number The id of the created rating
*/
export const createRating = async (route_id: string, stars: number): Promise<number> => {
let conn = ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO route_ratings (route_id, stars) VALUES (?, ?) RETURNING rating_id', [route_id, stars]);
let conn = await ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO route_ratings (route_id, stars) VALUES (?, ?) RETURNING rating_id', [route_id, stars]);
return res[0].comment_id;
} catch (err) {
throw err;
}
return res[0].comment_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};