const _ = require('lodash');

const db = require('../db.js');
const cache = require('../utils/cache.js');
const elron = require('../providers/elron.js');

// Get route by parameters.
async function getRoutes(req, res) {
	
	switch (req.query['provider']) {
		
		case 'mnt':
		case 'tlt': {
			
			// Get stop sequences for route.
			const routes = _.groupBy(await db.query(`
				SELECT stop_id, name, type, origin, destination FROM stop_times
				JOIN stops AS stop ON stop.id = stop_id
				JOIN trips AS trip ON trip.id = trip_id
				WHERE
					(trip_id, destination) IN (
						SELECT trip_id, destination FROM trips AS trip
						JOIN routes AS route ON route.id = trip.route_id
						JOIN stop_times ON trip_id = trip.id
						WHERE stop_id = ? AND name = ? AND type = ?
					)
				GROUP BY route_id, destination, sequence
			`, [ req.query['stop'], req.query['name'], req.query['type'] ]), (route) => `${route.origin} - ${route.destination}`);
			
			// Transform stop objects.
			for (const key in routes) routes[key] = routes[key].map((stop) => ({
				id: stop.stop_id,
				name: stop.name,
				type: stop.type
			}));
			
			return void res.send(routes);
			
		}
		
		case 'elron': {
			return void res.send(await elron.getRoute(id));
		}
		
	}
	
}

module.exports = (fastify, opts, next) => {
	
	fastify.get('/routes', {
		beforeHandler: cache.middleware,
		schema: {
			querystring: {
				type: 'object',
				required: [ 'stop', 'name', 'type', 'provider' ],
				properties: {
					stop: { type: 'string' },
					name: { type: 'string' },
					type: { type: 'string' },
					provider: { type: 'string', enum: [ 'mnt', 'tlt', 'elron' ] }
				}
			}
		}
	}, getRoutes);
	
	next();
	
};

@@ -220,35 +220,158 @@
 %0A%09%0A%09
-switch (req.query%5B'
+const %7B stop, name, type, provider %7D = req.query;%0A%09%0A%09res.send(await cache.use('routes', %60$%7Bstop%7D-$%7Bname%7D-$%7Btype%7D%60, async () =%3E %7B%0A%09%09%0A%09%09switch (
 provider
 '%5D) 
@@ -370,17 +370,17 @@
 ider
-'%5D
 ) %7B%0A%09%09
-%0A
+%09%0A%09
 %09%09ca
@@ -389,16 +389,17 @@
  'mnt':%0A
+%09
 %09%09case '
@@ -405,25 +405,27 @@
 'tlt': %7B%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09// Get st
@@ -448,16 +448,17 @@
  route.%0A
+%09
 %09%09%09const
@@ -494,16 +494,17 @@
 query(%60%0A
+%09
 %09%09%09%09SELE
@@ -563,24 +563,25 @@
 p_times%0A%09%09%09%09
+%09
 JOIN stops A
@@ -604,24 +604,25 @@
 d = stop_id%0A
+%09
 %09%09%09%09JOIN tri
@@ -661,19 +661,15 @@
 %09%09%09%09
+%09
 WHERE
-%0A%09%09%09%09%09
+ 
 (tri
@@ -706,32 +706,37 @@
 SELECT trip_id, 
+trip.
 destination FROM
@@ -905,16 +905,17 @@
 %0A%09%09%09%09%09)%0A
+%09
 %09%09%09%09GROU
@@ -958,68 +958,30 @@
 %0A%09%09%09
+%09
 %60, %5B 
-req.query%5B'stop'%5D, req.query%5B'name'%5D, req.query%5B'
+stop, name, 
 type
-'%5D
  %5D),
@@ -1038,17 +1038,19 @@
 %7D%60);%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09// Tr
@@ -1071,16 +1071,17 @@
 bjects.%0A
+%09
 %09%09%09for (
@@ -1144,16 +1144,17 @@
 ) =%3E (%7B%0A
+%09
 %09%09%09%09id: 
@@ -1167,24 +1167,25 @@
 top_id,%0A%09%09%09%09
+%09
 name: stop.n
@@ -1189,16 +1189,17 @@
 p.name,%0A
+%09
 %09%09%09%09type
@@ -1217,16 +1217,17 @@
 %0A%09%09%09
+%09
 %7D));%0A%09%09%09
 %0A%09%09%09
@@ -1226,53 +1226,44 @@
 %0A%09%09%09
+%09
 %0A%09%09%09
+%09
 return 
-void res.send(
 routes
-)
 ;%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%7D%0A%09%09
-%0A
+%09%0A%09
 %09%09ca
@@ -1283,29 +1283,16 @@
 %0A%09%09%09
+%09
 return 
-void res.send(
 awai
@@ -1315,20 +1315,31 @@
 (id)
-)
 ;%0A
+%09
 %09%09%7D%0A%09%09
-%0A%09%7D
+%09%0A%09%09%7D%0A%09%09%0A%09%7D));
 %0A%09%0A%7D

Implement cache in routes route (api)