Permalink
Browse files

Enforce post URL uniqueness

chapter7-7
  • Loading branch information...
tmeasday committed Oct 15, 2014
1 parent 2357173 commit c92904bed9083294839ad8927fb7b90b7794365f
Showing with 13 additions and 1 deletion.
  1. +4 −0 client/templates/posts/post_submit.js
  2. +9 −1 lib/collections/posts.js
@@ -12,6 +12,10 @@ Template.postSubmit.events({
if (error)
return alert(error.reason);
// show this result but route anyway
if (result.postExists)
alert('This link has already been posted');
Router.go('postPage', {_id: result._id});
});
}
View
@@ -2,12 +2,20 @@ Posts = new Mongo.Collection('posts');
Meteor.methods({
postInsert: function(postAttributes) {
check(Meteor.userId(), String);
check(this.userId, String);
check(postAttributes, {
title: String,
url: String
});
var postWithSameLink = Posts.findOne({url: postAttributes.url});
if (postWithSameLink) {
return {
postExists: true,
_id: postWithSameLink._id
}
}
var user = Meteor.user();
var post = _.extend(postAttributes, {
userId: user._id,

0 comments on commit c92904b

Please sign in to comment.