generated from Technigo/express-api-starter
-
Notifications
You must be signed in to change notification settings - Fork 30
Sofia Lennbom API - Happy thoughts #14
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
solen80a
wants to merge
34
commits into
Technigo:master
Choose a base branch
from
solen80a: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
34 commits
Select commit
Hold shift + click to select a range
dfb213a
Created notes, started with endpoints and installed dependencies.
solen80a 30afbdc
Added query param for messages added today, /messagesfromtoday
solen80a b638bd4
Connected the root to listEndpoints and moved my html documentation t…
solen80a 3746263
Changed from messages to thoughts
solen80a 06e6057
Installed and imported mongoose
solen80a dafb7fc
Added Url and Schema
solen80a e790ebd
Seeded the DB with data from json. Started to transform the code to f…
solen80a 744fa8b
Endpoint /thoughts with query params updated to fetch from API
solen80a 3df9184
Updated endpoint /thoughts/:id to fetch from DB.
solen80a 45c8320
Some cleaning.
solen80a dfde63d
Added a POST for new thoughts.
solen80a c3d4609
Added a DELETE for endpoint /thoughts/:id.
solen80a c938b18
Added PATCH for new thougth message and like.
solen80a e44a2ae
Added POSt to increase likes to a thought. Also added message to a li…
solen80a e209250
Changed the response on my get /thoughts
solen80a 3c662ce
Note update
solen80a 1b80d42
Deployed and connected API and DB. Installed dotenv and created a env…
solen80a b2d38fc
Added bcrypt. Created models thoughts and users
solen80a 03c4556
Updated patch
solen80a 7b7a74e
Updates to patch
solen80a 2ec257a
Made som comments.
solen80a 6df9d70
Broke out models for user and thought. Created get post for users and…
solen80a d2509ad
Broke out authanticateuser and created routes for thoughts and user, …
solen80a 09dd7fd
Made som cleaning of unused code.
solen80a fac38d2
Added to include user for edit, delete, post/get thought.
solen80a 427b13b
More fixes to POST thought to identified user.
solen80a e903905
More authentication fixes.
solen80a e71a4fd
more fixes
solen80a cfeda6d
Cleaning and checking tasks.
solen80a 5097a6d
Update deployed frontend and backend.
solen80a e25664b
Fixed user already exist error handling.
solen80a 21f3752
Removed unused code. Added maxlength to message. Added await to the u…
solen80a 55b2a19
Added check for password length.
solen80a ccdc4ef
removed empty files.
solen80a 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
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,11 @@ | ||
import { User } from "../models/user" | ||
|
||
export const authenticateUser = async (req, res, next) => { | ||
const user = await User.findOne({accessToken: req.header("Authorization")}) | ||
if(user) { | ||
req.user = user | ||
next() | ||
} else { | ||
res.status(401).json({loggedOut: true}) | ||
} | ||
} |
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,25 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const thoughtSchema = new mongoose.Schema({ | ||
message: { | ||
type: String, | ||
required: true, | ||
minlength: 5, | ||
maxlength: 140 | ||
}, | ||
hearts: { | ||
type: Number, | ||
default: 0 | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
}, | ||
user: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User", | ||
required: true | ||
} | ||
Comment on lines
+18
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⭐ |
||
}) | ||
|
||
export const Thought = mongoose.model("Thought", thoughtSchema) |
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,30 @@ | ||
//import bcrypt from "bcrypt" | ||
import crypto from "crypto" | ||
import mongoose from "mongoose"; | ||
|
||
const userSchema = new mongoose.Schema({ | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
minlength: 3, | ||
maxlength: 100 | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
minlength: 3, | ||
maxlength: 100 | ||
}, | ||
solen80a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
accessToken: { | ||
type: String, | ||
default: () => crypto.randomBytes(128).toString("hex") | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}) | ||
|
||
export const User = mongoose.model("User", userSchema) | ||
|
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,67 @@ | ||
W1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clear todo list! Love it. |
||
[x] Your API should have at least three (for now) routes. Try to push yourself to do more, though! | ||
[x] The endpoint "/" should return documentation of your API using e.g. Express List Endpoints | ||
[x] A minimum of one endpoint to return a collection of results (an array of elements). | ||
[x] A minimum of one endpoint to return a single result (single element). | ||
[x] Your API should be RESTful | ||
[x] You should follow the guidelines on how to write clean code. | ||
|
||
W2: | ||
[X] Your API should use Mongoose models to model your data and use these models to fetch data from the database. | ||
[x] Your API should validate user input and return appropriate errors if the input is invalid. | ||
[x] You should implement error handling for all your routes, with proper response statuses. | ||
[x] Your frontend should be updated with the possibility to Update and Delete a thought. | ||
[x] Update | ||
[x] Delete | ||
[x] Deployed backend API with deployed DB | ||
[x] Posibility to: | ||
[x] POST | ||
[x] PATCH | ||
[x] DELETE | ||
[x] Frontend posibility to POST (to your API) | ||
|
||
W3: | ||
[x] Signup and Login existing Happy Thoughts | ||
[x] Your API should have routes to register and log in | ||
[x] Your endpoints to Create, Update and Delete should be authenticated | ||
[x] Your frontend should have a registration form which POSTs to the API to create a new user. | ||
[x] Your frontend should have a login form to authenticate the user. | ||
[x] Your passwords in the database should be encrypted with bcrypt. | ||
[x] You should implement error handling. Your API should let the user know if something went wrong. Be as specific as possible to help the user, e.g. by validating the user input when creating a new user, and return "That email address already exists". Include correct status codes. Error messages should also be shown on the frontend. | ||
[x] The validation should ensure unique email addresses and/or usernames, depending on how you'd like to structure your User model. | ||
|
||
|
||
Requirements: | ||
[x] Your API must have at least the following routes. Try to push yourself to do more, though! Endpoints for: | ||
[x] Documentation of your API using e.g. Express List Endpoints | ||
[x] Reading thoughts | ||
[x] Reading a single thought | ||
[x] Liking a thought | ||
[x] Creating a thought (authenticated) | ||
[x] Updating a thought (authenticated) | ||
[x] Deleting a thought (authenticated) | ||
[x] Signing up | ||
[x] Logging in | ||
[x] Your API should be RESTful | ||
[x] You should follow the guidelines on how to write clean code | ||
[x] Your API should use Mongoose models to model your data and use these models to fetch data from the database. | ||
[x] Your API should validate user input and return appropriate errors if the input is invalid. | ||
[x] The validation should ensure unique email addresses and/or usernames depending on how you'd like to structure your User model. | ||
[x] You should implement error handling for all your routes, with proper response statuses. | ||
[x] Your frontend should be updated with the possibility to Update and Delete a thought, as well as signing up and logging in and some error handling if something goes wrong. | ||
[x] Your passwords in the database should be encrypted with bcrypt. | ||
[x] Your API should be deployed to Render or similar. | ||
[x] Everything in your backend should be reflected in your frontend. | ||
|
||
Stretch goal options: | ||
[] Allow anonymous (not-logged-in) users to post thoughts | ||
[] As a logged-in user, you should be able to see a list of the thoughts that you've liked. | ||
[] Give the thoughts a category or tags. So you could organise them. For example, 'Food thoughts', 'Project thoughts', 'Home thoughts', etc. | ||
[] Add filtering and sorting options to the endpoint which returns all thoughts. Examples: | ||
[] Sorting on date or number of likes | ||
[] Filtering to only see thoughts with more than x hearts, thoughts newer than x date or thoughts in a specific category (if you implemented categories) | ||
[] Implement pagination in your backend & frontend so you can click through pages of thoughts. The frontend could request a specific page and show only that page. The backend would take the request for that page and return only the thoughts for that page. | ||
[] You could also experiment with implementing infinite scrolling on the frontend rather than having a list of page numbers. This idea is similar to paging and involves frontend & backend changes. | ||
[] When registering, display error messages from the API in the frontend, next to the field which has the error. For example, if the email address is invalid, show an error message next to the email input. | ||
[] In the frontend, store the token in localStorage and send it in headers to persist the logged-in state for the user. | ||
[] Validate the email address format using a regular expression. |
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
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.
Superclear structure on the models and its easy to follow your code