plutoapi-v2/src/models/climbing-route-rating/route_comments/routeComments.router.ts

156 lines
5.8 KiB
TypeScript

import express, {Request, Response} from 'express';
import * as CommentService from './routeComments.service';
import {Guid} from 'guid-typescript';
import logger from '../../../middleware/logger';
import {RouteComment} from './RouteComment.interface';
import {verifyCaptcha} from '../common/VerifyCaptcha';
export const routeCommentsRouter = express.Router();
/**
* @swagger
* /crr/comments/by/route/{id}:
* get:
* summary: Retrieve the comments for the given route
* description: Returns all comments for the route with the specified id
* tags:
* - climbing-route-rating
* responses:
* 200:
* description: Success
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* comment_id:
* type: integer
* description: The comment id
* example: 2
* route_id:
* type: string
* description: The id of the route that the comment belongs to
* example: duck-score-guide
* comment:
* type: string
* description: The comment text
* example: Nice route! Was a lot of fun!
* timestamp:
* type: datetime
* description: The time when the comment was sent
* example: 2022-01-08T21:43:31.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
*/
routeCommentsRouter.get('/by/route/:id', async (req: Request, res: Response) => {
try {
let route_id = req.params.id;
const comments: RouteComment[] = await CommentService.findByRoute(route_id);
res.status(200).send(comments);
} 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/comments:
* post:
* summary: Create a new comment
* description: Creates a new comment and returns the id of the created comment
* tags:
* - climbing-route-rating
* responses:
* 201:
* description: Created
* content:
* application/json:
* schema:
* type: object
* properties:
* comment_id:
* type: integer
* description: The comment 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 comment for
* schema:
* type: string
* example: duck-score-guide
* - in: query
* name: comment
* required: true
* description: The comment text
* schema:
* type: string
* example: Nice route! Was a lot of fun!
* - in: query
* name: hcaptcha_response
* required: true
* description: The hCaptcha response key
* schema:
* type: string
* example: P0_ey[...]bVu
*/
routeCommentsRouter.post('/', async (req: Request, res: Response) => {
try {
let route_id = req.query.route_id as string;
let comment = req.query.comment as string;
let captcha_token = req.query['hcaptcha_response'] as string;
if (!route_id || !comment || !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 CommentService.createComment(route_id, comment);
if (result) {
res.status(201).send({'comment_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
});
}
});