<commit_before>'use strict';

import fs from 'fs';
import jwt from 'jsonwebtoken';
import jwtExpress from 'express-jwt';
import connectRoute from 'connect-route';
import multer from 'multer';

function router(api, hexo) {
  let Post = hexo.model('Post');
  let Tag = hexo.model('Tag');
  let Category = hexo.model('Category');

  api.post('/login', (req, res) => {
    let name = req.body.name;
    let password = <PASSWORD>;
    let user = hexo.config.admin;
    if (name === user.name && password === <PASSWORD>) {
      let token = jwt.sign(user.password, user.secret, {
        expiresInMinutes: user.expire
      });
      res.json({
        token
      });
    } else {
      res.status(400).send('Error name or password');
    }
  });

  api.get('/posts', (req, res) => {
    let posts = Post.sort('-date').toArray().map(post => {
      return {
        id: post._id,
        title: post.title,
        slug: post.slug,
        layout: post.layout,
        link: post.permalink,
        published: post.published,
        categories: post.categories.toArray().map(category => category.name),
        tags: post.tags.toArray().map(tag => tag.name),
        date: post.date
      };
    });
    res.json(posts);
  });

  api.get('/posts/:slug', (req, res) => {
    let slug = req.params.slug;
    let post = Post.findOne({
      slug
    });
    if (!post) {
      return res.status(404).send('Post not found');
    }
    post = {
      id: post._id,
      title: post.title,
      slug: post.slug,
      layout: post.layout,
      link: post.permalink,
      published: post.published,
      content: post._content,
      categories: post.categories.toArray().map(category => category.name),
      tags: post.tags.toArray().map(tag => tag.name),
      date: post.date,
      excerpt: post.excerpt
    };
    res.json(post);
  });

  api.post('/posts', (req, res) => {
    if (req.body.id) {
      let id = req.body.id;
      let post = Post.get(id);
      if (!post) {
        return res.status(404).send('Post not found');
      }
      fs.unlinkSync(hexo.source_dir + post.source);
    }
    let post = {
      title: req.body.title,
      slug: req.body.slug,
      content: req.body.content,
      layout: req.body.layout,
      date: req.body.date,
      categories: req.body.categories,
      tags: req.body.tags,
    };
    hexo.post.create(post).then(data => {
      let source = data.path.slice(hexo.source_dir.length);
      hexo.source.process(source).then(() => {
        if (require('os').platform() == 'win32') source = source.replace('\\', '/');
        let post = Post.findOne({
          source: source<commit_msg>#13 fixed to query by slug<commit_after>'use strict';

import fs from 'fs';
import jwt from 'jsonwebtoken';
import jwtExpress from 'express-jwt';
import connectRoute from 'connect-route';
import multer from 'multer';

function router(api, hexo) {
  let Post = hexo.model('Post');
  let Tag = hexo.model('Tag');
  let Category = hexo.model('Category');

  api.post('/login', (req, res) => {
    let name = req.body.name;
    let password = <PASSWORD>;
    let user = hexo.config.admin;
    if (name === user.name && password === <PASSWORD>.password) {
      let token = jwt.sign(user.password, user.secret, {
        expiresInMinutes: user.expire
      });
      res.json({
        token
      });
    } else {
      res.status(400).send('Error name or password');
    }
  });

  api.get('/posts', (req, res) => {
    let posts = Post.sort('-date').toArray().map(post => {
      return {
        id: post._id,
        title: post.title,
        slug: post.slug,
        layout: post.layout,
        link: post.permalink,
        published: post.published,
        categories: post.categories.toArray().map(category => category.name),
        tags: post.tags.toArray().map(tag => tag.name),
        date: post.date
      };
    });
    res.json(posts);
  });

  api.get('/posts/:slug', (req, res) => {
    let slug = req.params.slug;
    let post = Post.findOne({
      slug
    });
    if (!post) {
      return res.status(404).send('Post not found');
    }
    post = {
      id: post._id,
      title: post.title,
      slug: post.slug,
      layout: post.layout,
      link: post.permalink,
      published: post.published,
      content: post._content,
      categories: post.categories.toArray().map(category => category.name),
      tags: post.tags.toArray().map(tag => tag.name),
      date: post.date,
      excerpt: post.excerpt
    };
    res.json(post);
  });

  api.post('/posts', (req, res) => {
    if (req.body.id) {
      let id = req.body.id;
      let post = Post.get(id);
      if (!post) {
        return res.status(404).send('Post not found');
      }
      fs.unlinkSync(hexo.source_dir + post.source);
    }
    let post = {
      title: req.body.title,
      slug: req.body.slug,
      content: req.body.content,
      layout: req.body.layout,
      date: req.body.date,
      categories: req.body.categories,
      tags: req.body.tags,
    };
    hexo.post.create(post).then(data => {
      let source = data.path.slice(hexo.source_dir.length);
      hexo.source.process(source).then(() => {
        let post = Post.findOne({
          slug: req.body.slug
{'fraction_non_alphanumeric': 0.1656441717791411, 'fraction_numerical': 0.004217791411042945, 'mean_word_length': 2.293560606060606, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, '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': '1249949', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1731, 'n_words': 367}