-
Notifications
You must be signed in to change notification settings - Fork 0
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
WIP: Exercise 4 #7
Closed
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
593ff01
Add JWT, Graphql-middleware, Graphql-shield, signup, bcrypt, dotenv
yiimnta 5f666e9
test login, update createPost, votePost
yiimnta 0336687
temp
ilonae 6759e02
adding login, edit createPost, votePost
yiimnta 0f8020a
add jest test
yiimnta 8dacaa7
adding intermediate fixes
ilonae File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 @@ | ||
JWT_SECRET=thecountryroads | ||
This file contains 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,6 +1,6 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"node": true, | ||
"commonjs": true, | ||
"es2021": true | ||
}, | ||
|
This file contains 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 |
---|---|---|
|
@@ -12,9 +12,14 @@ | |
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"apollo-datasource-rest": "^0.9.5", | ||
"apollo-datasource": "^0.7.2", | ||
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. @yiimnta Danke, dass du das geändert hast! |
||
"apollo-server": "^2.19.0", | ||
"bcrypt": "^5.0.0", | ||
"dotenv": "^8.2.0", | ||
"graphql": "^15.4.0", | ||
"graphql-middleware": "^4.0.2", | ||
"graphql-shield": "^7.4.2", | ||
"jsonwebtoken": "^8.5.1", | ||
"nodemon": "^2.0.6" | ||
}, | ||
"devDependencies": { | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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,87 @@ | ||
const {DataSource} = require('apollo-datasource'); | ||
const {UserInputError} = require('apollo-server'); | ||
const crypto = require('crypto'); | ||
|
||
class Post { | ||
constructor (data) { | ||
this.id = crypto.randomBytes(16).toString('hex'); | ||
this.voters = []; | ||
this.votes = 0; | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Object.assign(this, data); | ||
} | ||
} | ||
|
||
class PostsDataSource extends DataSource { | ||
|
||
constructor() { | ||
super(); | ||
this.posts = []; | ||
} | ||
|
||
async allPosts () { | ||
return this.posts; | ||
} | ||
|
||
async createPost (data, currUser) { | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if(currUser){ | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const newPost = new Post({...data, author:currUser}); | ||
this.posts.push(newPost); | ||
|
||
return newPost; | ||
} | ||
|
||
return null; | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
async votePost(id, value, currUser) { | ||
const post = this.posts.find(e => e.id == id); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (post) { | ||
|
||
if(currUser) { | ||
|
||
if(!post.voters.find(voter => voter.id == currUser.id)){ | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
post.votes+= value; | ||
post.voters.push(currUser); | ||
|
||
return post; | ||
} | ||
else{ | ||
throw new UserInputError("This user voted on this post already"); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
else{ | ||
throw new UserInputError("No post with this ID", {invalidArgs: [id]}); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
async deletePost (id, currUser) { | ||
|
||
const post = this.posts.find(e => e.id == id); | ||
|
||
if (post) { | ||
|
||
if(currUser) { | ||
|
||
if( currUser.id == post.author.id) { | ||
|
||
const deleteIndex = this.posts.indexOf(post); | ||
this.posts.splice(deleteIndex,1); | ||
|
||
return post; | ||
|
||
}else { | ||
throw new Error("Only authors of a post are allowed to delete their own posts."); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
else{ | ||
throw new UserInputError("No post with this ID", {invalidArgs: [id]}); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} | ||
} | ||
module.exports = {Post, PostsDataSource} |
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.
❌ If this stays, you will loose a ⭐ here.
You have multiple options:
a) Mock
jsonwebtoken
node module in your tests, see our forum our group lichtow: Systems-Development-and-Frameworks/lichtow@6d164b9#diff-45f14d66b6a3b7ed6473601b04ce8c30013d014dfd73e439376d93e9196f93dcR98b) Use dotenv-flow and commit a non-secret in
.env.test
c) Explain in README.md one has to create a
.env
file before running the tests.