<commit_before>import express from 'express';
import moment from 'moment';

const router = express.Router();

router.get('/', function(req, res){
    res.render('@blog/post/index');
});

router.get('/data', function(req, res){

  req.app.model('@blog/post').getAll().then(function(posts){

      var content = {
          recordsTotal: posts.length,
          recordsFiltered: posts.length,
          data: posts
      }

      res.json(content);

  });
});

router.post('/data', function(req, res){

  if(req.body.delete) {
    let pM = req.app.model('@blog/post');
    pM.delete(req.body.posts).then(function() {
      res.json([]);
    });
  }

});

function _getChilds(parentId, categories) {
  let childs = [];
  for(let i=0; i<categories.length; i++) {
    if (categories[i].parent_id == parentId) {
      childs.push(categories[i]);
    }
  }

  if(childs.length > 0) {
    for(let i=0; i<childs.length; i++) {
      childs[i].childrens = _getChilds(childs[i].id, categories);
    }
  }

  return childs;
}

router.get('/edit/:id', function(req, res){

  let cM = req.app.model('@blog/category');
  let tM = req.app.model('@blog/tag');

  cM.getAll().then(function(categories){

    tM.getAll().then(function(tags) {

      let categoryTree = _getChilds(0, categories);
      let tagOptionArray = [];
      for(let i=0;i<tags.length;i++) {
        tagOptionArray.push('"'+tags[i].label+'"');
      }

      let viewData = {
        tag_options: '['+tagOptionArray.join(',')+']',
        category_options: categoryTree
      }

      if(req.params.id !== 'new') {
        let pM = req.app.model('@blog/post');
        pM.getOneById(req.params.id).then(function(p){

          let tagArray = [];
          for(let i=0;i<p.tags.length;i++) {
            tagArray.push('"'+p.tags[i].label+'"');
          }
          viewData.post= p;
          viewData.tags= '['+tagArray.join(',')+']';

          res.render('@blog/post/edit', viewData);
        })

      } else {

        let post = {
          id: req.params.id,
          title: '',
          slug: '',
          content: '',
          published_at: moment().format('YYYY-MM-D hh:mm:ss'),
          categoryIds: [],
        }

        viewData.post= post;
        viewData.tags= '[]';

        res.render('@blog/post/edit', viewData);
      }

    });
  })
});

router.post('/save', function(req, res){

  let postData = req.body.post;
  let pM = req.app.model('@blog/post');
  let tM = req.app.model('@blog/tag');

  req.checkBody('post[title]', 'Title should not be empty').notEmpty();
  req.checkBody('post[slug]', 'Slug should not be empty').notEmpty();

  var errors = req.validationErrors();
  if(errors) {
      var responseBody = {
        errorType: 'validation',
        errors: errors
      }
      res.json(responseBody, 400);
      return;
  }

  if(postData.id == 'new') {

    let insertData = {
      title: postData.title,
      content: postData.content,
      slug: postData.slug,
      published_at: postData.published_at,
      created_at: moment().format('YYYY-MM-D hh:mm:ss'),
      updated_at: moment().format('YYYY-MM-D hh:mm:ss'),
      author_id: req.user.id
    }

    pM.insert(insertData).then(function(a){
      pM.setCategories(a[0], postData.categories).then(function(){
        // handle post tags
        tM.insertIfNotExists(postData.tags).then(function(tagIds) {
          pM.setTags(a[0], tagIds).then(function(){

            let response = {
              id: a[0],
              message: "Post saved",
              status: "success",
            }

            res.json(response);
          })

        });
      });
    })

  } else {
    let updateData = {
      title: postData.title,
      content: postData.content,
      slug: postData.slug,
      published_at: postData.published_at,
      updated_at: moment().format('YYYY-MM-D hh:mm:ss'),
    }

    pM.update(postData.id, updateData, function(err){
      pM.setCategories(postData.id, postData.categories).then(function(){

        tM.insertIfNotExists(postData.tags).then(function(tagIds) {
          pM.setTags(postData.id, tagIds).then(function(){

            let response = {
              id: postData.id,
              message: "Post Updated",
              status: "success",
            }

            res.json(response);
          })

        });

      })
    })
  }

});

module.exports = router;
<commit_msg>fixes undefined post data<commit_after>import express from 'express';
import moment from 'moment';

const router = express.Router();

router.get('/', function(req, res){
    res.render('@blog/post/index');
});

router.get('/data', function(req, res){

  req.app.model('@blog/post').getAll().then(function(posts){

      var content = {
          recordsTotal: posts.length,
          recordsFiltered: posts.length,
          data: posts
      }

      res.json(content);

  });
});

router.post('/data', function(req, res){

  if(req.body.delete) {
    let pM = req.app.model('@blog/post');
    pM.delete(req.body.posts).then(function() {
      res.json([]);
    });
  }

});

function _getChilds(parentId, categories) {
  let childs = [];
  for(let i=0; i<categories.length; i++) {
    if (categories[i].parent_id == parentId) {
      childs.push(categories[i]);
    }
  }

  if(childs.length > 0) {
    for(let i=0; i<childs.length; i++) {
      childs[i].childrens = _getChilds(childs[i].id, categories);
    }
  }

  return childs;
}

router.get('/edit/:id', function(req, res){

  let cM = req.app.model('@blog/category');
  let tM = req.app.model('@blog/tag');

  cM.getAll().then(function(categories){

    tM.getAll().then(function(tags) {

      let categoryTree = _getChilds(0, categories);
      let tagOptionArray = [];
      for(let i=0;i<tags.length;i++) {
        tagOptionArray.push('"'+tags[i].label+'"');
      }

      let viewData = {
        tag_options: '['+tagOptionArray.join(',')+']',
        category_options: categoryTree
      }

      if(req.params.id !== 'new') {
        let pM = req.app.model('@blog/post');
        pM.getOneById(req.params.id).then(function(p){

          let tagArray = [];
          for(let i=0;i<p.tags.length;i++) {
            tagArray.push('"'+p.tags[i].label+'"');
          }
          viewData.post= p;
          viewData.tags= '['+tagArray.join(',')+']';

          res.render('@blog/post/edit', viewData);
        })

      } else {

        let post = {
          id: req.params.id,
          title: '',
          slug: '',
          content: '',
          published_at: moment().format('YYYY-MM-D hh:mm:ss'),
          categoryIds: [],
        }

        viewData.post= post;
        viewData.tags= '[]';

        res.render('@blog/post/edit', viewData);
      }

    });
  })
});

router.post('/save', function(req, res){

  let postData = req.body.post;
  let pM = req.app.model('@blog/post');
  let tM = req.app.model('@blog/tag');

  req.checkBody('post[title]', 'Title should not be empty').notEmpty();
  req.checkBody('post[slug]', 'Slug should not be empty').notEmpty();

  var errors = req.validationErrors();
  if(errors) {
      var responseBody = {
        errorType: 'validation',
        errors: errors
      }
      res.json(responseBody, 400);
      return;
  }

  let postDataCat = postData.categories || [];
  let postDataTags = postData.tags || [];

  if(postData.id == 'new') {

    let insertData = {
      title: postData.title,
      content: postData.content,
      slug: postData.slug,
      published_at: postData.published_at,
      created_at: moment().format('YYYY-MM-D hh:mm:ss'),
      updated_at: moment().format('YYYY-MM-D hh:mm:ss'),
      author_id: req.user.id
    }

    pM.insert(insertData).then(function(a){
      pM.setCategories(a[0], postDataCat).then(function(){
        // handle post tags
        tM.insertIfNotExists(postDataTags).then(function(tagIds) {
          pM.setTags(a[0], tagIds).then(function(){

            let response = {
              id: a[0],
              message: "Post saved",
              status: "success",
            }

            res.json(response);
          })

        });
      });
    })

  } else {
    let updateData = {
      title: postData.title,
      content: postData.content,
      slug: postData.slug,
      published_at: postData.published_at,
      updated_at: moment().format('YYYY-MM-D hh:mm:ss'),
    }

    pM.update(postData.id, updateData, function(err){
      pM.setCategories(postData.id, postDataCat).then(function(){

        tM.insertIfNotExists(postDataTags).then(function(tagIds) {
          pM.setTags(postData.id, tagIds).then(function(){

            let response = {
              id: postData.id,
              message: "Post Updated",
              status: "success",
            }

            res.json(response);
          })

        });

      })
    })
  }

});

module.exports = router;

{'fraction_non_alphanumeric': 0.17472875226039783, 'fraction_numerical': 0.0027124773960217, 'mean_word_length': 2.237833882180754, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1761527', 'n_tokens_mistral': 3016, 'n_tokens_neox': 2938, 'n_words': 471}