plutoapi-v2/src/models/climbing-route-rating/climbing_gyms/climbingGyms.router.ts

153 lines
5.4 KiB
TypeScript

/**
* 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: 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/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
* example: DAV Kletterhalle
* - in: query
* name: city
* required: true
* description: The city where the gym is in
* schema:
* type: string
* example: Karlsruhe
* - in: query
* name: hcaptcha_response
* required: true
* description: The hCaptcha response key
* schema:
* type: string
* example: P0_ey[...]bVu
*/
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['hcaptcha_response'] as string;
if (!name || !city || !captcha_token) {
res.status(400).send({'message': 'Missing parameters'});
return;
}
// Verify captcha
let success = await verifyCaptcha(captcha_token);
if (!success) {
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: 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
});
}
});