From b531845a4b4bc756269ad790c73e278e42f8c7b8 Mon Sep 17 00:00:00 2001 From: Max Grossman Date: Mon, 18 Jun 2018 06:51:25 -0400 Subject: [PATCH] successfully post seqeunce ref #10-spliiter-adapter; --- db/posm-paths.sql | 8 ++--- handlers/sequence/helpers.js | 61 +++++++++++++++++++++--------------- handlers/sequence/post.js | 16 +++++----- package.json | 3 +- test/sequence/handler.js | 8 +++-- 5 files changed, 54 insertions(+), 42 deletions(-) diff --git a/db/posm-paths.sql b/db/posm-paths.sql index 26317c7..19c8bf7 100644 --- a/db/posm-paths.sql +++ b/db/posm-paths.sql @@ -10,7 +10,7 @@ CREATE TABLE Users ( CREATE TABLE Sequences( id UUID NOT NULL PRIMARY KEY, - userId INTEGER NOT NULL, + userId UUID NOT NULL, images JSON1 NOT NULL, FOREIGN KEY(userId) REFERENCES Users(id) ); @@ -18,9 +18,9 @@ CREATE TABLE Sequences( CREATE TABLE Images( id UUID NOT NULL PRIMARY KEY, path TEXT NOT NULL, - time INTEGER NOT NULL, - seqId INTEGER NOT NULL, - userId INTEGER NOT NULL, + time TEXT NOT NULL, + seqId UUID NOT NULL, + userId UUID NOT NULL, FOREIGN KEY(userId) REFERENCES Users(id), FOREIGN KEY(seqId) REFERENCES Sequences(id) ); diff --git a/handlers/sequence/helpers.js b/handlers/sequence/helpers.js index 4016827..d4f03e1 100644 --- a/handlers/sequence/helpers.js +++ b/handlers/sequence/helpers.js @@ -7,20 +7,18 @@ const db = require('../../connection'); * inserts images into the image table * @param {Array} sequence list of sequence images */ -exports.insertImages = async (sequenceMap) => { - const sequenceId = sequenceMap.sequenceId, - userId = sequenceMap.userId, - sequence = sequenceMap.sequence; - +exports.insertImages = async (sequenceMap) => { try { - let values = await Promise.map(sequence, image => `${image.id}, ${image.image}, ${image.timestamp}, ${sequenceId}, ${userId}, GeomFromText('POINT(${image.loc.lat} ${image.loc.lon})'))`) - values = `(${values.join(',\n')})` - + const sequenceId = sequenceMap.sequenceId, + userId = sequenceMap.userId; + + let values = await Promise.map(sequenceMap.sequence, image => `('${image.id}', '${image.image}', '${image.timestamp}', '${sequenceId}', '${userId}', GeomFromText('POINT(${image.loc.lat} ${image.loc.lon})'))`); + console.log(values.slice(0,5).join(',')) await db.raw(` SELECT load_extension('mod_spatialite'); INSERT INTO Images ( id, path, time, seqId, userId, loc - ) VALUES ${values} + ) VALUES ${values.join(',')} `) } catch (error) { @@ -35,22 +33,33 @@ exports.insertImages = async (sequenceMap) => { * inserts sequence into Sequences table * @param {Array} sequence list of sequence images */ -exports.insertSequence = (sequence) => { - const sequenceId = sequence.sequenceId, - userId = sequence.userId; - - Promise.map(sequence, image => { return { id : image.id, loc: image.loc }}) - .then(async (sequenceImages) => { - try { - await db.raw(` - INSERT INTO Sequences ( - id, userId, images - ) VALUES (${sequenceId}, ${userId}, JSON(${sequenceImages}) - `) - } catch (e) { - throw e; - } - }) - .catch((error) => { throw error }); +exports.insertSequence = async (sequenceMap) => { + try { + // adapt images into list of geojson point features + // thanks geojson homepage -> http://geojson.org/ + const imageFeatures = await Promise.map(sequenceMap.sequence, image => { + return { + type: 'Feature', + geometry: { + type: 'Point', + coordinates: [image.loc.lat, image.loc.lon] + }, + properties: { id: image.id } + } + }); + + await db.raw(` + INSERT INTO Sequences ( + id, userId, images + ) VALUES ( + '${sequenceMap.sequenceId}', + '${sequenceMap.userId}', + json('${JSON.stringify(imageFeatures)}') + ); + `) + } catch (error) { + console.error(error); + throw error; + } } \ No newline at end of file diff --git a/handlers/sequence/post.js b/handlers/sequence/post.js index 0d5bcda..01f7c91 100644 --- a/handlers/sequence/post.js +++ b/handlers/sequence/post.js @@ -18,13 +18,15 @@ module.exports = async (r, h) => { sequences.forEach(async (sequence) => { try { - await Promise.map([insertImages], async (inserter) => { - try { - await inserter(sequence); - } catch (e) { - throw e; - } - }) + // await insertSequence(sequence); + await insertImages(sequence); + // Promise.map([insertSequence], async (inserter) => { + // try { + // await inserter(sequence); + // } catch (e) { + // throw e; + // } + // }) } catch (e) { throw e; } diff --git a/package.json b/package.json index d2b9afa..953b0f1 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,7 @@ "scripts": { "migrate": "sqlite3 db/posm-paths.sqlite3 < db/posm-paths.sql", "test": "./node_modules/.bin/mocha test/**/* --exit", - "seed": "knex seed:run", - "fixture": "yarn run migrate && yarn run seed" + "seed": "knex seed:run" }, "dependencies": { "bluebird": "^3.5.1", diff --git a/test/sequence/handler.js b/test/sequence/handler.js index 5d794ed..c440474 100644 --- a/test/sequence/handler.js +++ b/test/sequence/handler.js @@ -14,19 +14,21 @@ const routes = [ require('../../routes/sequence').post ]; +const db = require('../../connection'); + before(async () => await server.liftOff(routes)) describe('post', () => { it('replies 200 when sequence post is successful', async () => { try { - const request = mergeDefaults({ + const userId = (await db('Users').select('id').where({ name: 'danbjoseph' }))[0].id, + request = mergeDefaults({ method: 'POST', payload: ['/testData/danbjoseph'].map(p => process.cwd() + p), - url: '/sequence?userId=6cc99b2f-a00a-45c3-a74b-d532547dd852' + url: `/sequence?userId=${userId}` }), r = await server.inject(request), statusCode = r.statusCode; - expect(statusCode).to.be.eql(200); } catch (error) {