/** * Required External Modules and Interfaces */ import express, {Request, Response} from 'express'; import {Guid} from 'guid-typescript'; import logger from '../../../middleware/logger'; import {ClimbingGym} from './ClimbingGym.interface'; import * as GymService from './climbingGyms.service'; import {verifyCaptcha} from '../common/VerifyCaptcha'; /** * Router Definition */ export const climbingGymRouter = express.Router(); /** * @swagger * /crr/gyms: * get: * summary: Retrieve all known climbing gyms * description: Returns all climbing gyms in a JSON list * tags: * - climbing-route-rating * responses: * 200: * description: Success * content: * application/json: * schema: * type: array * items: * type: object * properties: * gym_id: * type: integer * description: The gym id * example: 1 * name: * type: string * description: The gym name * example: DAV Kletterhalle * city: * type: string * description: The city where the gym is in * example: Karlsruhe * verified: * type: boolean * description: If the gym is verified * example: 1 * 500: * description: A server error occurred. Please try again. If this issue persists, contact the admin. */ climbingGymRouter.get('/', async (req: Request, res: Response) => { try { const gyms: ClimbingGym[] = await GymService.findAll(); res.status(200).send(gyms); } 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 }); } }); /** * @swagger * /crr/gyms: * post: * summary: Create a new climbing gym * description: Creates a new climbing gym and returns the id of the created gym * tags: * - climbing-route-rating * responses: * 201: * description: Created * content: * application/json: * schema: * type: object * properties: * gym_id: * type: integer * description: The gym 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: name * required: true * description: The name of the gym * schema: * type: string * - in: query * name: city * required: true * description: The city where the gym is in * schema: * type: string * - in: query * name: h-captcha-response * required: true * description: The hCaptcha response key * schema: * type: string */ climbingGymRouter.post('/', async (req: Request, res: Response) => { try { let name = req.query.name as string; let city = req.query.city as string; let captcha_token = req.query['h-captcha-response'] as string; if (!name || !city || !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 GymService.createGym(name, city); if (result) { res.status(201).send({'gym_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 }); } });