Skip to content

Commit

Permalink
post a sequence!!!
Browse files Browse the repository at this point in the history
ref #10-splitter-sequence
  • Loading branch information
maxgrossman committed Aug 7, 2018
1 parent cacf6f7 commit 94172b1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 44 deletions.
13 changes: 5 additions & 8 deletions db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,13 @@ class Database {
.catch((err) => { throw err; })
}
addSequence(sequence) {
const values = `(
'${uuidv4()}',
'${userId}',
)`;
const sql = `INSERT INTO Sequences VALUES ${values};`;
const userId = sequence.userId;
const sequenceId = sequence.sequenceId;
const images = sequence.sequence;
return this
.execute(sql)
.addImages(userId, sequenceId, images)
.then((result) => result)
.catch((err) => { throw err; })
.catch((err) => { throw err; });
}
};

Expand Down
9 changes: 6 additions & 3 deletions handlers/sequence/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

const Boom = require('boom');
const Sequence = require('../../adapters/sequence');
const Database = require('../../db');
const databaseLocation = require('../../config')[process.env.ENVIRONMENT || 'develop'].db;

Promise = require('bluebird');

module.exports = async (r, h) => {
try {
const paths = r.payload;
const userId = r.query.userId;
const type = r.query.type;
const minCutDist = r.params.minDist || 0.5;
const maxCutDist = r.params.maxDist || 300;
const maxDelta = r.params.maxDelta || 120;
const sequenceSize = r.params.size || 0;
const sequences = await Sequence.build(paths, minCutDist, maxCutDist, maxDelta, sequenceSize, userId);

const sequences = await Sequence.build(paths, type, minCutDist, maxCutDist, maxDelta, sequenceSize, userId);
Database.connect(databaseLocation);
await Promise.each(sequences, (sequence) => Database.addSequence(sequence).catch(err => { throw err; }))

Database.close();
return h.response({ upload: 'successful' }).code(200);

} catch (error) {
Expand Down
7 changes: 6 additions & 1 deletion routes/sequence/post.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
'use strict';

const pathsSchema = require('../../schema/paths');
const Joi = require('joi');

module.exports = {
method: 'POST',
path: '/sequence',
config: {
handler: require('../../handlers/sequence').post,
validate: {
payload:pathsSchema,
query: {
userId: Joi.string().guid({ versionO: [ 'uuidv4' ]}),
type: Joi.string()
},
payload: pathsSchema,
failAction: async (r, h, err) => err
}
}
Expand Down
72 changes: 40 additions & 32 deletions test/sequence/handler.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
'use strict';

// const Joi = require('joi');
// const chai = require('chai');
// const expect = chai.expect;
const Joi = require('joi');
const chai = require('chai');
const expect = chai.expect;

// const uuidv4 = require('uuid/v4');
const uuidv4 = require('uuid/v4');

// const server = require('../server');
// const mergeDefaults = require('../helpers').mergeDefaults;
// const users = require('../../db/seeds/users');
// const danbjoseph = users[0];
// const routes = [
// // require('../../routes/sequence').get,
// require('../../routes/sequence').post
// ];
const server = require('../server');
const mergeDefaults = require('../helpers').mergeDefaults;
const users = require('../../db/seeds/users');
const danbjoseph = users[0];
const routes = [
// require('../../routes/sequence').get,
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({
// method: 'POST',
// payload: ['/testData/danbjoseph'].map(p => process.cwd() + p),
// url: `/sequence?userId=${danbjoseph.id}`
// }),
// r = await server.inject(request),
// statusCode = r.statusCode;
before(async () => await server.liftOff(routes))
describe('post', () => {
it('replies 200 when sequence post is successful', async () => {
try {
const request = mergeDefaults({
method: 'POST',
payload: ['/testData/danbjoseph'].map(p => process.cwd() + p),
url: `/sequence?userId=${danbjoseph.id}&type=directory`
});
const r = await server.inject(request);
const statusCode = r.statusCode;

// expect(statusCode).to.be.eql(200);
expect(statusCode).to.be.eql(200);

// } catch (error) {
// console.error(error);
} catch (error) {
console.error(error);

// }
// })
// it('replies 400 when unsuccessful because user already in db', async () => {
// })
// })
}
})
.timeout(10000000)
it('replies 400 when unsuccessful building sequence', async () => {
const request = mergeDefaults({
method: 'POST',
payload: ['/not/on/comp'].map(p => process.cwd() + p),
url: `/sequence?userId=${danbjoseph.name}&type=directory`
});
const r = await server.inject(request);
const statusCode = r.statusCode;
expect(statusCode).to.be.eql(400);
})
})
// describe('get', () => {
// it ('replies 200 and username when given valid uuid', async () => {
// })
Expand Down

0 comments on commit 94172b1

Please sign in to comment.