Guest User

Untitled

a guest
Nov 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. const mongoose = require('mongoose')
  2. const Schema = mongoose.Schema
  3.  
  4. var UserActivitySchema = new Schema({
  5. author: {type: mongoose.Schema.Types.ObjectId, required: true, unique: true, ref: 'user'},
  6. readHistory: [{articleid: {type: mongoose.Schema.Types.ObjectId, ref: 'article'}, date_read: {type: Date, default: Date.now()}}],
  7. watchHistory: [{videoid: {type: mongoose.Schema.Types.ObjectId, ref: 'video'}, date_watch: {type: Date, default: Date.now()}}],
  8. searchHistory: [{query: String, date:{type: Date, default: new Date()}}]
  9. })
  10.  
  11. module.exports = mongoose.model('useractivity',UserActivitySchema, 'useractivities')
  12.  
  13. if (req.query.page) var page = parseInt(req.query.page);
  14. else var page = 1;
  15. UserActivity.findOne({author: req.client.userId}).select('readHistory')
  16. .populate({path: 'readHistory.articleid', select: 'title description views thumbnailURI', options: { skip: (page-1)*10, limit: 10}, populate: {path: 'author', select: 'displayName -_id'}})
  17. .exec().then(activity=>{
  18. if (!activity || activity.readHistory.length <= 0)
  19. res.json({success: false, message: 'You have not read any article yet.'})
  20. else {
  21. res.json({success: true, readHistory: activity.readHistory})
  22. }
  23. }).catch(err=>{
  24. res.json({success: false, message: err.message})
  25. })
Add Comment
Please, Sign In to add comment