Permalink
Browse files

add seeds file & sample controller

  • Loading branch information...
faisal committed May 4, 2018
1 parent 45f6211 commit 2b770b998dec764d7e1429217da504757c8e37bc
Showing with 77 additions and 6 deletions.
  1. +10 −4 back-end/controllers/comments.js
  2. +63 −0 back-end/db/seed.js
  3. +4 −2 back-end/models/textPost.js
@@ -1,16 +1,22 @@
var models = require('../models');
var Comment = models.Comment;
var TextPost = models.TextPost;

function create(req, res) {

// your code here
}

function update(req, res) {

}
console.log('****', req.body);
Comment.findByIdAndUpdate(req.params.comment_id,
{$set: req.body}, function(err, comment){
if (err) res.send(err);
else res.json(comment);
});
}

function destroy(req, res) {

// OPTIONAL: your code here
}

module.exports.create = create;
@@ -0,0 +1,63 @@
var models = require('../models');

var commentsData = [{
content: 'Comment A',
votes: 6
}, {
content: 'Comment B',
votes: 3
}, {
content: 'Comment C',
votes: 12
}];

models.Comment.remove({}, function(err, res) {
if (err) {
console.log('Error removing comments: ', err);
return;
}
console.log('Removed all Comments');

models.Comment.create(commentsData, function(err, comments) {
if (err) {
console.log('Error creating comments: ', err);
return;
}
console.log('Created', comments.length, 'comments');

models.TextPost.remove({}, function(err, res) {
if (err) {
console.log('Error removing TextPosts', err);
return;
}
console.log('Removed all TextPosts');

models.TextPost.create([{
title: 'Title',
content: 'Content',
thumbnail_image_url: 'http://www.readersdigest.ca/wp-content/uploads/2011/01/4-ways-cheer-up-depressed-cat.jpg',
votes: 3,
comments: [comments[0], comments[1]]
}, {
title: 'Another Title',
content: 'Some more content',
thumbnail_image_url: 'http://www.petmd.com/sites/default/files/sleepy-cat-125522297.jpg',
votes: 8,
comments: [comments[2]]
}, {
title: 'My Last Title',
content: 'Yo some dope content',
thumbnail_image_url: 'https://ichef.bbci.co.uk/news/1024/cpsprodpb/693C/production/_95804962_p0517py6.jpg',
votes: 12,
comments: []
}], function(err, posts) {
if (err) {
console.log('Error creating TextPosts', err);
return;
}
console.log('Created', posts.length, 'posts');
return;
});
});
})
});
@@ -1,13 +1,15 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Comment = require('./comment');
CommentSchema = require('./comment').schema;

var TextPostSchema = new Schema({
title: String,
content: String,
thumbnail_image_url: String,
votes: Number,
comments: [Comment.schema]
comments: [CommentSchema],
}, {
timestamps: true
});

var TextPost = mongoose.model('TextPost', TextPostSchema);

0 comments on commit 2b770b9

Please sign in to comment.