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

40 lines
1.1 KiB
TypeScript

import {ClimbingRouteRatingDB} from '../ClimbingRouteRating.db';
import {ClimbingGym} from './ClimbingGym.interface';
/**
* Fetches and returns all known climbing gyms
* @return Promise<ClimbingHall[]> The climbing halls
*/
export const findAll = async (): Promise<ClimbingGym[]> => {
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT gym_id, name, city, verified FROM climbing_gyms');
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
/**
* Creates a climbing gym and returns the id of the created gym
* @param name The name of the climbing hall
* @param city The city of the climbing hall
* @return number The id of the climbing hall
*/
export const createGym = async (name: string, city: string): Promise<number> => {
let conn = await ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO climbing_gyms (name, city) VALUES (?, ?) RETURNING gym_id', [name, city]);
return res[0].gym_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};