p5.js-web-editor/server/controllers/collection.controller/collectionForUserExists.js
Andrew Nicolaou d02a413bf3 Displays existing collection
- List all collections for a given user
- View an individual collection
- Link to a sketch from a collection
2019-09-08 17:24:46 +02:00

29 lines
640 B
JavaScript

import Collection from '../../models/collection';
import User from '../../models/user';
export default function collectionForUserExists(username, collectionId, callback) {
function sendFailure() {
callback(false);
}
function sendSuccess(collection) {
callback(collection != null);
}
function findUser() {
return User.findOne({ username });
}
function findCollection(owner) {
if (owner == null) {
throw new Error('User not found');
}
return Collection.findOne({ _id: collectionId, owner });
}
return findUser()
.then(findCollection)
.then(sendSuccess)
.catch(sendFailure);
}