-
Notifications
You must be signed in to change notification settings - Fork 33
Feature/api rating bookmark data model #1131
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
Merged
Merged
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
9b05952
Move permisisons to own file
brylie d682d07
Move allow rules; create initial deny rules
brylie c0b961c
Add averageRating field
brylie 1a664ac
Add bookmarkCount field
brylie 5e4262f
Don't allow averageRating or bookmarkCount to be set by user
brylie 40ee643
Add getAverageRating and getBookmarkCount helpers
brylie 437cc3b
Move helpers to own file
brylie 84c874e
Cleanup
brylie e567f88
Move helpers; add setAverageRating helper
brylie 26ff894
averageRating and bookmarkCount optional
brylie 7139fad
Ensure apiRating value exists
brylie 6449750
Add setAllApiBackendAverageRatings method
brylie e0072eb
Include averageRating field
brylie cd089ae
Pre-populate averageRating star value
brylie a72f5d9
Add setBookmarkCount method; refactor to 'bookmark'
brylie 9827184
Add setAllApiBackendBookmarkCounts method
brylie 6731c73
Comment and verbose variable name
brylie bfc8cff
Add popularity column
brylie cfd9350
Use object shorthand
brylie 0bb19fb
Unset bookmark count if no bookmarks
brylie 7e1d0cc
Add setApiBackendBookmarkCount method
brylie 90bd6ae
Call setApiBackendBookmarkCount method
brylie 16dc998
Add setApiBackendAverageRating method
brylie 16f2543
Call setApiBackendAverageRating method
brylie cad5453
Cleanup console.log
brylie efb183b
Merge branch 'develop' of github.com:apinf/api-umbrella-dashboard int…
a2f52f2
add migration
a51e66d
Remove console.log
d49e8f4
Remove bookmarkCount and averageRating migration
brylie da38c73
Set bookmarkCounts and averageRatings on startup
brylie d1b98d9
Remove console.log
brylie 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import ss from 'simple-statistics'; | ||
|
|
||
| ApiBackends.helpers({ | ||
| getAverageRating () { | ||
| console.log("getting average rating"); | ||
| // Fetch all ratings | ||
| var apiBackendRatings = ApiBackendRatings.find({ | ||
| apiBackendId: this._id | ||
| }).fetch(); | ||
|
|
||
| // If ratings exist | ||
| if (apiBackendRatings) { | ||
| // Create array containing only rating values | ||
| var apiBackendRatingsArray = _.map(apiBackendRatings, function (rating) { | ||
| // get only the rating value; omit User ID and API Backend ID fields | ||
| return rating.rating; | ||
| }); | ||
|
|
||
| // Get the average (mean) value for API Backend ratings | ||
| var apiBackendRatingsAverage = ss.mean(apiBackendRatingsArray); | ||
|
|
||
| return apiBackendRatingsAverage; | ||
| } | ||
| }, | ||
| setAverageRating () { | ||
| // get average rating value | ||
| const averageRating = this.getAverageRating(); | ||
|
|
||
| // Check if average rating calculation succeeds | ||
| if (averageRating) { | ||
| // Update the API Backend with average rating value | ||
| ApiBackends.update(this._id, {$set: {averageRating}}); | ||
| } | ||
| }, | ||
| getBookmarkCount () { | ||
| // Get API Backend ID | ||
| const apiBackendId = this._id; | ||
|
|
||
| // Get count of API Bookmarks where API Backend ID is in API Backend IDs array | ||
| const apiBookmarkCount = ApiBookmarks.find({apiIds: apiBackendId}).count(); | ||
|
|
||
| return apiBookmarkCount; | ||
| }, | ||
| setBookmarkCount () { | ||
| // get average rating value | ||
| const bookmarkCount = this.getBookmarkCount(); | ||
|
|
||
| // Check if average rating calculation succeeds | ||
| if (bookmarkCount) { | ||
| // Update the API Backend with average rating value | ||
| ApiBackends.update(this._id, {$set: {bookmarkCount}}); | ||
| } else { | ||
| ApiBackends.update(this._id, {$unset: {bookmarkCount: ""}}) | ||
| } | ||
| }, | ||
| getRating () { | ||
| // Get API Backend ID | ||
| const apiBackendId = this._id; | ||
|
|
||
| // Get current user ID | ||
| const userId = Meteor.userId(); | ||
|
|
||
| // Check if user is logged in | ||
| if (Meteor.userId()) { | ||
| // Check if user has rated API Backend | ||
| var userRating = ApiBackendRatings.findOne({ | ||
| apiBackendId, | ||
| userId | ||
| }); | ||
|
|
||
| if (userRating) { | ||
| return userRating.rating; | ||
| } | ||
| } | ||
|
|
||
| // Otherwise, get average rating | ||
| return this.averageRating; | ||
| }, | ||
| currentUserCanEdit () { | ||
| // Get current userId | ||
| var userId = Meteor.userId(); | ||
|
|
||
| // Check that user is logged in | ||
| if( userId ) { | ||
| // Check if user is API manager | ||
| var isManager = _.contains(this.managerIds, userId); | ||
|
|
||
| if (isManager) { | ||
| return true; | ||
| } | ||
|
|
||
| // Check if user is administrator | ||
| var isAdmin = Roles.userIsInRole(userId, ['admin']); | ||
|
|
||
| if (isAdmin) { | ||
| return true; | ||
| } | ||
| } else { | ||
| // User is not logged in | ||
| return false; | ||
| } | ||
| }, | ||
| currentUserIsManager () { | ||
| // Get current User ID | ||
| var userId = Meteor.userId(); | ||
|
|
||
| // Get Manager IDs array from API Backend document | ||
| var managerIds = this.managerIds; | ||
|
|
||
| // Check if User ID is in Manager IDs array | ||
| var isManager = _.contains(managerIds, userId); | ||
|
|
||
| return isManager; | ||
| } | ||
| }); |
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,52 @@ | ||
| ApiBackends.allow({ | ||
| insert: function () { | ||
| return true; | ||
| }, | ||
| update: function (userId, apiBackendDoc) { | ||
| // Save ID of API Backend | ||
| const apiBackendId = apiBackendDoc._id; | ||
| // Get API backend with ID | ||
| const apiBackend = ApiBackends.findOne(apiBackendId); | ||
| // Check if current user can edit API Backend | ||
| let currentUserCanEdit = apiBackend.currentUserCanEdit(); | ||
|
|
||
| if (currentUserCanEdit) { | ||
| // User is allowed to perform action | ||
| return true; | ||
| } else { | ||
| // User is not allowded to perform action | ||
| return false; | ||
| } | ||
| }, | ||
| remove: function (userId, apiBackendDoc) { | ||
| // Save ID of API Backend | ||
| const apiBackendId = apiBackendDoc._id; | ||
| // Get API backend with ID | ||
| const apiBackend = ApiBackends.findOne(apiBackendId); | ||
| // Check if current user can edit API Backend | ||
| let currentUserCanEdit = apiBackend.currentUserCanEdit(); | ||
|
|
||
| if (currentUserCanEdit) { | ||
| // User is allowed to perform action | ||
| return true; | ||
| } else { | ||
| // User is not allowded to perform action | ||
| return false; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| ApiBackends.deny({ | ||
| insert () { | ||
| // Don't allow user to set average rating or bookmark count fields | ||
| if (_.contains(fields, "averageRating") || _.contains(fields, "bookmarkCount")) { | ||
| return true; | ||
| } | ||
| }, | ||
| update () { | ||
| // Don't allow user to set average rating or bookmark count fields | ||
| if (_.contains(fields, "averageRating") || _.contains(fields, "bookmarkCount")) { | ||
| return 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,20 @@ | ||
| Meteor.methods({ | ||
| setAllApiBackendBookmarkCounts () { | ||
| // Get all API Backends | ||
| const apiBackends = ApiBackends.find().fetch(); | ||
|
|
||
| // Update the average rating value for each API Backend | ||
| apiBackends.forEach(function (apiBackend) { | ||
| // Set average rating value for current API Backend | ||
| apiBackend.setBookmarkCount(); | ||
| }); | ||
| }, | ||
| setApiBackendBookmarkCount (apiBackendId) { | ||
| console.log("Setting api bookmark count") | ||
| // Get API Backend | ||
| const apiBackend = ApiBackends.findOne(apiBackendId); | ||
|
|
||
| // Update the API Backend bookmark count | ||
| apiBackend.setBookmarkCount(); | ||
| } | ||
| }); |
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,20 @@ | ||
| Meteor.methods({ | ||
| setAllApiBackendAverageRatings () { | ||
| // Get all API Backends | ||
| const apiBackends = ApiBackends.find().fetch(); | ||
|
|
||
| // Update the average rating value for each API Backend | ||
| apiBackends.forEach(function (apiBackend) { | ||
| // Set average rating value for current API Backend | ||
| apiBackend.setAverageRating(); | ||
| }); | ||
| }, | ||
| setApiBackendAverageRating (apiBackendId) { | ||
| console.log("Setting api bookmark count") | ||
| // Get API Backend | ||
| const apiBackend = ApiBackends.findOne(apiBackendId); | ||
|
|
||
| // Update the API Backend bookmark count | ||
| apiBackend.setAverageRating(); | ||
| } | ||
| }); | ||
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.
@brylie method name here could be something like set ApiBackendBookmarkCount
Uh oh!
There was an error while loading. Please reload this page.
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.
Which method? The
setApiBackendAverageRatingis related to theaverageRatingfield. Hence, this method is in theratings.jsfile.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.
@brylie Ok, I confused this because console.log says about bookmarks. Could you also cleanup/fix console.logs?