plutoapi-v2/src/models/climbing-route-rating/route_ratings/routeRatings.router.ts

141 lines
4.8 KiB
TypeScript

import express, {Request, Response} from 'express';
import * as RatingService from './routeRatings.service';
import {Guid} from 'guid-typescript';
import logger from '../../../middleware/logger';
import {verifyCaptcha} from '../common/VerifyCaptcha';
export const routeRatingsRouter = express.Router();
/**
* @swagger
* /crr/ratings/by/route/{id}:
* get:
* summary: Retrieve the rating for the given route
* description: Returns the medium amount of stars that the route got
* tags:
* - climbing-route-rating
* responses:
* 200:
* description: Success
* content:
* application/json:
* schema:
* type: object
* properties:
* rating:
* type: float
* description: The median amount of stars
* example: 4.5
* 500:
* description: A server error occurred. Please try again. If this issue persists, contact the admin.
* parameters:
* - in: path
* name: id
* required: true
* description: The id of the route
* schema:
* type: string
* example: duck-score-guide
*/
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: any) {
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
});
}
});
/**
* @swagger
* /crr/ratings:
* post:
* summary: Create a new rating
* description: Creates a new rating and returns the id of the created rating
* tags:
* - climbing-route-rating
* responses:
* 201:
* description: Created
* content:
* application/json:
* schema:
* type: object
* properties:
* rating_id:
* type: integer
* description: The rating id
* example: 1
* 400:
* description: Wrong parameters, see response body for detailed information
* 403:
* description: Invalid captcha, please try again.
* 500:
* description: A server error occurred. Please try again. If this issue persists, contact the admin.
* parameters:
* - in: query
* name: route_id
* required: true
* description: The id of the route to create the rating for
* schema:
* type: string
* example: duck-score-guide
* - in: query
* name: stars
* required: true
* description: The amount of stars to give
* schema:
* type: integer
* example: 4
* - in: query
* name: hcaptcha_response
* required: true
* description: The hCaptcha response key
* schema:
* type: string
* example: P0_ey[...]bVu
*/
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['hcaptcha_response'] as string;
if (!route_id || isNaN(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: any) {
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
});
}
});