var schema = require('./schema.js');

var exports = module.exports = {};

exports.articlesFor = (topic) => {
  return schema.models.article.findAll({
    where: {
      'Topic.id': topic
    },
    group: ['source.id']
  }).then((articles) => {
    return articles;
  })
};

exports.topics = () => {
  return schema.models.topic.all().then((topics) => { return topics; });
}

exports.articleBias = (articleId) => {
  var totalSum;
  return schema.models.article.findById(articleId).then((article) => {
    if (!article) return 0;
    schema.models.sentence.sum('bias',
      { where: { 'articleId': articleId }}
    ).then((sum) => {
      totalSum = sum;
      schema.models.sentence.count(
        { where: { 'articleId': articleId }}
      ).then((count) => {
        article.bias = count == 0 ? 0 : totalSum / count;
        return article;
      });
    });
  });
}
@@ -167,15 +167,14 @@
    '
-T
+t
 opic
-.i
+I
 d': 
@@ -209,10 +209,9 @@
 urce
-.i
+I
 d'%5D%0A

fix more join logic on articlesFor