Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
1b04b0b
added the trello URL
CBMR Feb 12, 2019
bd23360
added knex, sqlite3, dotenv, jest, supertest, nodemon
CBMR Feb 12, 2019
1fc9fba
configured the knex file testing and development
CBMR Feb 12, 2019
da8d117
added the node modules to the gitignore
CBMR Feb 12, 2019
39c36e5
added useNullAsDefault to the knex file
CBMR Feb 12, 2019
19a1154
ran a migration and seed
CBMR Feb 12, 2019
0e1ff4d
created the db config file and configured it
CBMR Feb 12, 2019
310113a
created the index file and set the port for the server
CBMR Feb 12, 2019
608c0c7
added .env file to the gitignore
CBMR Feb 12, 2019
e9d6bf3
created the server
CBMR Feb 12, 2019
107b5f1
created tests fot the API
CBMR Feb 13, 2019
f384dbb
ran the migration on the main development database
CBMR Feb 13, 2019
5068ac3
added cross env
CBMR Feb 13, 2019
a29a1c4
added the models
CBMR Feb 13, 2019
48312ac
added the API endpoints
CBMR Feb 13, 2019
343f9e8
added cors and moment.js
CBMR Feb 14, 2019
d50b424
added moment to format the timestamp
CBMR Feb 14, 2019
7ef5ef5
added notes
CBMR Feb 14, 2019
ae6fd82
added notes
CBMR Feb 14, 2019
8f3a771
added data
CBMR Feb 15, 2019
9af9686
changed how the server grabs the times
CBMR Feb 15, 2019
42ddfac
made a few changes to the schema of the database
CBMR Feb 15, 2019
0ddf5da
added the data to the DB
CBMR Feb 16, 2019
4ee6d2d
added the procfile for heroku
CBMR Feb 16, 2019
7cbd4be
removed moment js from backend
CBMR Feb 16, 2019
f4a0f76
dropped the old table, create new notes and users
CBMR Feb 19, 2019
b366ba3
added bcryptjs and jwt
CBMR Feb 19, 2019
ed8ba7f
made a change to the getNotes
CBMR Feb 19, 2019
73bd52f
started using express routes
CBMR Feb 19, 2019
75a092e
added a users model
CBMR Feb 19, 2019
b83115f
created the routes using express router
CBMR Feb 19, 2019
02b5ec5
corrected the if statement
CBMR Feb 19, 2019
10f8a40
added data
CBMR Feb 22, 2019
14e7800
made a change to how I get data
CBMR Feb 22, 2019
a2f7722
added routes
CBMR Feb 22, 2019
719e5f9
added userModels
CBMR Feb 22, 2019
825060b
Merge pull request #1 from CBMR/authentication
CBMR Feb 22, 2019
fe618a8
corrected endpoints
CBMR Feb 22, 2019
f6a6664
corrected the header data
CBMR Feb 22, 2019
9c7d35a
removed a console log
CBMR Feb 22, 2019
32b63b3
refactored the protected function
CBMR Feb 22, 2019
3bfbbe3
corrected a bug
CBMR Feb 22, 2019
98f1f5b
refactored
CBMR Feb 22, 2019
60188d1
corrected bug
CBMR Feb 22, 2019
5979820
bug fix
CBMR Feb 22, 2019
021cf29
bug fix
CBMR Feb 22, 2019
a49ec68
bug fix
CBMR Feb 22, 2019
0ac4550
bug fix
CBMR Feb 22, 2019
1c8277c
refactored
CBMR Feb 22, 2019
4b96f51
refactor
CBMR Feb 22, 2019
2549350
bug fix
CBMR Feb 22, 2019
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
.DS_Store

node_modules

.env
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node index.js
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ You are required to showcase progress with at least 4 commits a day. This will l

## Trello Set Up

##Trello URL
https://trello.com/b/MNlw7eFE/lambda-notes-backend-by-abdiel-fernandez

- Use your existing Trello account from the Front End Project, or create a new one.
- Create a new board called "Lambda Notes(Backend) - {Your Name}".
- Create lists titled `Backlog`,`To do`, `Blocked`, `In Progress`, and `Done`.
Expand Down
6 changes: 6 additions & 0 deletions data/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const knex = require('knex')
const config = require('../knexfile')

const dbEnv = process.env.DB_ENV || 'development'

module.exports = knex(config[dbEnv])
15 changes: 15 additions & 0 deletions data/migrations/20190211224425_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', table => {
table.increments();

table.string('title', 255).notNullable();
table.string('content', 10000).notNullable();

Choose a reason for hiding this comment

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

You could also do something like this

Suggested change
table.string('content', 10000).notNullable();
table.text('content').notNullable();

table.boolean('completed').notNullable()
table.timestamp('time_posted', true).defaultTo(knex.fn.now());
})
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('notes')
};
12 changes: 12 additions & 0 deletions data/migrations/20190214203238_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.up = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.timestamp('time_updated', true)
})
};

exports.down = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.dropColumn('time_updated')
})
};
12 changes: 12 additions & 0 deletions data/migrations/20190214204912_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.up = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.dropColumn('time_posted')
})
};

exports.down = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.dropColumn('time_posted')
})
};
12 changes: 12 additions & 0 deletions data/migrations/20190214205405_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.up = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.timestamp('time_posted')
})
};

exports.down = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.dropColumn('time_posted')
})
};
10 changes: 10 additions & 0 deletions data/migrations/20190214210109_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

exports.up = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.dropColumn('time_updated')
})
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('notes')
};
12 changes: 12 additions & 0 deletions data/migrations/20190214213518_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.up = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.timestamp('time_updated')
})
};

exports.down = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.dropColumn('time_updated')
})
};
13 changes: 13 additions & 0 deletions data/migrations/20190218203926_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

exports.up = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.integer('user_id').unsigned()
table.foreign('user_id').references('id').on('users')
})
};

exports.down = function(knex, Promise) {
return knex.schema.table('notes', table => {
table.dropColumn('user_id')
})
};
8 changes: 8 additions & 0 deletions data/migrations/20190218213446_droppingTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

exports.up = function(knex, Promise) {
return knex.schema.dropTableIfExists('notes')
};

exports.down = function(knex, Promise) {
return null
};
20 changes: 20 additions & 0 deletions data/migrations/20190218213733_creatingNotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

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

table.string('title', 255).notNullable();
table.string('content', 10000).notNullable();
table.boolean('completed').notNullable()

table.timestamp('time_posted')
table.timestamp('time_updated')

table.integer('user_id').unsigned()
table.foreign('user_id').references('id').on('users')
})
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('notes')
};
15 changes: 15 additions & 0 deletions data/migrations/20190218215331_users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

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

table.string('first_name').notNullable()
table.string('last_name').notNullable()
table.string('username').notNullable().unique()
table.string('password').notNullable()
})
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('users')
};
Binary file added data/notes_dev.sqlite3
Binary file not shown.
13 changes: 13 additions & 0 deletions data/seeds/001_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').truncate()
.then(function () {
// Inserts seed entries
return knex('notes').insert([
{id: 1, title: 'Test one', content: 'This is the first test'},
{id: 2, title: 'Test Two', content: 'This is the second test'},
{id: 3, title: 'Test Three', content: 'This is the third test'}
]);
});
};
Binary file added data/test.sqlite3
Binary file not shown.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require('dotenv').config()

const server = require('./server/server');

PORT = process.env.PORT || 4500

Choose a reason for hiding this comment

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

Missing a const here

Suggested change
PORT = process.env.PORT || 4500
const PORT = process.env.PORT || 4500


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

module.exports = {

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

testing: {
client: 'sqlite3',
connection: {
filename: './data/test.sqlite3'
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations',
},
seeds: {
directory: './data/seeds'
},
},
};
29 changes: 29 additions & 0 deletions notes/notesModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const db = require('../data/dbConfig');

const getAllNotes = (id) => {
return db('notes').where('user_id', id)
}

const getNoteByID = (id) => {
return db('notes').where('id', id)
}

const createNote = (note) => {
return db('notes').insert(note)
}

const updateNote = (id, change) => {
return db('notes').where('id', id).update(change)
}

const deleteNote = (id) => {
return db('notes').where('id', id).del()
}

module.exports = {
getAllNotes,
getNoteByID,
createNote,
updateNote,
deleteNote,
}
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "back-end-project-week",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"server": "nodemon",
"test": "cross-env DB_ENV=testing jest --watch --verbose"
},
"repository": "https://github.com/CBMR/back-end-project-week.git",
"author": "Abdiel Fernandez <[email protected]>",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"jsonwebtoken": "^8.4.0",
"knex": "^0.16.3",
"moment": "^2.24.0",
"sqlite3": "^4.0.6"
},
"devDependencies": {
"cross-env": "^5.2.0",
"jest": "^24.1.0",
"nodemon": "^1.18.10",
"supertest": "^3.4.2"
}
}
90 changes: 90 additions & 0 deletions server/routes/notesRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const express = require('express');
const jwt = require('jsonwebtoken')

const router = express.Router()

const notes = require('../../notes/notesModel')
const secret = process.env.SECRET ||'the-future-is-unknown'

function protected(req, res, next) {
const token = req.headers.authorization
if (token) {
jwt.verify(token, secret, (err, decodedToken) => {
if(err) {
res.status(401).json({errMessage: "invalid token"})
} else {
next()
}
})
} else {
res.status(401).json({errMessage: "no token attached"})
}
}


router.get('/notes/:id', protected, async (req, res) => {
console.log(req.params)
const {id} = req.params
console.log(id)
try {
const allNotes = await notes.getAllNotes(id)
res.status(200).json(allNotes)
} catch (error) {
res.status(500).json({failure: 'unable to get all the notes'})
}
})

router.get('notes/:id/note/:id', protected, async (req, res) => {
const { id } = req.params;
try {
let note = await notes.getNoteByID(id)
note = note[0];
res.status(200).json(note)
} catch (error) {
res.status(500).json({failure: 'unable to get the note'})
}
})

router.post('notes/:id/note/create', protected, async (req, res) => {
const {title, content} = req.body

if(title && content) {
try {
await notes.createNote(req.body)
res.status(201).json({success: "the note has been added"})
} catch (error) {
res.status(500).json({failure: "unable to create the note"})
}
} else {
res.status(422).json({failure: "please add the title or the content"})
}
})

router.put('notes/:id/note/:id/edit', protected, async (req, res) => {
const { id } = req.params
const { title, content } = req.body

if (title && content ) {
try {
await notes.updateNote(id, req.body)
res.status(201).json({success: "the note has been updated"})
} catch (error) {
res.status(500).json({failure: 'unable to update the note'})
}
} else {
res.status(422).json({failure: 'please make sure to add a title and content'})
}
})

router.delete('notes/:id/note/:id/delete', protected, async (req, res) => {
const { id } = req.params

try {
await notes.deleteNote(id)
res.status(200).json({success: "the note has been deleted"})
} catch (error) {
res.status(500).json({failure: 'unable to delete the note'})
}
})

module.exports = router
Loading