<commit_before>var express = require('express')
var router = express.Router()
var User = require('../models/User')
var Category = require('../models/Category')
var Content = require('../models/Content')

let responseData = {}

router.use((req, res, next) => {
  responseData = {
    code: 0,
    message: '成功'
  }
  next()
})

router.use((req, res, next) => {
  if (!req.session.user || !req.session.user.isAdmin) {
    res.statusCode = 403
    responseData.code = 1
    responseData.message = '对不起，只有管理员才可以进入后台管理'
    res.json(responseData)
    return
  }
  next()
})

router.param('user_id', function (req, res, next, id) {
  req.user = {
    id
  }
  next()
})

router.get('/users', (req, res, next) => {
  let page = Number(req.query.page) || 1
  const limit = Number(req.query.limit) || 10
  let pages = 0

  User.estimatedDocumentCount().then(total => {
    pages = Math.ceil(total / limit)
    page = Math.min(page, pages)
    page = Math.max(page, 1)
    var skip = (page - 1) * limit

    User.find({}, { password: 0 }).lean().limit(limit).skip(skip).then(users => {
      res.json({ ...responseData, list: users, total, page, limit })
    })
  })
})

router.delete('/users/:user_id', (req, res, next) => {
  var id = req.user.id || ''

  User.remove({ _id: id }).then(() => {
    res.json(responseData)
  })
})

router.get('/category', (req, res) => {
  var page = Number(req.query.page) || 1
  var limit = 10
  var pages = 0

  Category.count().then(count => {
    pages = Math.ceil(count / limit)
    page = Math.min(page, pages)
    page = Math.max(page, 1)
    var skip = (page - 1) * limit

    Category.find().sort({_id: -1}).limit(limit).skip(skip).then(categories => {
      res.render('admin/category_index', {
        userInfo: req.userInfo,
        categories: categories,
        page: page,
        count: count,
        limit: limit,
        pages: pages
      })
    })
  })
})

router.get('/category/add', (req, res) => {<commit_msg>feat: get '/categories'<commit_after>var express = require('express')
var router = express.Router()
var User = require('../models/User')
var Category = require('../models/Category')
var Content = require('../models/Content')

const calcPage = (page = 1, total, limit) => {
  const pages = Math.ceil(total / limit) || 0
  page = Math.min(page, pages)
  page = Math.max(page, 1)
  return page
}

const calcSkip = (page, limit) => (page - 1) * limit

let responseData = {}

router.use((req, res, next) => {
  responseData = {
    code: 0,
    message: '成功'
  }
  next()
})

router.use((req, res, next) => {
  if (!req.session.user || !req.session.user.isAdmin) {
    res.statusCode = 403
    responseData.code = 1
    responseData.message = '对不起，只有管理员才可以进入后台管理'
    res.json(responseData)
    return
  }
  next()
})

router.param('user_id', function (req, res, next, id) {
  req.user = {
    id
  }
  next()
})

router.get('/users', (req, res, next) => {
  let page = Number(req.query.page) || 1
  const limit = Number(req.query.limit) || 10

  User.estimatedDocumentCount().then(total => {
    page = calcPage(page, total, limit)
    const skip = calcSkip(page, limit)

    User.find({}, { password: 0 }).lean().limit(limit).skip(skip).then(users => {
      res.json({ ...responseData, list: users, total, page, limit })
    })
  })
})

router.delete('/users/:user_id', (req, res, next) => {
  var id = req.user.id || ''

  User.remove({ _id: id }).then(() => {
    res.json(responseData)
  })
})

router.get('/categories', (req, res) => {
  let page = Number(req.query.page) || 1
  const limit = Number(req.query.limit) || 10

  Category.estimatedDocumentCount().then(total => {
    page = calcPage(page, total, limit)
    const skip = calcSkip(page, limit)

    Category.find().lean().sort({_id: -1}).limit(limit).skip(skip).then(categories => {
      res.json({ ...responseData, list: categories, total, page, limit })
    })
  })
})

router.get('/category/add', (req, res) => {
{'fraction_non_alphanumeric': 0.1924834941594718, 'fraction_numerical': 0.009141696292534281, 'mean_word_length': 3.0276073619631902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '2841109', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1388, 'n_words': 303}