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
26 changes: 26 additions & 0 deletions database/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const knex = require('knex');

const knexConfig = require('../knexfile.js');

db = knex(knexConfig.development);

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

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

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

remove: (id)=>{
return db('notes').where({id}).del();
}
}
12 changes: 12 additions & 0 deletions database/migrations/20190211225549_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.text('textBody').notNullable();
})
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('notes');
};
Binary file added database/notes.sqlite3
Binary file not shown.
13 changes: 13 additions & 0 deletions database/seeds/01-testNotes.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').truncate()
.then(function () {
// Inserts seed entries
return knex('notes').insert([
{title:'Test 1', textBody: 'Can I get these notes to work?!'},
{title:'Test 2', textBody: 'If you are reading this then it work!'},
{title:'Test 3', textBody: 'You my friend are a genius!!!!'}
]);
});
};
102 changes: 102 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const express = require('express');
const helmet = require('helmet');

// const cors = (req, res, next) => {
// res.header('Access-Control-Allow-Origin', '*');
// res.header('Access-Control-Allow-Credentials', true);
// res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
// res.header('Access-Control-Allow-Headers', 'Content-Type');
// next();
// }
const cors = require('cors');

const db = require('./database/dbConfig.js')


const server = express();
const PORT = process.env.PORT || 4000;

server.use(helmet());
server.use(express.json());
server.use(cors('*'));


server.post('/api/notes', (req, res)=>{
const note = req.body;
db.insert(note)
.then(ids =>{
db.findById(ids[0])
.then(notes=>{
res.status(201).json(note)
Copy link

Choose a reason for hiding this comment

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

Notice the difference between the callback's parameter name and the name of the variable you're sending back: notes vs note. The data that you're sending back is the req.body that the client sent over. This doesn't include the new id since you're not sending back your innermost callback parameter!

})
}).catch(err =>{
res.status(500).json({error:"Problem adding note", err})
})
})

server.get('/api/notes', (req,res)=>{
db.getAll()
.then(notes =>{
res.json(notes)
}).catch(err =>{
res.status(500).json({error:"Cannot get notes", err})
})
})

server.get('/api/notes/:id', (req, res)=>{
const { id } = req.params;
db.findById(id)
.then(note =>{
if(note){
res.json(note)
}else{
res.status(404).json({message:'The note with the specified id does not exist!'})
}
}).catch(err=>{
res.status(500).json({error:"Trouble getting the note", err})
})
})

server.put('/api/notes/:id', (req, res)=>{
const { id } = req.params;
const note = req.body;
if(note.title && note.textBody){
db.update(id, note)
.then(updated =>{
if(updated){
db.findById(id).then(notes=>{
res.json(notes)
}).catch(err=>{
res.status(500).json({message:"could not return updated note!"})
})
}else{
res.status(404).json({message:"The note with the specified id does not exist!"})
}
}).catch(err =>{
res.status(500).json({error:"The note could not be motified",err})
})
}else{
res.status(400).json({message:"Missing a valid title and textBody"})
}
})

server.delete('/api/notes/:id', (req, res)=>{
const { id } = req.params;
db.remove(id)
.then(removed =>{
if(removed){
res.json({message:"Note has been Deleted!"})
}else{
res.status(500).json({message:"note id does not exist!"})
}
}).catch(err =>{
res.status(500).json({error:"The note could not be removed!"})
})
})


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


33 changes: 33 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Update with your config settings.

module.exports = {

development: {
client: 'sqlite3',
connection: {
filename: './database/notes.sqlite3'
},
useNullAsDefault:true,
migrations: {
directory: './database/migrations'
},
seeds:{
directory:'./database/seeds'
}
},

production:{
client: 'pg',
connection: process.env.DATABASE_URL,
pool:{
min:2,
max:10,
},
migrations: {
directory: './database/migrations'
},
seeds:{
directory:'./database/seeds'
}
}
};
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "back-end-project-week",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"server": "nodemon",
"start": "node index.js"
},
"repository": "https://github.com/hraya/back-end-project-week.git",
"author": "hraya <[email protected]>",
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.16.4",
"helmet": "^3.15.1",
"knex": "^0.16.3",
"pg": "^7.8.0",
"sqlite3": "^4.0.6"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
Loading