{"size": 1995, "ext": "js", "max_stars_repo_path": "app/api/blog/v1/article.js", "max_stars_repo_name": "edgardong/xscms", "max_stars_repo_stars_event_min_datetime": "2021-04-26T07:10:03.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-26T07:10:03.000Z", "max_issues_repo_path": "app/api/blog/v1/article.js", "max_issues_repo_name": "edgardong/xscms", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/api/blog/v1/article.js", "max_forks_repo_name": "edgardong/xscms", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.0, "max_line_length": 66, "alphanum_fraction": 0.6666666667}
const Router = require('koa-router')
const router = new Router({
	prefix: '/api/blog/v1/article'
})

const {
	PaginationValidator,
	PositiveIntegerValidator,
	WecValidator
} = require('../../../validators/validator')

const Article = require('../../../models/blog/article')

router.get('/pagination', async (ctx, next) => {
	const params = await new PaginationValidator().validate(ctx)
	const result = await Article.getPaginationArticle(params)
	ctx.body = {
		data: result
	}
})

router.get('/list', async (ctx, next) => {
	const params = await new WecValidator().validate(ctx)
	const result = await Article.getArticleList(params)
	ctx.body = {
		data: result
	}
})

router.get('/latest', async (ctx, next) => {
	const result = await Article.getLatestArticleList()
	ctx.body = result.data

})

router.get('/hot', async (ctx, next) => {
	const result = await Article.getHotArticleList()
	ctx.body = result.data

})

router.get('/relevant', async (ctx, next) => {
	const params = await new WecValidator().validate(ctx)
	const result = await Article.getRelevantArticleList(params)
	ctx.body = result.data ||[]

})

router.get('/:id', async (ctx, next) => {
	const result = await Article.findOne({
		where: {
			id: ctx.params.id
		}
	})

	if(result){

		// 更新阅读量
		await Article.update({
			read_count: result.read_count + 1
		}, {
			where: {
				id: ctx.params.id
			}
		})
		ctx.body = result
	}else {
		ctx.body = '??'
	}
})

router.post('/', async (ctx, next) => {
	const data = await new WecValidator().validate(ctx)
	const result = await Article.saveArticle(data)
	ctx.body = result
})

router.post('/publish', async (ctx, next) => {
	const data = await new WecValidator().validate(ctx)
	const result = await Article.publishArticle(data)
	ctx.body = result
})

router.del('/', async (ctx, next) => {
	const params = await new PositiveIntegerValidator().validate(ctx)
	const result = await Article.destroy({
		where: {
			id: params.id
		}
	})

	ctx.body = result
})

module.exports = router