Skip to content

Commit

Permalink
Added model
Browse files Browse the repository at this point in the history
  • Loading branch information
edgar971 committed Sep 27, 2017
1 parent 7f650e7 commit 714e84d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Empty file added scripts/seed.js
Empty file.
75 changes: 75 additions & 0 deletions server/models/listened.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import Promise from 'bluebird';
import mongoose from 'mongoose';
import httpStatus from 'http-status';
import APIError from '../helpers/APIError';

/**
* Listened Schema
* @property {String} userId - The id of the user.
* @property {ObjectId} postId - The id of the post.
*/
const ListenedSchema = new mongoose.Schema(
{
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
postId: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' },
},
{
timestamps: true
}
);

/**
* Add your
* - pre-save hooks
* - validations
* - virtuals
*/

/**
* Methods
*/
ListenedSchema.method({
});

/**
* Statics
*/
ListenedSchema.statics = {
/**
* Get listened post.
* @param {ObjectId} id - The objectId of listened.
* @param {ObjectId} userId - The user ID.
* @returns {Promise<ListenedSchema, APIError>}
*/
get(id, userId) {
return this.findOne({ _id: id, userId })
.exec()
.then((listened) => {
if (listened) {
return listened;
}
const err = new APIError('No such item exists!', httpStatus.NOT_FOUND);
return Promise.reject(err);
});
},

/**
* List listened items in descending order of 'createdAt' timestamp.
* @param {number} skip - Number of favorites to be skipped.
* @param {number} limit - Limit number of favorites to be returned.
* @param {number} userId - The user ID.
* @returns {Promise<ListenedSchema[]>}
*/
list({ skip = 0, limit = 50 } = {}, userId) {
return this.find({ userId })
.sort({ createdAt: -1 })
.skip(+skip)
.limit(+limit)
.exec();
}
};

/**
* @typedef ListenedSchema
*/
export default mongoose.model('Favorite', ListenedSchema);

0 comments on commit 714e84d

Please sign in to comment.