chore(release): 0.1.1
[risinglegends.git] / src / server / map.ts
1 import { City, Location, Path } from "../shared/map";
2 import { db } from './lib/db';
3
4 export async function getAllServices(city_id: number): Promise<Location[]> {
5   return db.select('*')
6             .from<Location>('locations')
7             .where({city_id})
8             .orderBy('type')
9             .orderBy('display_order');
10 }
11
12 export async function getService(serverId: number): Promise<Location> {
13   return db.select('*').first().from<Location>('locations').where({
14     id: serverId
15   });
16 }
17
18 export async function getAllPaths(city_id: number): Promise<Path[]> {
19   const res = await db.raw(`
20                 select 
21                   paths.*, c1.name as starting_city_name, c2.name as ending_city_name
22                 from paths
23                 join cities c1 on c1.id = paths.starting_city 
24                 join cities c2 on c2.id = paths.ending_city
25                 where paths.starting_city = ?
26                 `, [city_id]);
27
28   return res.rows.map(row => {
29     return {
30       starting_city: row.starting_city,
31       ending_city: row.ending_city,
32       starting_city_name: row.starting_city_name,
33       ending_city_name: row.ending_city_name
34     }
35   });
36 }
37
38 export async function getCityDetails(city_id: number): Promise<City> {
39   return db.first().select('*').from<City>('cities').where({id: city_id});
40 }