var express = require('express');
var utils = require('../utils');
var Trends = require('../models/Trends');
var __ = global.__;
var router = express.Router();
router.get('/', (req, res) => {
  Trends.findAll({}, { raw: true })
    .then(function (types) {
      if (types) {
        res.json(utils.dataJsonObject(types));
      } else {
        res.send(404, utils.notFoundJsonObject());
      }
    })
    .catch(function (err) {
      res.send(500, utils.serverFailJsonObject(err.message));
    });
});

router.get('/:id', (req, res) => {
  Trends.findOne({ where: { id: req.params.id } }, { raw: true })
    .then(function (types) {
      if (types) {
        res.json(utils.dataJsonObject(types));
      } else {
        res.send(404, utils.notFoundJsonObject());
      }
    })
    .catch(function (err) {
      res.send(500, utils.serverFailJsonObject(err.message));
    });
});

router.post('/', (req, res) => {
  let title = req.body.title;
  if (title) {
    Trends.create(req.body)
      .then(function () {
        return res.json(utils.customJsonObject(__('Create trend type success'), null, 201));
      }).catch(function (error) {
        utils.errorSelector(error, res);
      });
  } else {
    return res.json(utils.postDataNotCorrectJsonObject(__('Trends title can not be empty')));
  }
});

router.put('/:id', (req, res) => {
  Trends.update(req.body, { where: { id: req.params.id } })
    .then((data) => {
      if (data) {
        res.status(200).json(utils.dataJsonObject(__('Update success')));
      } else {
        res.status(404).json(utils.notFoundJsonObject());
      }
    })
    .catch(() => {
      res.status(500).json(utils.serverFailJsonObject());
    });
});

router.delete('/:id', (req, res) => {
  Trends.destroy({ where: { id: req.params.id } })
    .then((data) => {
      if (data) {
        res.status(200).json(utils.dataJsonObject(__('Delete success')));
      } else {
        res.status(404).json(utils.notFoundJsonObject());
      }
    })
    .catch(err => {
      res.status(500).json(utils.serverFailJsonObject(err.name));
    });
});
module.exports = router;
@@ -1,11 +1,13 @@
-var
+const
  express
@@ -29,19 +29,21 @@
 ress');%0A
-var
+const
  utils =
@@ -64,19 +64,21 @@
 tils');%0A
-var
+const
  Trends 
@@ -108,19 +108,21 @@
 ends');%0A
-var
+const
  __ = gl
@@ -134,11 +134,13 @@
 __;%0A
-var
+const
  rou
@@ -201,28 +201,271 @@
 %7B%0A  
-Trends.findAll(%7B%7D, %7B
+const page = req.query.page %7C%7C 1;%0A  const type_id = req.query.type_id%7C%7C0;%0A  Trends.findAll(%7B%0A    where:%7B%0A      type_id: type_id === 0? %7B$gt: 0%7D:type_id ,%0A    %7D,%0A    order: %5B%0A      %5B'createdAt', 'DESC'%5D%0A    %5D,%0A    limit: 10,%0A    offset: (page - 1) * 10,%0A  %7D, %7B%0A   
  raw
@@ -462,32 +462,34 @@
  %7B%0A    raw: true
+%0A 
  %7D)%0A    .then(fu

add pagination and sort order for api trends