Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.idea/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Trello Link: https://trello.com/b/OX4kMOli/project-week-backend

# Back End Project Week

This week you will build a backend for a note taking app called "Lambda Notes".
Expand Down
Empty file added api/notes.js
Empty file.
4 changes: 4 additions & 0 deletions data/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const knex = require('knex');
const knexConfig = require('../knexfile.js');

module.exports = knex(knexConfig.development);
37 changes: 37 additions & 0 deletions data/helpers/mappers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = {
intToBoolean,
booleanToint,
projectToBody,
actionToBody,
};

function intToBoolean(int) {
return int === 1 ? true : false;
}

function booleanToint(bool) {
return bool === true ? 1 : 0;
}

function projectToBody(project) {
const result = {
...project,
completed: intToBoolean(project.completed),
};

if (project.actions) {
result.actions = project.actions.map(action => ({
...action,
completed: intToBoolean(action.completed),
}));
}

return result;
}

function actionToBody(action) {
return {
...action,
completed: intToBoolean(action.completed),
};
}
46 changes: 46 additions & 0 deletions data/helpers/noteModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const db = require('../dbConfig.js');
const mappers = require('./mappers');

module.exports = {
get: function(id) {
let query = db('notes as n');

if (id) {
query.where('n.id', id).first();

const promises = [query, this.getProjectActions(id)]; // [ projects, actions ]

return Promise.all(promises).then(function(results) {
let [project, actions] = results;
project.actions = actions;

return mappers.projectToBody(project);
});
}

return query.then(projects => {
return projects.map(project => mappers.projectToBody(project));
});
},
/*getProjectActions: function(projectId) {
return db('actions')
.where('project_id', projectId)
.then(actions => actions.map(action => mappers.actionToBody(action)));
},*/
insert: function(note) {
return db('notes')
.insert(note)
.then(([id]) => this.get(id));
},
update: function(id, changes) {
return db('notes')
.where('id', id)
.update(changes)
.then(count => (count > 0 ? this.get(id) : null));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to have the > 0

},
remove: function(id) {
return db('notes')
.where('id', id)
.del();
},
};
8 changes: 8 additions & 0 deletions data/migrations/20190212185648_notesInit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

exports.up = function(knex, Promise) {

};

exports.down = function(knex, Promise) {

};
15 changes: 15 additions & 0 deletions data/seeds/notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
exports.seed = function(knex, Promise) {
return knex('notes')
.del()
.then(function() {
return knex('notes').insert([
{
name: 'Complete Node.js and Express Challenge',
description:
'Build and Awesome API Using Node.js and Express to Manage Projects and Actions GTD Style!',
},
]);
});
};
*/
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express'),
bodyParser = require('body-parser'),
notes = require('./api/notes');

const app = express();

app
.use(bodyParser.json())
.use('/notes', notes)


app.listen(5000);
12 changes: 12 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
development: {
client: 'sqlite3',
connection: { filename: './data/lambda.sqlite3' },
useNullAsDefault: true,
migrations: {
directory: './data/migrations',
tableName: 'dbmigrations',
},
seeds: { directory: './data/seeds' },
},
};