const uuid = require('uuid');
const express = require('express');
const router = express.Router();
const Promise = require('bluebird');
const logger = require('../../logger');

const { getPathwayJson } = require('./generate-pathway-json');

const db = require('../../db');

let findPathwayByUri = uri => {
  return db.accessTable('pathways').then( res => {
    let { table, conn } = res;

    return (
      table.filter({ uri: uri })
        .limit( 1 )
        .run( conn )
        .then( cursor => cursor.toArray() )
        .then( results => {
          if( results.length > 0 ){
            return results[0].network;
          } else {
            return null;
          }
        } )
    );
  });
};

let insertPathway = ( pathwayJson, uri ) => {
  return db.accessTable('pathways').then( res => {
    let { table, conn } = res;

    return (
      table.insert({
        id: uuid(),
        uri: uri,
        network: pathwayJson
      })
      .run( conn )
      .then( () => pathwayJson )
    );
  });
};

let getAndStorePathway = ( uri ) => {
  return getPathwayJson( uri ).then( pathwayJson => insertPathway( pathwayJson, uri ) );
};

router.get('/', ( req, res ) => {
  let uri = req.query.uri;

  let find = () => findPathwayByUri( uri );
  let storeIfNoResult = result => result != null ? result : getAndStorePathway( uri );
  let sendResponse = result => res.json(result);

  ( Promise.try(find)
    .then(storeIfNoResult)
    .then(sendResponse)
    .catch( err => {
      logger.error( err );
      res.status( 500 ).end( 'Server error' );
    } )
  );
});

module.exports = router;
@@ -1,34 +1,4 @@
-const uuid = require('uuid');%0A
 cons
@@ -208,914 +208,8 @@
 );%0A%0A
-const db = require('../../db');%0A%0Alet findPathwayByUri = uri =%3E %7B%0A  return db.accessTable('pathways').then( res =%3E %7B%0A    let %7B table, conn %7D = res;%0A%0A    return (%0A      table.filter(%7B uri: uri %7D)%0A        .limit( 1 )%0A        .run( conn )%0A        .then( cursor =%3E cursor.toArray() )%0A        .then( results =%3E %7B%0A          if( results.length %3E 0 )%7B%0A            return results%5B0%5D.network;%0A          %7D else %7B%0A            return null;%0A          %7D%0A        %7D )%0A    );%0A  %7D);%0A%7D;%0A%0Alet insertPathway = ( pathwayJson, uri ) =%3E %7B%0A  return db.accessTable('pathways').then( res =%3E %7B%0A    let %7B table, conn %7D = res;%0A%0A    return (%0A      table.insert(%7B%0A        id: uuid(),%0A        uri: uri,%0A        network: pathwayJson%0A      %7D)%0A      .run( conn )%0A      .then( () =%3E pathwayJson )%0A    );%0A  %7D);%0A%7D;%0A%0Alet getAndStorePathway = ( uri ) =%3E %7B%0A  return getPathwayJson( uri ).then( pathwayJson =%3E insertPathway( pathwayJson, uri ) );%0A%7D;%0A%0A
 rout
@@ -289,111 +289,22 @@
  =%3E 
-find
+get
 Pathway
-ByUri( uri );%0A  let storeIfNoResult = result =%3E result != null ? result : getAndStorePathway
+Json
 ( ur
@@ -384,35 +384,8 @@
 nd)%0A
-    .then(storeIfNoResult)%0A
     

Remove DB code for pathways