const keystone = require('keystone');
const Post = keystone.list('Post');
const PostCategory = keystone.list('PostCategory');

exports = module.exports = function (req, res) {
  const loadCategories = () => {
    return PostCategory.model.find().exec();
  };

  const loadPosts = () => {
    return Post.model.find().exec();
  };

  const parseCategory = (category) => ({
    key: category.key,
    name: category.name,
    id: category.id
  });

  const parseCategories = (categories) => {
    return categories.map((cat) => parseCategory(cat))
  };

  const loadData = () => {
    var loadedPosts = [];
    loadPosts().then((posts) => {
      loadedPosts = posts;
      return loadCategories()
    }).then((categories) => {
      return res.apiResponse({
        success: true,
        posts: loadedPosts,
        categories: parseCategories(categories)
      });
    }).catch((error) => {
      console.log("Calling error with: ", error);
      return res.state(500).send({ error: `An error occured: ${error.message}` });
    });
  }

  loadData();
};

@@ -123,57 +123,209 @@
 ');%0A
-%0Aexports = module.exports = function (req, res) %7B
+const PostTag = keystone.list('PostTag');%0Aconst User = keystone.list('User');%0A%0Aexports = module.exports = function (req, res) %7B%0A%0A  const loadTags = () =%3E %7B%0A    return PostTag.model.find().exec();%0A  %7D;%0A
 %0A  c
@@ -493,28 +493,20 @@
 arse
-Category = (category
+Item = (item
 ) =%3E
@@ -518,24 +518,20 @@
    key: 
-category
+item
 .key,%0A  
@@ -538,24 +538,20 @@
   name: 
-category
+item
 .name,%0A 
@@ -561,16 +561,12 @@
 id: 
-category
+item
 .id%0A
@@ -578,42 +578,88 @@
 %0A%0A  
-const parseCategories = (categorie
+/* Return an item parsed (either category or a tag)*/%0A  const parseItems = (item
 s) =
@@ -677,27 +677,23 @@
 urn 
-categorie
+item
 s.map((
-cat
+item
 ) =%3E
@@ -702,20 +702,17 @@
 arse
-Category(cat
+Item(item
 ))%0A 
@@ -769,16 +769,47 @@
 s = %5B%5D;%0A
+    var loadedCategories = %5B%5D;%0A
     load
@@ -917,24 +917,165 @@
 ories) =%3E %7B%0A
+      loadedCategories = parseItems(categories);%0A      return loadTags();%0A    %7D).then((tags) =%3E %7B%0A      const loadedTags = parseItems(tags);%0A
       return
@@ -1156,37 +1156,53 @@
     
-categories: parseCategories(c
+tags: loadedTags,%0A        categories: loadedC
 ateg
@@ -1206,17 +1206,16 @@
 tegories
-)
 %0A      %7D

add method for loading tags to get all for posts