Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * List of Articles
- */
- exports.list = function (req, res) {
- Article.find({ 'user.displayName': 'GIGANTOR !' }).sort('-created').populate('user', 'displayName').exec(function (err, articles) {
- if (err) {
- return res.status(422).send({
- message: errorHandler.getErrorMessage(err)
- });
- } else {
- res.json(articles);
- }
- });
- };
- /**
- * Show the current article
- */
- exports.read = function (req, res) {
- // convert mongoose document to JSON
- var article = req.article ? req.article.toJSON() : {};
- // Add a custom field to the Article, for determining if the current User is the "owner".
- // NOTE: This field is NOT persisted to the database, since it doesn't exist in the Article model.
- article.isCurrentUserOwner = !!(req.user && article.user && article.user._id.toString() === req.user._id.toString());
- res.json(article);
- };
- 'use strict';
- /**
- * Module dependencies
- */
- var mongoose = require('mongoose'),
- Schema = mongoose.Schema;
- /**
- * Article Schema
- */
- var ArticleSchema = new Schema({
- created: {
- type: Date,
- default: Date.now
- },
- title: {
- type: String,
- default: '',
- trim: true,
- required: 'Title cannot be blank'
- },
- content: {
- type: String,
- default: '',
- trim: true,
- required: 'Content cannot be blank'
- },
- user: {
- type: Schema.ObjectId,
- ref: 'User'
- }
- });
- mongoose.model('Article', ArticleSchema);
- /**
- * User Schema
- */
- var UserSchema = new Schema({
- displayName: {
- type: String,
- trim: true
- },
- email: {
- type: String,
- index: {
- unique: true,
- sparse: true // For this to work on a previously indexed field, the index must be dropped & the application restarted.
- },
- lowercase: true,
- trim: true,
- default: '',
- validate: [validateLocalStrategyEmail, 'Please fill a valid email address']
- },
- username: {
- type: String,
- unique: 'Username already exists',
- required: 'Please fill in a username',
- validate: [validateUsername, 'Please enter a valid username: 3+ characters long, non restricted word, characters "_-.", no consecutive dots, does not begin or end with dots, letters a-z and numbers 0-9.'],
- lowercase: true,
- trim: true
- },
- password: {
- type: String,
- default: ''
- },
- salt: {
- type: String
- },
- provider: {
- type: String,
- required: 'Provider is required'
- },
- providerData: {},
- additionalProvidersData: {},
- roles: {
- type: [{
- type: String,
- enum: ['user', 'admin']
- }],
- default: ['user'],
- required: 'Please provide at least one role'
- },
- updated: {
- type: Date
- },
- created: {
- type: Date,
- default: Date.now
- },
- /* For reset password */
- resetPasswordToken: {
- type: String
- },
- resetPasswordExpires: {
- type: Date
- }
- });
- Article.find({}).sort('-created').populate('user', 'displayName').exec(function (err, articles) {
- if (err) {
- return res.status(422).send({
- message: errorHandler.getErrorMessage(err)
- });
- } else {
- let filteredArticles = articles
- .filter(article => article.user.displayName === 'GIGANTOR !');
- res.json(filteredArticles);
- }
- });
- Article.find({ user: 'somemongoobjectidofuser' }).sort('-created').populate('user', 'displayName').exec(function (err, articles) {
- if (err) {
- return res.status(422).send({
- message: errorHandler.getErrorMessage(err)
- });
- } else {
- res.json(articles);
- }
- });
- /**
- * List of Articles
- */
- exports.list = function (req, res) {
- Article.find({ user: req.user._id.toString() }).sort('-created').populate('user', 'displayName').exec(function (err, articles) {
- if (err) {
- return res.status(422).send({
- message: errorHandler.getErrorMessage(err)
- });
- } else {
- res.json(articles);
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement