Created
January 23, 2017 14:21
-
-
Save Ahrengot/583551fe02d85055c0bf06ae78154b69 to your computer and use it in GitHub Desktop.
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
Parse.Cloud.beforeSave('rating', function(request, response) { | |
/** | |
* Ensure that the video is shortlisted | |
*/ | |
var videoId = request.object.get('video') | |
var videoQuery = new Parse.Query('video'); | |
videoQuery.get(videoId).then(function(video) { | |
if ( !video.get('shortlisted') ) { | |
return response.error("Du kan kun stemme på shortlistede videoer") | |
} | |
console.log("video was shortlisted"); | |
var query = new Parse.Query('rating'); | |
return response.error("User ID from request: ", request.user.id); | |
query.equalTo('userId', request.user.id); | |
query.equalTo('video', videoId); | |
// Ensure the same user doesn't vote multiple times | |
query | |
.count() | |
.then(function(count) { | |
if ( count < 2 ) { | |
return response.success(); | |
} else { | |
return response.error("Du kan kun stemme én gang per video"); | |
} | |
}, function(error) { | |
return response.error('Ups. Der gik et eller andet galt: ' + error); | |
}) | |
}, function() { | |
response.error('Videoen findes ikke') | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment