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

39 lines
1.2 KiB
TypeScript

import {ClimbingRouteRatingDB} from '../ClimbingRouteRating.db';
import {RouteComment} from './RouteComment.interface';
/**
* Fetches and returns all comments that belong to the given route
* @return Promise<RouteComment[]> The comments
*/
export const findByRoute = async (route_id: string): Promise<RouteComment[]> => {
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT comment_id, route_id, comment, timestamp FROM route_comments WHERE route_id = ?', route_id);
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
/**
* Creates a new comment and returns the id of the created comment
* @param route_id The id of the route to create the comment for
* @param comment The comment
* @return number The id of the comment
*/
export const createComment = async (route_id: string, comment: string): Promise<number> => {
let conn = await ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO route_comments (route_id, comment) VALUES (?, ?) RETURNING comment_id', [route_id, comment]);
return res[0].comment_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};