var express = require('express');
var router = express.Router();
var db = require('../db');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Riot Challenge' });
});

/* GET data for all items, async */
router.get('/data', function(req, res, next) {
  var matchData511 = db.get('match_data');
  var matchData514 = db.get('match_data2');

  var promises = [matchData511.find({}), matchData514.find({})];

  Promise.all(promises).then(function(values){
    var data = values.map(function(docs){
      return buildItemData(docs);
    });
    res.json({ matchItemData511: data[0], matchItemData514: data[1] });
  });
});

/* GET data for a single item in fixed slot, async */
// NOT IMPLEMENTED
router.get('/singleItemData', function(req, res, next) {
  var itemName = req.query.item;
  var slotIndex = req.query.slot;

  if (!itemName || !slotIndex) {
    res.status(400).json({'error':'Bad request'});
  }
});

var buildItemData = function(docs){
  var items = [{}, {}, {}, {}, {}, {}];
  items.forEach(function(itemSlot, slotIndex){
    docs.forEach(function(doc){
      for (var player in doc) {
        if (doc[player].items && doc[player].items[slotIndex]) {
          var boughtItem = doc[player].items[slotIndex];
          if (itemSlot[boughtItem]) {
            itemSlot[boughtItem] += 1;
          } else {
            itemSlot[boughtItem] = 1;
          }
        }
      }
    });
  });
  return items;
};

module.exports = router;

@@ -319,26 +319,19 @@
 db.get('
-match_data
+511
 ');%0A  va
@@ -359,19 +359,11 @@
 et('
-match_data2
+514
 ');%0A
@@ -532,28 +532,29 @@
 return build
-Item
+Match
 Data(docs);%0A
@@ -702,27 +702,8 @@
  */%0A
-// NOT IMPLEMENTED%0A
 rout
@@ -915,244 +915,1392 @@
   %7D%0A
-%7D);%0A%0Avar buildItemData = function(docs)%7B%0A  var items = %5B%7B%7D, %7B%7D, %7B%7D, %7B%7D, %7B%7D, %7B%7D%5D;%0A  items.forEach(function(itemSlot, slotIndex)%7B%0A    docs.forEach(function(doc)%7B%0A      for (var player in doc) %7B%0A        if (doc%5Bplayer%5D.items && doc%5Bplayer%5D
+%0A  var itemData511 = db.get('511');%0A  var itemData514 = db.get('514');%0A%0A  var queryField = 'players.items.' + (parseInt(slotIndex)-1);%0A  var singleItemQuery = %7B%7D;%0A  singleItemQuery%5BqueryField%5D = itemName;%0A%0A  var promises = %5BitemData511.find(singleItemQuery), itemData514.find(singleItemQuery)%5D;%0A%0A  Promise.all(promises).then(function(values)%7B%0A    var data = values.map(function(docs)%7B%0A      return buildItemData(docs, req.query);%0A    %7D);%0A    res.json(%7B singleItemData511: data%5B0%5D, singleItemData514: data%5B1%5D %7D);%0A  %7D);%0A%0A%7D);%0A%0Avar buildMatchData = function(docs)%7B%0A  var items = %5B%7B%7D, %7B%7D, %7B%7D, %7B%7D, %7B%7D, %7B%7D%5D;%0A  items.forEach(function(itemSlot, slotIndex)%7B%0A    docs.forEach(function(doc)%7B%0A      doc.players.forEach(function(player) %7B%0A        if (player.items%5BslotIndex%5D) %7B%0A          var boughtItem = player.items%5BslotIndex%5D;%0A          if (itemSlot%5BboughtItem%5D) %7B%0A            itemSlot%5BboughtItem%5D += 1;%0A          %7D else %7B%0A            itemSlot%5BboughtItem%5D = 1;%0A          %7D%0A        %7D%0A      %7D);%0A    %7D);%0A  %7D);%0A  return items;%0A%7D;%0A%0Avar buildItemData = function(docs, query)%7B%0A  var items = %5B%7B%7D, %7B%7D, %7B%7D, %7B%7D, %7B%7D, %7B%7D%5D;%0A  items.forEach(function(itemSlot, slotIndex)%7B%0A    docs.forEach(function(doc)%7B%0A      var wantedPlayers = doc.players.filter(function(player) %7B%0A        return player.items%5BparseInt(query.slot)-1%5D === query.item%0A      %7D);%0A      wantedPlayers.forEach(function(player) %7B%0A        if (player
 .ite
@@ -2347,19 +2347,14 @@
 m = 
-doc%5B
 player
-%5D
 .ite
@@ -2527,24 +2527,26 @@
    %7D%0A      %7D
+);
 %0A    %7D);%0A  %7D

Add endpoint for single item data [#101943816]