Bläddra i källkod

Step 4: Websites should be listed with the most up voted site first.

- Ordering is: most upvotes, then least downvotes, then title ascending.
Frederic G. MARAND 9 år sedan
förälder
incheckning
0cb4c7676f
3 ändrade filer med 39 tillägg och 21 borttagningar
  1. 32 18
      client/website_item.js
  2. 3 1
      client/website_list.js
  3. 4 2
      server/collections.js

+ 32 - 18
client/website_item.js

@@ -14,15 +14,22 @@ Template.website_item.events({
     console.log("Up voting website with id " + websiteId);
     // Put the code in here to add a vote to a website!
     const userId = Meteor.userId();
-    const modifiers = {
-      $addToSet: {
-        plus: userId
-      },
-      $pull: {
-        minus: userId
-      }
-    };
-    Websites.update({ _id: websiteId }, modifiers);
+    let modifiers = {};
+    let increments = {}
+    if (!_.contains(this.plus, userId)) {
+      modifiers.$addToSet = { plus: userId };
+      increments.plusScore = 1;
+    }
+    if (_.contains(this.minus, userId)) {
+      modifiers.$pull = { minus: userId };
+      increments.minusScore = -1;
+    }
+    if (!_.isEmpty(increments)) {
+      modifiers.$inc = increments;
+    }
+    if (!_.isEmpty(modifiers)) {
+      Websites.update({ _id: websiteId }, modifiers);
+    }
 
     // Prevent the button from reloading the page.
     return false;
@@ -36,15 +43,22 @@ Template.website_item.events({
 
     // Put the code in here to remove a vote from a website!
     const userId = Meteor.userId();
-    const modifiers = {
-      $addToSet: {
-        minus: userId
-      },
-      $pull: {
-        plus: userId
-      }
-    };
-    Websites.update({ _id: websiteId }, modifiers);
+    let modifiers = {};
+    let increments = {}
+    if (!_.contains(this.minus, userId)) {
+      modifiers.$addToSet = { minus: userId };
+      increments.minusScore = 1;
+    }
+    if (_.contains(this.plus, userId)) {
+      modifiers.$pull = { plus: userId };
+      increments.plusScore = -1;
+    }
+    if (!_.isEmpty(increments)) {
+      modifiers.$inc = increments;
+    }
+    if (!_.isEmpty(modifiers)) {
+      Websites.update({ _id: websiteId }, modifiers);
+    }
 
     // Prevent the button from reloading the page
     return false;

+ 3 - 1
client/website_list.js

@@ -5,6 +5,8 @@
 // Helper function that returns all available websites
 Template.website_list.helpers({
   websites: () => {
-    return Websites.find({});
+    return Websites.find({}, {
+      sort: { plusScore: -1, minusScore: 1, title: 1 }
+    });
   }
 });

+ 4 - 2
server/collections.js

@@ -64,8 +64,10 @@ Websites.allow({
       throw new Meteor.Error("logged-out", "User must be logged to vote on a site.");
     }
     const orderedFields = fields.sort();
-    if (!_.isEqual(orderedFields, ["minus", "plus"])) {
-      throw new Meteor.Error("invalid-field", "May only update minus and plus.");
+    if (!_.isEqual(orderedFields, ["minus", "minusScore"]) &&
+      !_.isEqual(orderedFields, ["plus", "plusScore"]) &&
+      !_.isEqual(orderedFields, ["minus", "minusScore", "plus", "plusScore"])) {
+      throw new Meteor.Error("invalid-field", "May only update minus[Score] and plus[Score].");
     }
 
     // FIXME : check modifier.