index.js 1.5 KB
var express = require('express');
var createError = require('http-errors');
var router = express.Router();
const post = require('../../../model/video');
const tags = require('../../../model/tagList');

router.get('/list', (req, res, next) => {
	let { tags, skip } = req.query;
	let joinedTag;
	console.log(tags);
	if (tags) joinedTag = tags.join('|');
	else {
		joinedTag = '';
	}
	let regexsearch = { tag: { $regex: joinedTag, $options: 'si' } };
	skip = parseInt(skip);
	post
		.find(regexsearch)
		.sort({ _id: -1 })
		.limit(10)
		.skip(skip)
		.then((rs) => {
			res.send({ success: true, d: rs });
		})
		.catch((e) => {
			console.log(e);
			res.send({ success: false, msg: e.message });
		});
});

router.get('/tag', (req, res, next) => {
	tags
		.find()
		.then((rs) => {
			var temp = [];
			rs.forEach((element) => {
				element.tag.forEach((element) => {
					temp.push(element);
				});
			});
			var uniqArray = Array.from(new Set(temp));
			res.send({ success: true, d: uniqArray });
		})
		.catch((e) => {
			console.log(e);
			res.send({ success: false, msg: e.message });
		});
});

router.delete('/:_id', (req, res, next) => {
	const _id = req.params._id;
	post
		.findOne({ _id })
		.then((r) => {
			console.log(r);
			return post.deleteOne({ _id });
		})
		.then((r) => {
			res.send({ success: true, d: r });
		})
		.catch((e) => {
			console.log(e);
			res.send({ success: false, msg: e.message });
		});
});

router.all('*', function (req, res, next) {
	next(new Error('Wrong Url!'));
});

module.exports = router;