Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to add posts #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions models/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var PostSchema = Schema({
title : String,
body : String
});

var Post = mongoose.model('Post', PostSchema);

module.exports = Post;
1 change: 1 addition & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var UserSchema = new Schema({
, password : { type: String, select: false }
, first : { type: String, trim: true }
, last : { type: String, trim: true }
, posts : [{ type: Schema.Types.ObjectId, ref: 'Post' }]
})

UserSchema.virtual('fullname').get(function() {
Expand Down
15 changes: 13 additions & 2 deletions public/js/controllers/UsersCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@

angular.module('basic-auth')
.controller('ProfileCtrl', ['$scope', '$http', '$auth', 'Auth', function($scope, $http, $auth, Auth) {
$http.get('/api/me').then(function(data) {
$scope.user = data.data;
$http.get('/api/me').success(function(data) {
$scope.user = data;
});

$scope.createPost = function() {
$http.post('/api/posts', $scope.post)
.success(function(response) {
$scope.user.posts.unshift(response);
$scope.post = {};
})
.error(function(response) {
console.log(response)
})
}
}]);
16 changes: 16 additions & 0 deletions resources/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var Post = require('../models/post.js')
, User = require('../models/user.js')
, auth = require('./auth')

module.exports = function(app) {
app.post('/api/posts', auth.ensureAuthenticated, function (req,res) {
User.findById(req.userId).exec(function(err, user) {
var post = new Post(req.body);
post.save(function(err, post) {
user.posts.unshift(post._id);
user.save();
res.send(post);
});
})
})
}
9 changes: 6 additions & 3 deletions resources/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ var User = require('../models/user.js')
module.exports = function(app) {

app.get('/api/me', auth.ensureAuthenticated, function(req, res) {
User.findById(req.userId, function(err, user) {
res.send(user);
});
User.findOne({ _id: req.userId })
.populate('posts')
.exec(function(err, user) {
console.log(user)
res.send(user);
});
});

app.put('/api/me', auth.ensureAuthenticated, function(req, res) {
Expand Down
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ app.set('view engine', 'html');
app.get('/', resources.index);
app.get('/templates/:name', resources.templates);
require('./resources/users')(app);
require('./resources/posts')(app);

// redirect all others to the index (HTML5 history)
app.get('*', resources.index);
Expand Down
16 changes: 15 additions & 1 deletion views/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@ <h4>{{user.first}} {{user.last}}</h4>
</div>
<div class="col-sm-9">
<div class="page-header">
<h4>Something</h4>
<h4>Posts</h4>
</div>
<form ng-submit="createPost()">
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" ng-model="post.title">
</div>
<div class="form-group">
<textarea class="form-control" ng-model="post.body"></textarea>
</div>
<button type="submit">Save</button>
</form>
<div ng-repeat="post in user.posts">
<h3>{{post.title}}</h3>
<p>{{post.body}}</p>
</div>
</div>
</div>
Expand Down