-
Notifications
You must be signed in to change notification settings - Fork 446
Humberto Raya /back-end-project-week #495
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
Open
hraya
wants to merge
11
commits into
bloominstituteoftechnology:master
Choose a base branch
from
hraya:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8448c97
Initial Commit
hraya 6edc1e2
Began Setting up server
hraya a73d7ae
Seeded Data
hraya c1baf65
Began endpoints
hraya d16e4ef
Completed endpoints
hraya 5bf5c6c
Added postgres
hraya ec7fd5d
Fixed script
hraya cce8ec1
Messing with cors
hraya 41e6e62
Debugging cors
hraya 5d48d30
Fixed PORT
hraya 2c7fe99
Messing with cors
hraya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| .DS_Store | ||
| node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!!!!'} | ||
| ]); | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| }).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}!`) | ||
| }) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | ||
| } | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
notesvsnote. The data that you're sending back is thereq.bodythat the client sent over. This doesn't include the new id since you're not sending back your innermost callback parameter!