-
Notifications
You must be signed in to change notification settings - Fork 446
Angelina La Salle | Back-End-Project-Week #483
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
alasalle
wants to merge
82
commits into
bloominstituteoftechnology:master
Choose a base branch
from
alasalle: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
82 commits
Select commit
Hold shift + click to select a range
806aae2
added dependencies
alasalle 21cd429
created basic file/folder structure
alasalle ecf475f
added notes database
alasalle c0ab9c6
set up config file
alasalle 2b47e10
set up index.js
alasalle 87511c5
starting to make API in server.js
alasalle cb12c79
created helper functions for notes
alasalle 9f9c235
created get endpoints for all notes
alasalle 0b771ef
added post endpoint for note
alasalle 68accfb
added put enpoint for notes
alasalle df4e5ed
completed delete endpoint for notes
alasalle e558587
cleaned up server.js
alasalle 4e36039
added users table so I can set up auth
alasalle 00de8d2
completed auth
alasalle dd6c9e2
attempting heroku deployment
alasalle bf54bdf
still working w/ heroku
alasalle 8705621
heroku
alasalle 4c7afc7
heroku is up and running, added initial message
alasalle e85c234
changed post tags endpoint for easier use
alasalle b8a2e20
added params to post tags endpoint
alasalle 48c99ef
added logout
alasalle 4ea3c87
hooked authRouter up to server.js
alasalle 6258837
trying to fix 500 error when registering
alasalle 426ccf6
trying to fix 500 error when registering
alasalle 65b7f0c
getting rid of authentication to check functionality:
alasalle d20c851
still working on server error
alasalle f46d1b3
got rid of auth
alasalle 0fd511b
trying to fix addTag error
alasalle 5d1427e
tring to fix add tag err
alasalle d4cc28c
working on tags
alasalle b531ae3
still fixing add new tag
alasalle f58e431
tags
alasalle e78147f
still working on this
alasalle 5e15e8a
still trying to get this to work
alasalle bcaf349
cors causing a network error
alasalle d403954
cors error
alasalle f0b1856
might have solved add tag issue, will need to check on front end
alasalle e7d4d69
working on postgres
alasalle fd62654
heroku postgres complete
alasalle bd60324
trying to get auth up and running
alasalle fdf5cc2
working on auth
alasalle c039acc
cors error
alasalle 8edb8ea
still working on auth
alasalle eb32125
no more cors error, now 500 error
alasalle 3579035
woring on authentication
alasalle ac5a598
seeing if auth code is causing server error
alasalle 8bf09bd
trying to fix server error
alasalle 946aaee
trying to fix server error
alasalle 9fd9771
still working on this server error
alasalle 9117477
server error
alasalle e8b8127
server error
alasalle 5705d8d
truncate to del so I can seed the postgres db
alasalle e9de5c7
trying auth again
alasalle cec8529
working on auth
alasalle a576a72
forgot to switch back to production
alasalle 3e32c05
working on auth
alasalle 8a0be63
still fixing auth
alasalle d638f83
503 error
alasalle d0311f2
took out auth from notes and tags routers, ran out of time to fix auth
alasalle d0a2baa
"don't want dev db in repo"
alasalle 97fab70
"changed db name"
alasalle 2b17c4c
"updated dependencies and set up new db"
alasalle 1d9c02b
"wip: fixing auth issues
alasalle 5be87cd
"got rid of old dbs"
alasalle e504aba
"rid of old stuff in package.json
alasalle d8513b7
"set up pretest to clear out test db"
alasalle 2978a99
"seeded users"
alasalle a2fb79a
"changed del to truncate"
alasalle 5d78772
"wrote basic test for server"
alasalle 11d75b9
"wrote unit tests for authRouter"
alasalle 8795ed6
"ran prettier on all files"
alasalle 3d2de15
got rid of old code
alasalle 848cdea
testing something
alasalle ec2acb2
added single user endpoints
alasalle 200ed7a
added user to notes
alasalle c365588
seeded
alasalle 4563171
changed logout
alasalle 57e7f77
testing logout
alasalle e7b4652
changed ondelete
alasalle 288cbd8
truncate to del
alasalle 5e4bceb
forgot to push to heroku master
alasalle 61a43fd
changed notes back to del
alasalle 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,4 @@ | ||
| .DS_Store | ||
| node_modules | ||
| .env | ||
| .db3 |
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
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,158 @@ | ||
| const express = require("express"); | ||
| const jwt = require("jsonwebtoken"); | ||
| const bcrypt = require("bcryptjs"); | ||
|
|
||
| const db = require("../../data/dbConfig"); | ||
| const users = require("../../users/usersModel"); | ||
| const notes = require("../../notes/notesModel"); | ||
| const tags = require("../../notes/tagsModel"); | ||
| const { generateToken } = require("../../auth/authenticate"); | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.post("/register", (req, res) => { | ||
| let user = req.body; | ||
| console.log({ user: user }); | ||
| if ( | ||
| !user.username || | ||
| typeof user.username !== "string" || | ||
| user.username === "" | ||
| ) { | ||
| res | ||
| .status(400) | ||
| .json({ error: "username must be included and must be a string" }); | ||
| } else if ( | ||
| !user.password || | ||
| typeof user.password !== "string" || | ||
| user.password === "" | ||
| ) { | ||
| res | ||
| .status(400) | ||
| .json({ error: "password must be included and must be a string" }); | ||
| } else if (user.username.length > 255) { | ||
| res.status(400).json({ error: "username must not exceed 255 characters" }); | ||
| } else if (user.password.length > 255) { | ||
| res.status(400).json({ error: "password must not exceed 255 characters" }); | ||
| } else { | ||
| // Hash password using bcrypt | ||
| const hash = bcrypt.hashSync(user.password, 10); | ||
| user.password = hash; | ||
|
|
||
| db("users") | ||
| .insert(user) | ||
| .then(ids => { | ||
| const id = ids[0]; | ||
|
|
||
| return users.fetchByUserName(user.username); | ||
| }) | ||
| .then(response => { | ||
| const token = generateToken(response); | ||
| res.status(201).json(token); | ||
| }) | ||
| .catch(err => { | ||
| res.status(500).json(err); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| router.post("/login", (req, res) => { | ||
| const user = req.body; | ||
|
|
||
| if ( | ||
| !user.username || | ||
| typeof user.username !== "string" || | ||
| user.username === "" | ||
| ) { | ||
| res | ||
| .status(400) | ||
| .json({ error: "username must be included and must be a string" }); | ||
| } else if ( | ||
| !user.password || | ||
| typeof user.password !== "string" || | ||
| user.password === "" | ||
| ) { | ||
| res | ||
| .status(400) | ||
| .json({ error: "password must be included and must be a string" }); | ||
| } else { | ||
| return users | ||
| .fetchByUserName(user.username) | ||
| .first() | ||
| .then(response => { | ||
| console.log({ user: user, response: response }); | ||
| if (response && bcrypt.compareSync(user.password, response.password)) { | ||
| const token = generateToken(response); | ||
|
|
||
| res.status(200).json(token); | ||
| } else { | ||
| res.status(401).json({ message: "Incorrect Login Information!" }); | ||
| } | ||
| }) | ||
| .catch(err => { | ||
| res.status(500).json({ err }); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| router.post("/logout", (req, res) => { | ||
| console.log("IS THIS HITTING????") | ||
| localStorage.removeItem("jwt") | ||
| res.status(200).send("logout successful"); | ||
| }); | ||
|
|
||
| router.get("/", (req, res) => { | ||
| users | ||
| .fetch() | ||
| .then(users => { | ||
| users[0] | ||
| ? res.status(200).json(users) | ||
| : res | ||
| .status(400) | ||
| .json({ error: "there are currently no users in our directory" }); | ||
| }) | ||
| .catch(err => { | ||
| res.status(500).json({ error: "could not retrieve users" }); | ||
| }); | ||
| }); | ||
|
|
||
| router.get("/:username", (req, res) => { | ||
| const { username } = req.params; | ||
| users.fetchByUserName(username).then(user => { | ||
| user | ||
| ? res.status(200).json(user) | ||
| : res.status(400).json({ error: "could not retrieve user" }); | ||
| }); | ||
| }); | ||
|
|
||
| router.get("/:username/notes", (req, res) => { | ||
| const { username } = req.params; | ||
| notes.fetchByUsername(username).then(notes => { | ||
| notes[0] | ||
| ? res.status(200).json(notes) | ||
| : res.status(400).json({ error: "could not retrieve user notes" }); | ||
| }); | ||
| }); | ||
|
|
||
| router.get("/:username/tags", (req, res) => { | ||
| const { username } = req.params; | ||
| const tagsArray = []; | ||
| notes.fetchByUsername(username).then(notes => { | ||
| notes[0] | ||
| ? notes | ||
| .forEach(note => { | ||
| return tags.fetchTagsByNote(note.id); | ||
| }) | ||
| .then(tags => { | ||
| tags[0] | ||
| ? tags.forEach(tag => { | ||
| tagsArray.push(tag); | ||
| }).then(() => { | ||
| res.status(200).json(tagArray) | ||
| }) | ||
| : res.status(200).json({ error: "User notes have no tags" }); | ||
| }) | ||
| : res.status(200).json({ error: "User has no notes" }); | ||
| }); | ||
| }); | ||
|
|
||
| module.exports = router; |
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,167 @@ | ||
| const notes = require("../../notes/notesModel"); | ||
| const tags = require("../../notes/tagsModel"); | ||
|
|
||
| const express = require("express"); | ||
| const router = express.Router(); | ||
|
|
||
| const { authenticate } = require("../../auth/authenticate"); | ||
|
|
||
| // const requestOptions = { | ||
| // headers: { | ||
| // accept: "application/json" } | ||
| // }; | ||
|
|
||
| router.get("/", (req, res) => { | ||
| notes | ||
| .fetch() | ||
| .then(notes => { | ||
| notes[0] | ||
| ? res.json(notes) | ||
| : res | ||
| .status(400) | ||
| .json({ error: "there are currently no notes in our directory" }); | ||
| }) | ||
| .catch(err => { | ||
| res.status(500).json({ error: "could not retrieve notes" }); | ||
| }); | ||
| }); | ||
|
|
||
| router.get("/:id", (req, res) => { | ||
| const { id } = req.params; | ||
| notes | ||
| .fetch(id) | ||
| .then(note => { | ||
| if (note[0]) { | ||
| res.json(note); | ||
| } else { | ||
| res.status(404).json({ error: "note does not exist" }); | ||
| } | ||
| }) | ||
| .catch(err => { | ||
| res.status(500).json({ error: "note could not be retrieved." }); | ||
| }); | ||
| }); | ||
|
|
||
| router.get("/:id/tags", (req, res) => { | ||
| const { id } = req.params; | ||
|
|
||
| notes | ||
| .fetch(id) | ||
| .then(note => { | ||
| if (note[0]) { | ||
| tags.fetchTagsByNote(note[0].id).then(tags => { | ||
| if (tags[0]) { | ||
| res.json(tags); | ||
| } else { | ||
| res.status(404).json({ error: "note has no tags" }); | ||
| } | ||
| }); | ||
| } else { | ||
| res.status(404).json({ error: "note does not exist" }); | ||
| } | ||
| }) | ||
| .catch(err => { | ||
| res.status(500).json({ error: "note tags could not be retrieved." }); | ||
| }); | ||
| }); | ||
|
|
||
| router.post("/", (req, res) => { | ||
| const note = req.body; | ||
|
|
||
| if (!note.title || typeof note.title !== "string" || note.title === "") { | ||
| res | ||
| .status(400) | ||
| .json({ error: "title must be included and must be a string" }); | ||
| } else if ( | ||
| !note.content || | ||
| typeof note.content !== "string" || | ||
| note.content === "" | ||
| ) { | ||
| res | ||
| .status(400) | ||
| .json({ error: "content must be included and must be a string" }); | ||
| } else { | ||
| notes | ||
| .insert(note) | ||
| .then(ids => { | ||
| res.status(201).json({ added: { ...note, id: ids[0] } }); | ||
| }) | ||
| .catch(err => { | ||
| res.status(500).json({ error: "trouble adding note" }); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| router.put("/:id", (req, res) => { | ||
| const newNote = req.body; | ||
| const { id } = req.params; | ||
| notes | ||
| .fetch(id) | ||
| .then(response => { | ||
| if (response[0]) { | ||
| if ( | ||
| !newNote.title || | ||
| typeof newNote.title !== "string" || | ||
| newNote.title === "" | ||
| ) { | ||
| res | ||
| .status(400) | ||
| .json({ error: "title is required and must be a string" }); | ||
| } else if ( | ||
| !newNote.content || | ||
| typeof newNote.content !== "string" || | ||
| newNote.content === "" | ||
| ) { | ||
| res | ||
| .status(400) | ||
| .json({ error: "content is required and must be a string" }); | ||
| } else { | ||
| notes | ||
| .update(id, newNote) | ||
| .then(rows => { | ||
| notes | ||
| .fetch(id) | ||
| .then(resp => res.status(201).json(resp)) | ||
| .catch(err => | ||
| res | ||
| .status(500) | ||
| .json({ error: "trouble retrieving updated note" }) | ||
| ); | ||
| }) | ||
| .catch(err => | ||
| res.status(500).json({ error: "trouble updating note" }) | ||
| ); | ||
| } | ||
| } else { | ||
| res.status(404).json({ error: "note does not exist" }); | ||
| } | ||
| }) | ||
| .catch(err => | ||
| res.status(500).json({ error: "trouble retrieving note to update" }) | ||
| ); | ||
| }); | ||
|
|
||
| router.delete("/:id", async (req, res) => { | ||
| const { id } = req.params; | ||
| const deleted = await notes.fetch(id); | ||
|
|
||
| notes | ||
| .fetch(id) | ||
| .then(note => { | ||
| if (note[0]) { | ||
| notes | ||
| .remove(id) | ||
| .then(rows => res.status(201).json(deleted)) | ||
| .catch(err => | ||
| res.status(500).json({ error: "trouble deleting note" }) | ||
| ); | ||
| } else { | ||
| res.status(404).json({ error: "note does not exist" }); | ||
| } | ||
| }) | ||
| .catch(err => | ||
| res.status(500).json({ error: "trouble retrieving note to be deleted" }) | ||
| ); | ||
| }); | ||
|
|
||
| module.exports = router; | ||
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.
Your code is in great shape Angela 👍 because you're being so thorough with your error messages think about extracting them to another file and placing them in an object, then you can inject them throughout your app, and easily add or change them.