API-37: Adding endpoints for climbing route rating app (!16)
Jenkins Production Deployment

Co-authored-by: Patrick Mueller <patrick@mueller-patrick.tech>
Reviewed-on: #16
Co-authored-by: Patrick Müller <patrick@plutodev.de>
Co-committed-by: Patrick Müller <patrick@plutodev.de>
This commit was merged in pull request #16.
This commit is contained in:
2022-01-08 22:21:19 +00:00
parent b4d5bdd0b6
commit 25a66b060c
18 changed files with 4267 additions and 26 deletions
@@ -0,0 +1,6 @@
export interface RouteRating {
rating_id: number;
route_id: string;
stars: number;
timestamp: Date;
}
@@ -0,0 +1,60 @@
import express, {Request, Response} from 'express';
import * as RatingService from './routeRatings.service';
import {Guid} from 'guid-typescript';
import logger from '../../../middleware/logger';
export const routeRatingsRouter = express.Router();
routeRatingsRouter.get('/by/route/:id', async (req: Request, res: Response) => {
try {
let route_id = req.params.id;
let rating = await RatingService.getStarsForRoute(route_id);
res.status(200).send({'rating': rating});
} catch (e) {
let errorGuid = Guid.create().toString();
logger.error('Error handling a request: ' + e.message, {reference: errorGuid});
res.status(500).send({
'status': 'PROCESSING_ERROR',
'message': 'Internal Server Error. Try again later.',
'reference': errorGuid
});
}
});
routeRatingsRouter.post('/', async (req: Request, res: Response) => {
try {
let route_id = req.query.route_id as string;
let stars = Number(req.query.stars);
let captcha_token = req.query.captcha_token as string;
if (!route_id || isNaN(stars) || !captcha_token) {
console.log(route_id, stars, captcha_token);
res.status(400).send({'message': 'Missing parameters'});
return;
}
// Verify captcha
/*if (!await verifyCaptcha(captcha_token)) {
res.status(403).send({'message': 'Invalid Captcha. Please try again.'});
return;
}*/
let result = await RatingService.createRating(route_id, stars);
if (result) {
res.status(201).send({'rating_id': result});
} else {
res.status(500).send({});
}
} catch (e) {
let errorGuid = Guid.create().toString();
logger.error('Error handling a request: ' + e.message, {reference: errorGuid});
res.status(500).send({
'status': 'PROCESSING_ERROR',
'message': 'Internal Server Error. Try again later.',
'reference': errorGuid
});
}
});
@@ -0,0 +1,52 @@
import {ClimbingRouteRatingDB} from '../ClimbingRouteRating.db';
import {RouteRating} from './RouteRating.interface';
/**
* Fetches and returns all ratings for the given route
* @param route_id The id of the route to get the ratings for
* @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;
}
};
/**
* Get the median amount of stars the given route got from climbers
* @param route_id The id of the route to get the rating for
* @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 starsSum = 0;
let starsAmount = 0;
for (let rating of ratings) {
starsSum += rating.stars;
starsAmount++;
}
return Number((starsSum / starsAmount).toFixed(1));
};
/**
* Creates a new rating and returns the id
* @param route_id The id of the route to be rated
* @param stars The amount of stars to be given
* @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]);
return res[0].comment_id;
} catch (err) {
throw err;
}
};