Guest User

Untitled

a guest
Dec 7th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. var mongoose = require('mongoose');
  2. var Schema = mongoose.Schema;
  3.  
  4. var StorySchema = new Schema({
  5.  
  6. creator: {type:Schema.Types.ObjectId, ref: 'User'},
  7. content: String,
  8. create: { type:Date, default: Date.now }
  9. });
  10.  
  11. module.exports = mongoose.model('Story', StorySchema);
  12.  
  13. var mongoose = require('mongoose');
  14. var bcrypt = require('bcrypt-nodejs');
  15.  
  16. var Schema = mongoose.Schema;
  17.  
  18. var UserSchema = new Schema({
  19. name:String,
  20. username: { type:String, required: true, index: { unique: true}},
  21. password: { type: String, reuired: true, select: false }
  22. });
  23.  
  24. UserSchema.pre('save', function(next){
  25. var user = this;
  26.  
  27. if(!user.isModified('password')) return next();
  28.  
  29. bcrypt.hash(user.password, null, null, function(err, hash){
  30. if(err) return next(err);
  31.  
  32. user.password = hash;
  33. next();
  34. });
  35.  
  36. });
  37.  
  38. UserSchema.methods.comparePassword = function(password){
  39. var user = this;
  40. return bcrypt.compareSync(password, user.password);
  41. }
  42.  
  43. module.exports = mongoose.model('User', UserSchema);
  44.  
  45. angular.module('storyCtrl', ['storyService'])
  46.  
  47. .controller('StoryController', function(Story, socketio){
  48.  
  49. vm = this;
  50.  
  51. vm.stories=[];
  52.  
  53.  
  54.  
  55. vm.getStories = function(){
  56. Story.all()
  57.  
  58. .success(function(data){
  59. vm.stories = data;
  60. });
  61. };
  62.  
  63. vm.createStory = function(){
  64.  
  65. vm.message = '';
  66.  
  67. Story.create(vm.storyData)
  68. .success(function(data){
  69. vm.storyData.content = '';
  70. vm.message = data.message;
  71.  
  72. // vm.getStories(); // Refresh
  73.  
  74. // vm.stories.push(vm.storyData);
  75. });
  76.  
  77. };
  78.  
  79. vm.getStories();
  80.  
  81. socketio.on('story', function(data){
  82. vm.stories.push(data);
  83. });
  84. })
  85.  
  86. .controller('allStoriesController', function(stories, socketio){
  87.  
  88. var vm = this;
  89.  
  90. vm.stories = stories.data;
  91.  
  92.  
  93. socketio.on('story', function(data){
  94. vm.stories.push(data);
  95. });
  96.  
  97. })
  98.  
  99. <div>
  100. <div class="col-md-4">
  101. </div>
  102.  
  103. <div class="col-md-4">
  104. <div class="row">
  105.  
  106. <div class="panel panel-default">
  107. <div class="panel-heading">
  108. <h4>Stories</h4>
  109. </div>
  110.  
  111. <div class="panel-body">
  112. <ul class="list-group" >
  113.  
  114.  
  115.  
  116.  
  117. <li class="list-group-item" ng-repeat="s in story.stories | reverse">
  118.  
  119. {{ s.content | date:"h:mma 'on' MMM d, y" }}
  120.  
  121.  
  122. <small>Posted by @ {{s.creator}}</small>
  123. <small class="pull-right">{{s.create | date:"h:mma 'on' MMM d, y"}}</small>
  124.  
  125.  
  126.  
  127. </li>
  128. </ul>
  129. </div>
  130. </div>
  131.  
  132. </div>
  133. </div>
  134.  
  135. <div class="col-md-4">
  136. </div>
  137.  
  138. </div>
Add Comment
Please, Sign In to add comment