Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back-End Project Week Corey Kohler #494

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ Once your MVP has been approved, you have been given a feature list that the cli
- Gate your favorite feature behind the _premium_ paywall

You will notice that this repository does not have any starter code. This is on purpose. You are to start from scratch using any files you have built throughout your time here at Lambda School as reference.


## Trello Information

https://trello.com/b/hCCuXhq4/lambda-notes-backend-corey-kohler
34 changes: 34 additions & 0 deletions data/dbHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const knex = require('knex');
const knexConfig = require('../knexfile');
const db = knex(knexConfig.development);


module.exports = {
find: () => {
return db('notes');
},

findById: (id) => {
return db('notes').where({ id: Number(id) });
},

insert: (note) => {
return db('notes')
.insert(note)
.then(ids => ({
id: ids[0]
}))
},

update: (id, note) => {
return db('notes')
.where('id', Number(id))
.update(note);
},

remove: (id) => {
return db('notes')
.where('id', id)
.del();
}
}
15 changes: 15 additions & 0 deletions data/migrations/20190212111847_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

exports.up = function(knex, Promise) {
return knex.schema.createTable('notes', (notes) => {
notes.increments()

notes.string('title', 255).notNullable();
notes.text('note').notNullable();

notes.timestamps(true, true);
})
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('notes');
};
Binary file added dev.sqlite3
Binary file not shown.
128 changes: 128 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
const express = require('express');
const server = express();
const db = require('./data/dbHelpers');
const cors = require('cors');


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


const PORT = 5050

//--------------------Create-------------------------

server.post('/api/notes', (req, res) => {
const note = req.body;
if(note.title && note.note) {
db.insert(note)
.then(noteInfo => {
db.findById(noteInfo.id)
.then(note => {
res.status(201)
.json(note);
})
})
.catch(err => {
res
.status(500)
.json({ message: "Failed to insert note into Database."})
})
} else {
res
.status(400)
.json({ message: "Please provide title and content for your note"})
}
})

//--------------------Read---------------------------

server.get('/api/notes', (req, res) => {
db.find()
.then(notes => {
if(notes.length > 0) {
res.json(notes)
} else {
res.json({ message: "There are currently no notes"})
}
})
.catch(err => {
res.status(500)
.json({ err: 'The notes information could not be retrieved' })
})
})

server.get('/api/notes/:id', (req, res) => {
const { id } = req.params;
db.findById(id)
.then(note => {
if(note.length === 1) {
res.json(note);
} else {
res
.status(404)
.json({ message: 'The note with the specified ID does not exist' })
}
})
.catch(err => {
res.status(500)
.json({ message: "The note information could not be returned" })
})

})

//--------------------Update-------------------------

server.put('/api/notes/:id', (req, res) => {
const { id } = req.params;
const note = req.body;
if(note.title && note.note) {
db.update(id, note)
.then(updatedNote => {
console.log(updatedNote)
if(updatedNote) {
db.findById(id)
.then(note => {

res.json(note);
})
} else {
res
.status(404)
.json({ message: "The post with the specified ID does not exist"})
}
})
.catch( err => {
res
.status(500)
.json({ message: 'The note could not be updated'})
})
} else {
res
.status(400)
.json({ message: "Please provide a title and note to be updated" });
}
})

//--------------------Destroy------------------------

server.delete('/api/notes/:id', (req, res) => {
const { id } = req.params;
console.log(id);
db.remove(id)
.then(count => {
if(count) {
res.json({ message: "Note was successfully deleted"})
} else {
res.json({ message: "The post with the specified ID does not exist"})
}
})
.catch( err => {
res.status(500)
.json({ message: "The post could not be removed"})
})
})

server.listen(PORT, () => {
console.log(`Server is listening on PORT ${PORT}`)
})
48 changes: 48 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Update with your config settings.

module.exports = {

development: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3'
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations'
}
},

staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},

production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}

};
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "back-end-project-week",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"repository": "https://github.com/phinehas90/back-end-project-week.git",
"author": "Phinehas <kohler.nai@gmail.com>",
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.16.4",
"knex": "^0.16.3",
"sqlite3": "^4.0.6"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
Loading