{"action":"created","comment":{"url":"https://api.github.com/repos/jerbobbo/beerio/pulls/comments/61439625","id":61439625,"diff_hunk":"@@ -18,47 +17,39 @@ router.get('/', function(req, res, next) {\n });\n \n router.post('/', function(req, res, next) {\n-  var cart, newCartItem;\n-  // need to send back quantity\n   if (req.user) {\n-    Cart.findOne( {user: req.user._id} ).populate( { path: 'cartItems'})\n-    .then(function(_cart) {\n-      if (!_cart) return Cart.create( {user: req.user._id} );\n-      return _cart;\n-    })\n-    .then(function(_cart) {\n-      cart = _cart;\n-      var exists = false,\n-          index;\n-\n-      cart.cartItems.forEach(function(cartItem, idx){\n-        if (cartItem.productId.toString() === req.body._id.toString()) {\n-          exists = true;\n-          index  = idx;\n-          return;\n+    Cart.findOne({ user: req.user._id })\n+      .then(function(cart) {\n+        if (!cart) {\n+          return Cart.create( {user: req.user._id });\n         }\n-      });\n-\n-      if (exists) {\n-        // if it exists then we increment\n-        cart.cartItems[index].quantity++;\n-      } else {\n-        // create a new cart item\n-        newCartItem = new CartItem({\n-          productId: req.body._id,\n-          quantity: req.body.quantity\n-        });\n-        cart.cartItems.push(newCartItem);\n-      }\n-      return cart.populate('lineItems').save();\n-    })\n-    .then(function(_cart) {\n-      console.log('NEW CART', _cart)\n-      res.json(_cart);\n-    })\n-    .catch(res.json);\n-  } else {\n-    res.sendStatus(401);\n+        return cart;\n+      })\n+      .then(function(cart) {\n+        var found;\n+        cart.items.forEach(function(item) {\n+          if (req.body._id == item.productId._id) {\n+            found = true;\n+            CartItem.findByIdAndUpdate(item._id, { $inc: { quantity: 1 }})","path":"server/app/routes/cart/index.js","position":68,"original_position":68,"commit_id":"d2f8a8a2b422036685f9eac7645d91adcfad591b","original_commit_id":"d2f8a8a2b422036685f9eac7645d91adcfad591b","user":{"login":"antkim003","id":4511875,"avatar_url":"https://avatars.githubusercontent.com/u/4511875?v=3","gravatar_id":"","url":"https://api.github.com/users/antkim003","html_url":"https://github.com/antkim003","followers_url":"https://api.github.com/users/antkim003/followers","following_url":"https://api.github.com/users/antkim003/following{/other_user}","gists_url":"https://api.github.com/users/antkim003/gists{/gist_id}","starred_url":"https://api.github.com/users/antkim003/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/antkim003/subscriptions","organizations_url":"https://api.github.com/users/antkim003/orgs","repos_url":"https://api.github.com/users/antkim003/repos","events_url":"https://api.github.com/users/antkim003/events{/privacy}","received_events_url":"https://api.github.com/users/antkim003/received_events","type":"User","site_admin":false},"body":"$inc - does that increment it?\r\nniceeeee","created_at":"2016-04-28T14:46:59Z","updated_at":"2016-04-28T14:46:59Z","html_url":"https://github.com/jerbobbo/beerio/pull/74#discussion_r61439625","pull_request_url":"https://api.github.com/repos/jerbobbo/beerio/pulls/74","_links":{"self":{"href":"https://api.github.com/repos/jerbobbo/beerio/pulls/comments/61439625"},"html":{"href":"https://github.com/jerbobbo/beerio/pull/74#discussion_r61439625"},"pull_request":{"href":"https://api.github.com/repos/jerbobbo/beerio/pulls/74"}}},"pull_request":{"url":"https://api.github.com/repos/jerbobbo/beerio/pulls/74","id":68162433,"html_url":"https://github.com/jerbobbo/beerio/pull/74","diff_url":"https://github.com/jerbobbo/beerio/pull/74.diff","patch_url":"https://github.com/jerbobbo/beerio/pull/74.patch","issue_url":"https://api.github.com/repos/jerbobbo/beerio/issues/74","number":74,"state":"open","locked":false,"title":"bug fixes to cart post api and updates on order cart","user":{"login":"karn09","id":3743119,"avatar_url":"https://avatars.githubusercontent.com/u/3743119?v=3","gravatar_id":"","url":"https://api.github.com/users/karn09","html_url":"https://github.com/karn09","followers_url":"https://api.github.com/users/karn09/followers","following_url":"https://api.github.com/users/karn09/following{/other_user}","gists_url":"https://api.github.com/users/karn09/gists{/gist_id}","starred_url":"https://api.github.com/users/karn09/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/karn09/subscriptions","organizations_url":"https://api.github.com/users/karn09/orgs","repos_url":"https://api.github.com/users/karn09/repos","events_url":"https://api.github.com/users/karn09/events{/privacy}","received_events_url":"https://api.github.com/users/karn09/received_events","type":"User","site_admin":false},"body":"GET /api/cart was originally returning an array of ObjectIds. Due to the way that POST /api/cart was written \r\n\r\n`newCartItem = new CartItem({\r\nproductId: req.body._id,\t\t\r\nquantity: req.body.quantity\r\n});\t\t\r\ncart.cartItems.push(newCartItem);\r\n`\r\n\r\na record associated with CartItem was never created. With no record, the GET route was unable to populate with the needed product data. POST is also reliant on this in order to help with deduplication.\r\n\r\nI ended up shortening the code up a bit, and should hopefully be a bit more readable.\r\n\r\nAdditionally, I added 'mongoose-autopopulate', which allows for records to be auto-populated if `autopopulate: true`","created_at":"2016-04-28T04:30:41Z","updated_at":"2016-04-28T14:46:59Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8475e0f91bc8d3797972deef7fd91b37366e46cc","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/jerbobbo/beerio/pulls/74/commits","review_comments_url":"https://api.github.com/repos/jerbobbo/beerio/pulls/74/comments","review_comment_url":"https://api.github.com/repos/jerbobbo/beerio/pulls/comments{/number}","comments_url":"https://api.github.com/repos/jerbobbo/beerio/issues/74/comments","statuses_url":"https://api.github.com/repos/jerbobbo/beerio/statuses/d2f8a8a2b422036685f9eac7645d91adcfad591b","head":{"label":"jerbobbo:jn-cart-add","ref":"jn-cart-add","sha":"d2f8a8a2b422036685f9eac7645d91adcfad591b","user":{"login":"jerbobbo","id":13544595,"avatar_url":"https://avatars.githubusercontent.com/u/13544595?v=3","gravatar_id":"","url":"https://api.github.com/users/jerbobbo","html_url":"https://github.com/jerbobbo","followers_url":"https://api.github.com/users/jerbobbo/followers","following_url":"https://api.github.com/users/jerbobbo/following{/other_user}","gists_url":"https://api.github.com/users/jerbobbo/gists{/gist_id}","starred_url":"https://api.github.com/users/jerbobbo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerbobbo/subscriptions","organizations_url":"https://api.github.com/users/jerbobbo/orgs","repos_url":"https://api.github.com/users/jerbobbo/repos","events_url":"https://api.github.com/users/jerbobbo/events{/privacy}","received_events_url":"https://api.github.com/users/jerbobbo/received_events","type":"User","site_admin":false},"repo":{"id":56645547,"name":"beerio","full_name":"jerbobbo/beerio","owner":{"login":"jerbobbo","id":13544595,"avatar_url":"https://avatars.githubusercontent.com/u/13544595?v=3","gravatar_id":"","url":"https://api.github.com/users/jerbobbo","html_url":"https://github.com/jerbobbo","followers_url":"https://api.github.com/users/jerbobbo/followers","following_url":"https://api.github.com/users/jerbobbo/following{/other_user}","gists_url":"https://api.github.com/users/jerbobbo/gists{/gist_id}","starred_url":"https://api.github.com/users/jerbobbo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerbobbo/subscriptions","organizations_url":"https://api.github.com/users/jerbobbo/orgs","repos_url":"https://api.github.com/users/jerbobbo/repos","events_url":"https://api.github.com/users/jerbobbo/events{/privacy}","received_events_url":"https://api.github.com/users/jerbobbo/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jerbobbo/beerio","description":"","fork":false,"url":"https://api.github.com/repos/jerbobbo/beerio","forks_url":"https://api.github.com/repos/jerbobbo/beerio/forks","keys_url":"https://api.github.com/repos/jerbobbo/beerio/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jerbobbo/beerio/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jerbobbo/beerio/teams","hooks_url":"https://api.github.com/repos/jerbobbo/beerio/hooks","issue_events_url":"https://api.github.com/repos/jerbobbo/beerio/issues/events{/number}","events_url":"https://api.github.com/repos/jerbobbo/beerio/events","assignees_url":"https://api.github.com/repos/jerbobbo/beerio/assignees{/user}","branches_url":"https://api.github.com/repos/jerbobbo/beerio/branches{/branch}","tags_url":"https://api.github.com/repos/jerbobbo/beerio/tags","blobs_url":"https://api.github.com/repos/jerbobbo/beerio/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jerbobbo/beerio/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jerbobbo/beerio/git/refs{/sha}","trees_url":"https://api.github.com/repos/jerbobbo/beerio/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jerbobbo/beerio/statuses/{sha}","languages_url":"https://api.github.com/repos/jerbobbo/beerio/languages","stargazers_url":"https://api.github.com/repos/jerbobbo/beerio/stargazers","contributors_url":"https://api.github.com/repos/jerbobbo/beerio/contributors","subscribers_url":"https://api.github.com/repos/jerbobbo/beerio/subscribers","subscription_url":"https://api.github.com/repos/jerbobbo/beerio/subscription","commits_url":"https://api.github.com/repos/jerbobbo/beerio/commits{/sha}","git_commits_url":"https://api.github.com/repos/jerbobbo/beerio/git/commits{/sha}","comments_url":"https://api.github.com/repos/jerbobbo/beerio/comments{/number}","issue_comment_url":"https://api.github.com/repos/jerbobbo/beerio/issues/comments{/number}","contents_url":"https://api.github.com/repos/jerbobbo/beerio/contents/{+path}","compare_url":"https://api.github.com/repos/jerbobbo/beerio/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jerbobbo/beerio/merges","archive_url":"https://api.github.com/repos/jerbobbo/beerio/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jerbobbo/beerio/downloads","issues_url":"https://api.github.com/repos/jerbobbo/beerio/issues{/number}","pulls_url":"https://api.github.com/repos/jerbobbo/beerio/pulls{/number}","milestones_url":"https://api.github.com/repos/jerbobbo/beerio/milestones{/number}","notifications_url":"https://api.github.com/repos/jerbobbo/beerio/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jerbobbo/beerio/labels{/name}","releases_url":"https://api.github.com/repos/jerbobbo/beerio/releases{/id}","deployments_url":"https://api.github.com/repos/jerbobbo/beerio/deployments","created_at":"2016-04-20T01:35:00Z","updated_at":"2016-04-27T14:01:08Z","pushed_at":"2016-04-28T14:42:33Z","git_url":"git://github.com/jerbobbo/beerio.git","ssh_url":"git@github.com:jerbobbo/beerio.git","clone_url":"https://github.com/jerbobbo/beerio.git","svn_url":"https://github.com/jerbobbo/beerio","homepage":null,"size":194,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":31,"forks":1,"open_issues":31,"watchers":0,"default_branch":"master"}},"base":{"label":"jerbobbo:master","ref":"master","sha":"a07e6e8cb996ee9c18bf0777b8eba4e067b7cf6b","user":{"login":"jerbobbo","id":13544595,"avatar_url":"https://avatars.githubusercontent.com/u/13544595?v=3","gravatar_id":"","url":"https://api.github.com/users/jerbobbo","html_url":"https://github.com/jerbobbo","followers_url":"https://api.github.com/users/jerbobbo/followers","following_url":"https://api.github.com/users/jerbobbo/following{/other_user}","gists_url":"https://api.github.com/users/jerbobbo/gists{/gist_id}","starred_url":"https://api.github.com/users/jerbobbo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerbobbo/subscriptions","organizations_url":"https://api.github.com/users/jerbobbo/orgs","repos_url":"https://api.github.com/users/jerbobbo/repos","events_url":"https://api.github.com/users/jerbobbo/events{/privacy}","received_events_url":"https://api.github.com/users/jerbobbo/received_events","type":"User","site_admin":false},"repo":{"id":56645547,"name":"beerio","full_name":"jerbobbo/beerio","owner":{"login":"jerbobbo","id":13544595,"avatar_url":"https://avatars.githubusercontent.com/u/13544595?v=3","gravatar_id":"","url":"https://api.github.com/users/jerbobbo","html_url":"https://github.com/jerbobbo","followers_url":"https://api.github.com/users/jerbobbo/followers","following_url":"https://api.github.com/users/jerbobbo/following{/other_user}","gists_url":"https://api.github.com/users/jerbobbo/gists{/gist_id}","starred_url":"https://api.github.com/users/jerbobbo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerbobbo/subscriptions","organizations_url":"https://api.github.com/users/jerbobbo/orgs","repos_url":"https://api.github.com/users/jerbobbo/repos","events_url":"https://api.github.com/users/jerbobbo/events{/privacy}","received_events_url":"https://api.github.com/users/jerbobbo/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jerbobbo/beerio","description":"","fork":false,"url":"https://api.github.com/repos/jerbobbo/beerio","forks_url":"https://api.github.com/repos/jerbobbo/beerio/forks","keys_url":"https://api.github.com/repos/jerbobbo/beerio/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jerbobbo/beerio/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jerbobbo/beerio/teams","hooks_url":"https://api.github.com/repos/jerbobbo/beerio/hooks","issue_events_url":"https://api.github.com/repos/jerbobbo/beerio/issues/events{/number}","events_url":"https://api.github.com/repos/jerbobbo/beerio/events","assignees_url":"https://api.github.com/repos/jerbobbo/beerio/assignees{/user}","branches_url":"https://api.github.com/repos/jerbobbo/beerio/branches{/branch}","tags_url":"https://api.github.com/repos/jerbobbo/beerio/tags","blobs_url":"https://api.github.com/repos/jerbobbo/beerio/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jerbobbo/beerio/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jerbobbo/beerio/git/refs{/sha}","trees_url":"https://api.github.com/repos/jerbobbo/beerio/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jerbobbo/beerio/statuses/{sha}","languages_url":"https://api.github.com/repos/jerbobbo/beerio/languages","stargazers_url":"https://api.github.com/repos/jerbobbo/beerio/stargazers","contributors_url":"https://api.github.com/repos/jerbobbo/beerio/contributors","subscribers_url":"https://api.github.com/repos/jerbobbo/beerio/subscribers","subscription_url":"https://api.github.com/repos/jerbobbo/beerio/subscription","commits_url":"https://api.github.com/repos/jerbobbo/beerio/commits{/sha}","git_commits_url":"https://api.github.com/repos/jerbobbo/beerio/git/commits{/sha}","comments_url":"https://api.github.com/repos/jerbobbo/beerio/comments{/number}","issue_comment_url":"https://api.github.com/repos/jerbobbo/beerio/issues/comments{/number}","contents_url":"https://api.github.com/repos/jerbobbo/beerio/contents/{+path}","compare_url":"https://api.github.com/repos/jerbobbo/beerio/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jerbobbo/beerio/merges","archive_url":"https://api.github.com/repos/jerbobbo/beerio/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jerbobbo/beerio/downloads","issues_url":"https://api.github.com/repos/jerbobbo/beerio/issues{/number}","pulls_url":"https://api.github.com/repos/jerbobbo/beerio/pulls{/number}","milestones_url":"https://api.github.com/repos/jerbobbo/beerio/milestones{/number}","notifications_url":"https://api.github.com/repos/jerbobbo/beerio/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jerbobbo/beerio/labels{/name}","releases_url":"https://api.github.com/repos/jerbobbo/beerio/releases{/id}","deployments_url":"https://api.github.com/repos/jerbobbo/beerio/deployments","created_at":"2016-04-20T01:35:00Z","updated_at":"2016-04-27T14:01:08Z","pushed_at":"2016-04-28T14:42:33Z","git_url":"git://github.com/jerbobbo/beerio.git","ssh_url":"git@github.com:jerbobbo/beerio.git","clone_url":"https://github.com/jerbobbo/beerio.git","svn_url":"https://github.com/jerbobbo/beerio","homepage":null,"size":194,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":31,"forks":1,"open_issues":31,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/jerbobbo/beerio/pulls/74"},"html":{"href":"https://github.com/jerbobbo/beerio/pull/74"},"issue":{"href":"https://api.github.com/repos/jerbobbo/beerio/issues/74"},"comments":{"href":"https://api.github.com/repos/jerbobbo/beerio/issues/74/comments"},"review_comments":{"href":"https://api.github.com/repos/jerbobbo/beerio/pulls/74/comments"},"review_comment":{"href":"https://api.github.com/repos/jerbobbo/beerio/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/jerbobbo/beerio/pulls/74/commits"},"statuses":{"href":"https://api.github.com/repos/jerbobbo/beerio/statuses/d2f8a8a2b422036685f9eac7645d91adcfad591b"}}}}