Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @author Mike Hamilton <mike@mikehamilton.ca>
  3.  * @copyright Mike Hamilton 2017
  4.  * @license MIT
  5.  *
  6.  * @requires koa@next
  7.  * @requires koa-router
  8.  * @requires pg
  9.  * @requires knex
  10.  * @requires knex-postgis
  11.  */
  12.  
  13. const Koa = require('koa')
  14. const app = new Koa()
  15.  
  16. const Router = require('koa-router')
  17. const router = new Router({
  18.   prefix: '/hydrant'
  19. })
  20.  
  21. // REQUIRE PostGRES module
  22. var pg = require('pg')
  23.  
  24. // REQUIRE knex + setup
  25. var knex = require('knex')({
  26.   dialect: 'postgres',
  27.   client: 'pg',
  28.   connection: {
  29.     host: 'wbsnowmaking.com',
  30.     user: 'xxx',
  31.     password: 'xxxx',
  32.     database: 'whistlerblackcomb'
  33.   },
  34.   pool: {
  35.     min: 0,
  36.     max: 7
  37.   }
  38. })
  39.  
  40. // REQUIRE knex-postgis (PostGRES extension)
  41. var st = require('knex-postgis')(knex);
  42.  
  43. /**
  44.  * @desc Function to return single element
  45.  * @param {number} id
  46.  * @param {string} table
  47.  * @param {string} schema
  48.  * @param {number} srid
  49.  */
  50. async function getSingle(id, table, schema, srid) {
  51.   return await knex
  52.     .withSchema(schema)
  53.     .from(table)
  54.     .where({
  55.       id: id
  56.     })
  57.     .select(
  58.       '*',
  59.       st.y(st.transform('geom', srid)).as('lat_gen'),
  60.       st.x(st.transform('geom', srid)).as('lon_gen')
  61.     )
  62. }
  63.  
  64. /**
  65.  * @desc Function to return all elements
  66.  * @param {string} table
  67.  * @param {string} schema
  68.  * @param {number} srid
  69.  */
  70. async function getAll(table, schema, srid) {
  71.   return await knex
  72.     .withSchema(schema)
  73.     .select(
  74.       '*',
  75.       st.y(st.transform('geom', srid)).as('lat_gen'),
  76.       st.x(st.transform('geom', srid)).as('lon_gen')
  77.     )
  78.     .from(table)
  79. }
  80.  
  81. /**
  82.  * @desc Function to delete single element
  83.  * @param {number} id
  84.  * @param {string} table
  85.  * @param {string} schema
  86.  */
  87. async function delSingle(id, table, schema) {
  88.   return await knex
  89.     .withSchema(schema)
  90.     .where({
  91.       id: id
  92.     })
  93.     .del()
  94.     .from(table)
  95. }
  96.  
  97. router
  98.     // (C)reate
  99.     .put('/', async ctx => {
  100.         ctx.body = JSON.stringify('Create')
  101.     })
  102.  
  103.     // (R)ead All
  104.     .get('/', async ctx => {
  105.         ctx.body = await getAll('hydrants', 'snowmaking', '4326')
  106.     })
  107.  
  108.     // (R)ead Single
  109.     .get('/:id', async (ctx, next) => {
  110.       try {
  111.         response = await getSingle(ctx.params.id, 'hydrants', 'snowmaking', '4326')
  112.         if (response == '') {
  113.           return ctx.status = 404
  114.         } else {
  115.           return ctx.body = response
  116.         }
  117.       } catch (err) {
  118.         ctx.status = err.status || 500
  119.         ctx.body = JSON.stringify('Oh dear, something seems to have gone wrong.')
  120.         ctx.app.emit('error', err, ctx)
  121.       }
  122.     })
  123.  
  124.     // (U)pdate
  125.     .patch('/:id', async ctx => {
  126.         ctx.body = JSON.stringify('Update')
  127.     })
  128.  
  129.     // (D)elete
  130.     .delete('/:id', async ctx => {
  131.         ctx.body = JSON.stringify('Delete')
  132.     })
  133.  
  134. app
  135.   .use(router.routes())
  136.   .use(router.allowedMethods())
  137.  
  138. app.listen(3000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement