<commit_before>'use strict';
/**
 * 文章相关
 */

const model = require('../model');
const url = require('url');
const utils = require('../utils');
let Article = model.Article;
let ArticleSort = model.ArticleSort;
let Sort = model.Sort;

/**
 * 直接 url.parse(ctx.request.url, true).query;
 * 结果是 { page: '1', size: '10', name: '顺' }
 * 1.支持中文
 * 2.由于是通过url传递参数，因此参数全是字符。使用时需要强转
 * @param ctx
 * @param next
 */
var fn_article_list_get = async(ctx, next) => {
    let params = url.parse(ctx.request.url, true).query;
    let currentPage = params.page * 1 || 1;
    if (currentPage <= 0) {
        currentPage = 1;
    }
    let pageSize = params.size * 1 || 10;
    if (pageSize <= 0) {
        pageSize = 10;
    }
    let type = params.type || '0';
    let articleList;
    /**
     * 多取出一个，方便判断是否还有更多。
     */
    if (type === '0') {
        articleList = await Article.findAll({
            limit: pageSize + 1,
            offset: (currentPage - 1) * pageSize,
            order: [['updatedAt', 'DESC']],
            attributes: ['id', 'title', 'des', 'userId', 'updatedAt'],
            where: {
                id: {$ne: 0}
            }
        });
    } else {
        articleList = await ArticleSort.findAll({
            limit: pageSize + 1,
            offset: (currentPage - 1) * pageSize,
            where: {
                sortId: type,
                id: {$ne: 0}
            },
            attributes: ['sortId'],
            include: {
                model: Article,
                order: [['updatedAt', 'DESC']],
                attributes: ['id', 'title', 'des', 'userId', 'updatedAt']
            }
        });
        let articleListTemp = [];
        for (let i = 0; i < articleList.length; i++) {
            let temp = {};
            temp.id = articleList[i].article.id;
            temp.title = articleList[i].article.title;
            temp.des = articleList[i].article.des;
            temp.userId = articleList[i].article.userId;
            articleListTemp[i] = temp;
        }
        articleList = articleListTemp;
    }
    if (articleList.length < pageSize + 1) {
        ctx.rest(articleList, false);
    } else {
        ctx.rest(articleList.slice(0, pageSize), true);
    }
};

var fn_article_get = async(ctx, next) => {
    let params = url.parse(ctx.request.url, true).query;
    var article = await Article.findOne({
        where: {
            id: params.id || 0
        }
    });
    console.log(params.id);
    ctx.rest(article);
};

var fn_article_post = async(ctx, next) => {
    let article = {};
    article.title = ctx.request.body.title || '';
    article.des = ctx.request.body.des || '';
    article.content = ctx.request.body.content || '';
    article.userId = ctx.request.body.userId || 1;
    var sorts = ctx.request.body.type || [0];<commit_msg>update code<commit_after>'use strict';
/**
 * 文章相关
 */

const model = require('../model');
const url = require('url');
const utils = require('../utils');
let Article = model.Article;
let ArticleSort = model.ArticleSort;
let Sort = model.Sort;

/**
 * 直接 url.parse(ctx.request.url, true).query;
 * 结果是 { page: '1', size: '10', name: '顺' }
 * 1.支持中文
 * 2.由于是通过url传递参数，因此参数全是字符。使用时需要强转
 * @param ctx
 * @param next
 */
var fn_article_list_get = async(ctx, next) => {
    let params = url.parse(ctx.request.url, true).query;
    let currentPage = params.page * 1 || 1;
    if (currentPage <= 0) {
        currentPage = 1;
    }
    let pageSize = params.size * 1 || 10;
    if (pageSize <= 0) {
        pageSize = 10;
    }
    let type = params.type || '0';
    let articleList;
    /**
     * 多取出一个，方便判断是否还有更多。
     */
    if (type === '0') {
        articleList = await Article.findAll({
            limit: pageSize + 1,
            offset: (currentPage - 1) * pageSize,
            order: [['updatedAt', 'DESC']],
            attributes: ['id', 'title', 'des', 'userId', 'updatedAt'],
            where: {
                id: {$ne: 0}
            }
        });
    } else {
        articleList = await ArticleSort.findAll({
            limit: pageSize + 1,
            offset: (currentPage - 1) * pageSize,
            where: {
                sortId: type,
                id: {$ne: 0}
            },
            attributes: ['sortId'],
            include: {
                model: Article,
                order: [['updatedAt', 'DESC']],
                attributes: ['id', 'title', 'des', 'userId', 'updatedAt']
            }
        });
        let articleListTemp = [];
        for (let i = 0; i < articleList.length; i++) {
            let temp = {};
            temp.id = articleList[i].article.id;
            temp.title = articleList[i].article.title;
            temp.des = articleList[i].article.des;
            temp.userId = articleList[i].article.userId;
            temp.updatedAt = articleList[i].article.updatedAt;
            articleListTemp[i] = temp;
        }
        articleList = articleListTemp;
    }
    if (articleList.length < pageSize + 1) {
        ctx.rest(articleList, false);
    } else {
        ctx.rest(articleList.slice(0, pageSize), true);
    }
};

var fn_article_get = async(ctx, next) => {
    let params = url.parse(ctx.request.url, true).query;
    var article = await Article.findOne({
        where: {
            id: params.id || 0
        }
    });
    console.log(params.id);
    ctx.rest(article);
};

var fn_article_post = async(ctx, next) => {
    let article = {};
    article.title = ctx.request.body.title || '';
    article.des = ctx.request.body.des || '';
    article.content = ctx.request.body.content || '';
    article.userId = ctx.request.body.userId || 1;
    var sorts = ctx.request.body.type || [0];
{'fraction_non_alphanumeric': 0.16010591350397177, 'fraction_numerical': 0.010238305383936452, 'mean_word_length': 1.9026639344262295, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '622696', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1850, 'n_words': 400}