Skip to content

Commit

Permalink
successfully post seqeunce
Browse files Browse the repository at this point in the history
ref #10-spliiter-adapter;
  • Loading branch information
maxgrossman committed Jun 18, 2018
1 parent 8a4b7cb commit b531845
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
8 changes: 4 additions & 4 deletions db/posm-paths.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ 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)
);

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)
);
Expand Down
61 changes: 35 additions & 26 deletions handlers/sequence/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
16 changes: 9 additions & 7 deletions handlers/sequence/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions test/sequence/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b531845

Please sign in to comment.