import {ClimbingRouteRatingDB} from '../ClimbingRouteRating.db'; import {RouteComment} from './RouteComment.interface'; /** * Fetches and returns all comments that belong to the given route * @return Promise The comments */ export const findByRoute = async (route_id: string): Promise => { let conn = 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; } }; /** * 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 => { let conn = 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; } };