Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
node_modules/
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node index.js
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ You are required to showcase progress with at least 4 commits a day. This will l
- Share your board with the project manager that has been assigned to you. If you have not been assigned yet, reach out to your Section Lead for guidance.
- Add your Trello URL to your project's README.md file. Commit the change, push it to your repository & submit a pull request.

https://trello.com/b/R1umQ7aa/lambda-notes-backend-mitchell-robles
Copy link

Choose a reason for hiding this comment

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

This board is private!


## Backend MVP Features:

We recommend that you finish all the MVP features before trying to deploy.
Expand Down
12 changes: 12 additions & 0 deletions data/migrations/20190212182501_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.up = function(knex, Promise) {
return knex.schema.createTable('notes', table => {
table.increments();
table.string('title').notNullable();
table.string('body').notNullable();
})
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('notes');
};
Binary file added data/notes.sqlite3
Binary file not shown.
13 changes: 13 additions & 0 deletions data/seeds/Notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('notes').del()
.then(function () {
// Inserts seed entries
return knex('notes').insert([
{id: 1, title: 'Note1', body: 'Body1'},
{id: 2, title: 'Note2', body: 'Body2'},
{id: 3, title: 'Note3', body: 'Body3'}
]);
});
};
90 changes: 90 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const helmet = require('helmet');
const knex = require('knex');
const dbConfig = require('./knexfile.js');
const db = knex(dbConfig.development);

const server = express();

server.use(express.json());
server.use(cors());
server.use(morgan());
server.use(helmet());


const PORT = process.env.PORT || 2200;

server.post('/api/notes', (req, res) => {
const newNote = req.body;

if (newNote.title && newNote.body) {
db('notes').insert(newNote)
.then(noteId => {
res.status(200).json(noteId);
})
.catch(err => {
res.status(500).json({ message: 'Failure to create note' });
})
} else {
res.status(400).json({ message: 'Missing title or body' });
};
});

server.get('/api/notes', (req, res) => {
db('notes')
.then(notes => {
res.status(200).json(notes);
})
.catch(err => {
res.status(500).json({ message: 'Failure to load notes' });
});
});

server.get('/api/notes/:id', (req, res) => {
const { id } = req.params;

db('notes').where({ id })
.then(note => {
res.status(200).json(note);
})
.catch(err => {
res.status(500).json({ message: 'Failed to get note' })
})
});

server.put('/api/notes/:id', (req, res) => {
const { id } = req.params;
const noteEdit = req.body;

if (noteEdit.title && noteEdit.body) {
db('notes').where({ id }).update(noteEdit)
.then(note => {
res.status(200).json('Note has been successfully edited!');
})
.catch(err => {
res.status(500).json({ message: 'Failure to edit note' });
})
} else {
res.status(400).json({ message: 'Missing title or body' })
};
});

server.delete('/api/notes/:id', (req, res) => {
const { id } = req.params;

db('notes').where({ id }).del()
.then(note => {
Copy link

Choose a reason for hiding this comment

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

This parameter is called note, but is it actually? (It should be a count of sorts, typically being 1, of how many entries were deleted.)

res.status(201).json('Note has been deleted');
})
.catch(err => {
res.status(500).json({ message: 'Failed to delete note' });
})
});

server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
});

// Front end is now working with back end with complete functionality!
16 changes: 16 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {

development: {
client: 'sqlite3',
connection: {
filename: './data/notes.sqlite3'
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
}
}
};
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "back-end-project-week",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"server": "nodemon index.js"
},
"repository": "https://github.com/mitchellr92/back-end-project-week.git",
"author": "<[email protected]>",
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.16.4",
"helmet": "^3.15.1",
"knex": "^0.16.3",
"morgan": "^1.9.1",
"sqlite3": "^4.1.1"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
Loading