/** * Required External Modules and Interfaces */ import express, {Request, Response} from 'express'; import {Guid} from 'guid-typescript'; import logger from '../../../middleware/logger'; import {ClimbingRoute} from './ClimbingRoute.interface'; import * as RouteService from './climbingRoutes.service'; import {verifyCaptcha} from '../common/VerifyCaptcha'; /** * Router Definition */ export const climbingRoutesRouter = express.Router(); /** * @swagger * /crr/routes: * get: * summary: Retrieve all known climbing routes * description: Returns all climbing routes in a JSON list * tags: * - climbing-route-rating * responses: * 200: * description: Success * content: * application/json: * schema: * type: array * items: * type: object * properties: * route_id: * type: string * description: The route id * example: duck-score-guide * gym_id: * type: integer * description: The id of the gym that the route belongs to * example: 1 * name: * type: string * description: The route name * example: Mary Poppins * difficulty: * type: string * description: The difficulty of the route * example: 'DE: 5, FR: 5c' * route_setting_date: * type: datetime * description: The route setting date * example: 2022-01-07T23:00:00.000Z * 500: * description: A server error occurred. Please try again. If this issue persists, contact the admin. */ climbingRoutesRouter.get('/', async (req: Request, res: Response) => { try { const routes: ClimbingRoute[] = await RouteService.findAll(); res.status(200).send(routes); } 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/routes/{id}: * get: * summary: Retrieve the route with the given id * description: Returns the climbing route with the given id if it exists * tags: * - climbing-route-rating * responses: * 200: * description: Success * content: * application/json: * schema: * type: array * items: * type: object * properties: * route_id: * type: string * description: The route id * example: duck-score-guide * gym_id: * type: integer * description: The id of the gym that the route belongs to * example: 1 * name: * type: string * description: The route name * example: Mary Poppins * difficulty: * type: string * description: The difficulty of the route * example: 'DE: 5, FR: 5c' * route_setting_date: * type: datetime * description: The route setting date * example: 2022-01-07T23:00:00.000Z * 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 */ climbingRoutesRouter.get('/:id', async (req: Request, res: Response) => { try { let route_id = req.params.id; const route: ClimbingRoute = await RouteService.findById(route_id); res.status(200).send(route); } 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/routes: * post: * summary: Create a new climbing route * description: Creates a new climbing route and returns the id of the created route * tags: * - climbing-route-rating * responses: * 201: * description: Created * content: * application/json: * schema: * type: object * properties: * route_id: * type: string * description: The route id * example: duck-score-guide * 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: gym_id * required: true * description: The gym id of the gym that the route belongs to * schema: * type: integer * example: 1 * - in: query * name: name * required: true * description: The name of the route * schema: * type: string * example: Mary Poppins * - in: query * name: difficulty * required: true * description: The difficulty of the route * schema: * type: string * example: 'DE: 5, FR: 5c' * - in: query * name: hcaptcha_response * required: true * description: The hCaptcha response key * schema: * type: string * example: P0_ey[...]bVu */ climbingRoutesRouter.post('/', async (req: Request, res: Response) => { try { let gym_id = Number(req.query.gym_id); let name = req.query.name as string; let difficulty = req.query.difficulty as string; let captcha_token = req.query['hcaptcha_response'] as string; if (isNaN(gym_id) || !name || !difficulty || !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 route_id = await RouteService.createRoute(gym_id, name, difficulty); res.status(201).send({'route_id': route_id}); } 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 }); } });